<?php
/**
 * @var object $adjustments
 * @var array $products
 * @var string $search
 * @var string $type
 */
?>
<div class="page-title mb-4">
    <h5><i class="fas fa-balance-scale me-2 text-gold"></i>Stock Adjustments</h5>
</div>

<div class="row g-4">
    <div class="col-lg-5">
        <div class="glass-card p-0">
            <div class="card-header">
                <div class="card-header-left">
                    <i class="fas fa-plus-circle card-icon"></i>
                    <h6 class="card-title">New Adjustment</h6>
                </div>
            </div>
            <div class="card-body">
                <form method="POST" action="<?= url('stock/adjustments/store') ?>">
                    <?= csrf_field() ?>

                    <div class="mb-3">
                        <label class="form-label">Product <span class="text-danger">*</span></label>
                        <select name="product_id" class="form-select bg-transparent <?= errorClass('product_id') ?>" required>
                            <option value="">Select Product</option>
                            <?php foreach ($products as $p): ?>
                                <option value="<?= $p->id ?>">
                                    <?= e($p->name) ?> (<?= e($p->sku) ?>) - Stock: <?= (int)$p->current_stock ?>
                                </option>
                            <?php endforeach; ?>
                        </select>
                        <?= errorMsg('product_id') ?>
                    </div>

                    <div class="row g-3 mb-3">
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label class="form-label">Adjustment Type <span class="text-danger">*</span></label>
                                <select name="adjustment_type" class="form-select bg-transparent <?= errorClass('adjustment_type') ?>" required>
                                    <option value="">Select Type</option>
                                    <option value="addition">Addition (+)</option>
                                    <option value="deduction">Deduction (-)</option>
                                    <option value="correction">Correction (Set exact)</option>
                                </select>
                                <?= errorMsg('adjustment_type') ?>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label class="form-label">Quantity <span class="text-danger">*</span></label>
                                <input type="number" name="quantity" class="form-control bg-transparent <?= errorClass('quantity') ?>"
                                       value="<?= e(old('quantity', '1')) ?>" min="1" required>
                                <?= errorMsg('quantity') ?>
                            </div>
                        </div>
                    </div>

                    <div class="mb-3">
                        <label class="form-label">Reason <span class="text-danger">*</span></label>
                        <textarea name="reason" class="form-control bg-transparent <?= errorClass('reason') ?>"
                                  rows="3" maxlength="1000" required><?= e(old('reason')) ?></textarea>
                        <?= errorMsg('reason') ?>
                    </div>

                    <div class="mb-3">
                        <label class="form-label">Reference (optional)</label>
                        <input type="text" name="reference" class="form-control bg-transparent <?= errorClass('reference') ?>"
                               value="<?= e(old('reference')) ?>" maxlength="255" placeholder="e.g., damaged, expired, count">
                        <?= errorMsg('reference') ?>
                    </div>

                    <button type="submit" class="btn btn-gold">
                        <i class="fas fa-save me-1"></i>Record Adjustment
                    </button>
                </form>
            </div>
        </div>
    </div>

    <div class="col-lg-7">
        <div class="glass-card p-0">
            <div class="card-header">
                <div class="card-header-left">
                    <i class="fas fa-history card-icon"></i>
                    <h6 class="card-title">Adjustment History</h6>
                </div>
                <div>
                    <div class="btn-group btn-group-sm">
                        <a href="<?= url('stock/adjustments') ?>" class="btn btn-sm <?= empty($type) ? 'btn-gold' : 'btn-outline-gold' ?>">All</a>
                        <a href="<?= url('stock/adjustments?type=addition') ?>" class="btn btn-sm <?= $type === 'addition' ? 'btn-gold' : 'btn-outline-gold' ?>">Addition</a>
                        <a href="<?= url('stock/adjustments?type=deduction') ?>" class="btn btn-sm <?= $type === 'deduction' ? 'btn-gold' : 'btn-outline-gold' ?>">Deduction</a>
                        <a href="<?= url('stock/adjustments?type=correction') ?>" class="btn btn-sm <?= $type === 'correction' ? 'btn-gold' : 'btn-outline-gold' ?>">Correction</a>
                    </div>
                </div>
            </div>
            <div class="card-body">
                <div class="mb-3">
                    <form method="GET" action="<?= url('stock/adjustments') ?>" class="d-flex gap-2">
                        <div class="input-group">
                            <input type="text" name="search" class="form-control form-control-sm bg-transparent"
                                   placeholder="Search by product or reason..." value="<?= e($search) ?>">
                            <button type="submit" class="btn btn-sm btn-gold">Search</button>
                            <?php if (!empty($search)): ?>
                                <a href="<?= url('stock/adjustments') ?>" class="btn btn-sm btn-outline-secondary">
                                    <i class="fas fa-times"></i>
                                </a>
                            <?php endif; ?>
                        </div>
                    </form>
                </div>

                <div class="table-responsive">
                    <table class="table table-dark table-hover mb-0">
                        <thead>
                            <tr>
                                <th>Date</th>
                                <th>Product</th>
                                <th>Type</th>
                                <th class="text-center">Qty</th>
                                <th class="text-center">Before</th>
                                <th class="text-center">After</th>
                                <th>Reason</th>
                                <th>By</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php if (empty($adjustments->data)): ?>
                                <tr>
                                    <td colspan="8" class="text-center text-muted py-3">No adjustments found</td>
                                </tr>
                            <?php else: ?>
                                <?php foreach ($adjustments->data as $adj): ?>
                                    <tr>
                                        <td><?= formatDateTime($adj->created_at) ?></td>
                                        <td>
                                            <a href="<?= url('inventory/show/' . $adj->product_id) ?>" class="text-gold">
                                                <?= e($adj->product_name ?? '-') ?>
                                            </a>
                                        </td>
                                        <td>
                                            <span class="badge bg-<?= $adj->adjustment_type === 'addition' ? 'success' : ($adj->adjustment_type === 'deduction' ? 'danger' : 'warning') ?>">
                                                <?= e(ucfirst($adj->adjustment_type)) ?>
                                            </span>
                                        </td>
                                        <td class="text-center"><?= (int)$adj->quantity ?></td>
                                        <td class="text-center"><?= (int)$adj->stock_before ?></td>
                                        <td class="text-center"><?= (int)$adj->stock_after ?></td>
                                        <td><small><?= e(truncate($adj->reason ?? '', 40)) ?></small></td>
                                        <td><small><?= e($adj->user_first_name ?? '') ?> <?= e($adj->user_last_name ?? '') ?></small></td>
                                    </tr>
                                <?php endforeach; ?>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>

                <?php if ($adjustments->totalPages > 1): ?>
                    <div class="mt-3">
                        <?= paginateLinks($adjustments, url('stock/adjustments')) ?>
                    </div>
                <?php endif; ?>
            </div>
        </div>
    </div>
</div>
