<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>العد الفعلي — <?= e($audit->audit_number) ?><?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<a href="/inventory/audits/<?= (int) $audit->id ?>" class="btn btn-outline"><i data-lucide="arrow-right" style="width:15px;height:15px;vertical-align:middle;margin-left:4px;"></i> العودة للجرد</a>
<?php $__template->endSection(); ?>

<?php $__template->section('content'); ?>

<form method="POST" action="/inventory/audits/<?= (int) $audit->id ?>/count" id="countForm">
    <?= csrf_field() ?>

    <div class="card" style="margin-bottom:20px;">
        <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;display:flex;align-items:center;gap:8px;">
            <i data-lucide="list-checks" style="width:18px;height:18px;color:#0D7377;"></i>
            <h3 style="margin:0;color:#0D7377;font-size:15px;">العد الفعلي للأصناف</h3>
            <div style="margin-right:auto;font-size:13px;color:#6B7280;">
                رقم الجرد: <code style="font-size:12px;background:#F3F4F6;padding:2px 8px;border-radius:4px;"><?= e($audit->audit_number) ?></code>
            </div>
        </div>

        <?php if (!empty($items)): ?>
        <div class="table-responsive">
            <table class="data-table">
                <thead>
                    <tr>
                        <th>#</th>
                        <th>الصنف</th>
                        <th>SKU</th>
                        <th>الوحدة</th>
                        <th>الكمية بالنظام</th>
                        <th>الكمية الفعلية</th>
                        <th>الحالة</th>
                    </tr>
                </thead>
                <tbody>
                    <?php $rowNum = 0; ?>
                    <?php foreach ($items as $item): ?>
                    <?php
                    $rowNum++;
                    $sysQty = (float) ($item['system_quantity'] ?? 0);
                    $physQty = $item['physical_quantity'];
                    $hasVariance = $physQty !== null && (float) $physQty !== $sysQty;
                    $rowBg = $hasVariance ? '#FFF7ED' : '';
                    ?>
                    <tr style="<?= $rowBg ? 'background:' . $rowBg . ';' : '' ?>">
                        <td style="color:#6B7280;font-size:13px;"><?= $rowNum ?></td>
                        <td style="font-weight:600;"><?= e($item['item_name'] ?? '') ?></td>
                        <td>
                            <code style="font-size:11px;background:#F3F4F6;padding:2px 6px;border-radius:4px;"><?= e($item['sku'] ?? '—') ?></code>
                        </td>
                        <td style="color:#6B7280;"><?= e($item['unit_of_measure'] ?? '—') ?></td>
                        <td style="font-weight:700;direction:ltr;text-align:left;color:#6B7280;"><?= number_format($sysQty, 2) ?></td>
                        <td style="width:150px;">
                            <input type="number"
                                   name="physical_quantities[<?= (int) $item['id'] ?>]"
                                   value="<?= e($physQty ?? '') ?>"
                                   class="form-input physical-qty"
                                   data-system="<?= $sysQty ?>"
                                   step="0.01"
                                   min="0"
                                   placeholder="0.00"
                                   style="direction:ltr;text-align:left;width:130px;">
                        </td>
                        <td>
                            <?php
                            $st = $item['status'] ?? 'pending';
                            if ($st === 'counted'):
                            ?>
                            <span style="display:inline-block;padding:3px 10px;border-radius:10px;font-size:12px;font-weight:600;background:#ECFDF5;color:#059669;">تم العد</span>
                            <?php else: ?>
                            <span style="display:inline-block;padding:3px 10px;border-radius:10px;font-size:12px;font-weight:600;background:#F3F4F6;color:#6B7280;">معلق</span>
                            <?php endif; ?>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
        <?php else: ?>
        <div style="padding:40px 20px;text-align:center;color:#6B7280;">
            <i data-lucide="package-x" style="width:36px;height:36px;color:#D1D5DB;margin-bottom:8px;"></i>
            <p style="margin:0;">لا توجد أصناف في هذا الجرد</p>
        </div>
        <?php endif; ?>
    </div>

    <?php if (!empty($items)): ?>
    <div style="display:flex;gap:10px;justify-content:flex-start;">
        <button type="submit" class="btn btn-primary" style="padding:12px 30px;font-size:15px;">
            <i data-lucide="save" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i>
            حفظ العد
        </button>
        <a href="/inventory/audits/<?= (int) $audit->id ?>" class="btn btn-outline" style="padding:12px 30px;font-size:15px;">إلغاء</a>
    </div>
    <?php endif; ?>
</form>

<script>
document.addEventListener('DOMContentLoaded', function() {
    if (typeof lucide !== 'undefined') {
        lucide.createIcons();
    }

    // Highlight rows with variance on input change
    document.querySelectorAll('.physical-qty').forEach(function(input) {
        input.addEventListener('input', function() {
            var row = this.closest('tr');
            var sysQty = parseFloat(this.dataset.system) || 0;
            var physQty = parseFloat(this.value);
            if (!isNaN(physQty) && physQty !== sysQty) {
                row.style.background = '#FFF7ED';
            } else {
                row.style.background = '';
            }
        });
    });
});
</script>
<?php $__template->endSection(); ?>
