<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>جرد <?= e($audit->audit_number) ?><?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<?php if ($audit->status === 'pending_approval' && can('stock.audit')): ?>
<form method="POST" action="/inventory/audits/<?= (int) $audit->id ?>/approve" style="display:inline;">
    <?= csrf_field() ?>
    <button type="submit" class="btn" style="background:#059669;color:#fff;border:none;" onclick="return confirm('هل أنت متأكد من اعتماد هذا الجرد؟');">
        <i data-lucide="check-circle" style="width:15px;height:15px;vertical-align:middle;margin-left:4px;"></i> اعتماد الجرد
    </button>
</form>
<?php endif; ?>
<?php if ($audit->status === 'in_progress' && can('stock.audit')): ?>
<a href="/inventory/audits/<?= (int) $audit->id ?>/count" class="btn" style="background:#2563EB;color:#fff;border:none;">
    <i data-lucide="list-checks" style="width:15px;height:15px;vertical-align:middle;margin-left:4px;"></i> متابعة العد
</a>
<?php endif; ?>
<a href="/inventory/audits" 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'); ?>

<?php
$statusColors = [
    'draft'            => ['bg' => '#F3F4F6', 'color' => '#6B7280', 'label' => 'مسودة'],
    'in_progress'      => ['bg' => '#EFF6FF', 'color' => '#2563EB', 'label' => 'قيد التنفيذ'],
    'pending_approval' => ['bg' => '#FFF7ED', 'color' => '#D97706', 'label' => 'بانتظار الموافقة'],
    'approved'         => ['bg' => '#ECFDF5', 'color' => '#059669', 'label' => 'معتمد'],
    'cancelled'        => ['bg' => '#FEE2E2', 'color' => '#DC2626', 'label' => 'ملغى'],
];
$auditTypeLabels = [
    'full'       => 'جرد شامل',
    'partial'    => 'جرد جزئي',
    'spot_check' => 'جرد عشوائي',
];
$st = $audit->status ?? 'draft';
$stInfo = $statusColors[$st] ?? $statusColors['draft'];
$at = $audit->audit_type ?? 'full';
$atLabel = $auditTypeLabels[$at] ?? $at;
?>

<!-- Audit Info Card -->
<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="clipboard-list" style="width:18px;height:18px;color:#1e3a5f;"></i>
        <h3 style="margin:0;color:#1e3a5f;font-size:15px;">بيانات الجرد</h3>
    </div>
    <div style="padding:20px;">
        <div style="display:grid;grid-template-columns:1fr 1fr;gap:0;">
            <table style="width:100%;font-size:14px;">
                <tr>
                    <td style="padding:10px 0;color:#6B7280;width:40%;">رقم الجرد</td>
                    <td style="padding:10px 0;font-weight:600;">
                        <code style="font-size:12px;background:#F3F4F6;padding:2px 8px;border-radius:4px;"><?= e($audit->audit_number) ?></code>
                    </td>
                </tr>
                <tr>
                    <td style="padding:10px 0;color:#6B7280;">المخزن</td>
                    <td style="padding:10px 0;font-weight:600;"><?= e($warehouse['name_ar'] ?? '') ?></td>
                </tr>
                <tr>
                    <td style="padding:10px 0;color:#6B7280;">نوع الجرد</td>
                    <td style="padding:10px 0;font-weight:600;"><?= e($atLabel) ?></td>
                </tr>
            </table>
            <table style="width:100%;font-size:14px;">
                <tr>
                    <td style="padding:10px 0;color:#6B7280;width:40%;">تاريخ الجرد</td>
                    <td style="padding:10px 0;"><?= e($audit->audit_date ?? '—') ?></td>
                </tr>
                <tr>
                    <td style="padding:10px 0;color:#6B7280;">الحالة</td>
                    <td style="padding:10px 0;">
                        <span style="display:inline-block;padding:3px 10px;border-radius:10px;font-size:12px;font-weight:600;background:<?= $stInfo['bg'] ?>;color:<?= $stInfo['color'] ?>;">
                            <?= e($stInfo['label']) ?>
                        </span>
                    </td>
                </tr>
                <?php if (!empty($audit->notes)): ?>
                <tr>
                    <td style="padding:10px 0;color:#6B7280;">ملاحظات</td>
                    <td style="padding:10px 0;"><?= e($audit->notes) ?></td>
                </tr>
                <?php endif; ?>
            </table>
        </div>
    </div>
</div>

<!-- Summary Cards -->
<?php
$totalItems = count($items);
$countedItems = 0;
$varianceItems = 0;
foreach ($items as $itm) {
    if ($itm['physical_quantity'] !== null) $countedItems++;
    $v = (float) ($itm['variance'] ?? 0);
    if ($v != 0) $varianceItems++;
}
?>
<div style="display:grid;grid-template-columns:1fr 1fr 1fr;gap:15px;margin-bottom:20px;">
    <div class="card" style="padding:20px;text-align:center;">
        <div style="font-size:13px;color:#6B7280;margin-bottom:6px;">إجمالي الأصناف</div>
        <div style="font-size:28px;font-weight:800;color:#1e3a5f;"><?= $totalItems ?></div>
    </div>
    <div class="card" style="padding:20px;text-align:center;">
        <div style="font-size:13px;color:#6B7280;margin-bottom:6px;">تم العد</div>
        <div style="font-size:28px;font-weight:800;color:#2563EB;"><?= $countedItems ?></div>
    </div>
    <div class="card" style="padding:20px;text-align:center;">
        <div style="font-size:13px;color:#6B7280;margin-bottom:6px;">الفروقات</div>
        <div style="font-size:28px;font-weight:800;color:<?= $varianceItems > 0 ? '#DC2626' : '#059669' ?>;"><?= $varianceItems ?></div>
    </div>
</div>

<!-- Items Table -->
<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="package" style="width:18px;height:18px;color:#D97706;"></i>
        <h3 style="margin:0;color:#D97706;font-size:15px;">أصناف الجرد</h3>
    </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>
                    <th>الحالة</th>
                </tr>
            </thead>
            <tbody>
                <?php $rowNum = 0; ?>
                <?php foreach ($items as $item): ?>
                <?php
                $rowNum++;
                $variance = (float) ($item['variance'] ?? 0);
                $varianceCost = (float) ($item['variance_cost'] ?? 0);
                $itemStatus = $item['status'] ?? 'pending';
                if ($variance > 0) {
                    $varColor = '#059669';
                    $varBg = '#ECFDF5';
                    $varSign = '+';
                } elseif ($variance < 0) {
                    $varColor = '#DC2626';
                    $varBg = '#FEE2E2';
                    $varSign = '';
                } else {
                    $varColor = '#6B7280';
                    $varBg = '';
                    $varSign = '';
                }
                $rowBg = $variance != 0 ? ($variance > 0 ? '#F0FDF4' : '#FEF2F2') : '';
                ?>
                <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="font-weight:600;direction:ltr;text-align:left;"><?= number_format((float) ($item['system_quantity'] ?? 0), 2) ?></td>
                    <td style="font-weight:600;direction:ltr;text-align:left;">
                        <?= $item['physical_quantity'] !== null ? number_format((float) $item['physical_quantity'], 2) : '—' ?>
                    </td>
                    <td style="font-weight:700;direction:ltr;text-align:left;color:<?= $varColor ?>;">
                        <?php if ($variance != 0): ?>
                        <span style="display:inline-block;padding:2px 8px;border-radius:8px;background:<?= $varBg ?>;color:<?= $varColor ?>;">
                            <?= $varSign ?><?= number_format($variance, 2) ?>
                        </span>
                        <?php else: ?>
                        <span style="color:#6B7280;">0.00</span>
                        <?php endif; ?>
                    </td>
                    <td style="font-weight:600;direction:ltr;text-align:left;color:<?= $varColor ?>;">
                        <?php if ($varianceCost != 0): ?>
                        <?= $varSign ?><?= money(abs($varianceCost)) ?>
                        <?php else: ?>
                        <span style="color:#6B7280;">—</span>
                        <?php endif; ?>
                    </td>
                    <td>
                        <?php if ($itemStatus === '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>

<script>
document.addEventListener('DOMContentLoaded', function() {
    if (typeof lucide !== 'undefined') {
        lucide.createIcons();
    }
});
</script>
<?php $__template->endSection(); ?>
