<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>عمليات الجرد<?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<a href="/inventory/audits/create" class="btn btn-primary"><i data-lucide="plus" style="width:16px;height:16px;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' => 'جرد عشوائي',
];
$auditTypeColors = [
    'full'       => ['bg' => '#F0FDFA', 'color' => '#0D7377'],
    'partial'    => ['bg' => '#FFF7ED', 'color' => '#D97706'],
    'spot_check' => ['bg' => '#EFF6FF', 'color' => '#2563EB'],
];
?>

<!-- Search & Filters -->
<div class="card" style="margin-bottom:20px;padding:15px;">
    <form method="GET" action="/inventory/audits" style="display:flex;gap:10px;flex-wrap:wrap;align-items:end;">
        <div style="min-width:160px;">
            <label class="form-label" style="font-size:12px;">الحالة</label>
            <select name="status" class="form-select">
                <option value="">الكل</option>
                <?php foreach ($statuses as $key => $label): ?>
                <option value="<?= e($key) ?>" <?= ($filters['status'] ?? '') === $key ? 'selected' : '' ?>><?= e($label) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div style="min-width:160px;">
            <label class="form-label" style="font-size:12px;">نوع الجرد</label>
            <select name="audit_type" class="form-select">
                <option value="">الكل</option>
                <?php foreach ($auditTypes as $key => $label): ?>
                <option value="<?= e($key) ?>" <?= ($filters['audit_type'] ?? '') === $key ? 'selected' : '' ?>><?= e($label) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div style="min-width:160px;">
            <label class="form-label" style="font-size:12px;">المخزن</label>
            <select name="warehouse_id" class="form-select">
                <option value="">الكل</option>
                <?php foreach ($warehouses as $wh): ?>
                <option value="<?= (int) $wh['id'] ?>" <?= ($filters['warehouse_id'] ?? '') == $wh['id'] ? 'selected' : '' ?>><?= e($wh['name_ar']) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <button type="submit" class="btn btn-outline"><i data-lucide="search" style="width:16px;height:16px;vertical-align:middle;"></i> بحث</button>
        <a href="/inventory/audits" class="btn btn-sm btn-outline" style="color:#6B7280;">مسح</a>
    </form>
</div>

<!-- Audits Table -->
<?php if (!empty($audits)): ?>
<div class="card">
    <div class="table-responsive">
        <table class="data-table">
            <thead>
                <tr>
                    <th>رقم الجرد</th>
                    <th>المخزن</th>
                    <th>النوع</th>
                    <th>الحالة</th>
                    <th>تاريخ الجرد</th>
                    <th>إجمالي الأصناف</th>
                    <th>تم العد</th>
                    <th>الفروقات</th>
                    <th>الإجراءات</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($audits as $audit): ?>
                <?php
                $st = $audit['status'] ?? 'draft';
                $stInfo = $statusColors[$st] ?? $statusColors['draft'];
                $at = $audit['audit_type'] ?? 'full';
                $atLabel = $auditTypeLabels[$at] ?? $at;
                $atColors = $auditTypeColors[$at] ?? $auditTypeColors['full'];
                ?>
                <tr>
                    <td>
                        <a href="/inventory/audits/<?= (int) $audit['id'] ?>" style="color:#0D7377;font-weight:600;text-decoration:none;font-family:monospace;">
                            <?= e($audit['audit_number'] ?? '') ?>
                        </a>
                    </td>
                    <td style="font-weight:600;"><?= e($audit['warehouse_name'] ?? '') ?></td>
                    <td>
                        <span style="display:inline-block;padding:3px 10px;border-radius:10px;font-size:12px;font-weight:600;background:<?= $atColors['bg'] ?>;color:<?= $atColors['color'] ?>;">
                            <?= e($atLabel) ?>
                        </span>
                    </td>
                    <td>
                        <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>
                    <td style="white-space:nowrap;"><?= e($audit['audit_date'] ?? '—') ?></td>
                    <td style="text-align:center;font-weight:600;"><?= (int) ($audit['total_items'] ?? 0) ?></td>
                    <td style="text-align:center;font-weight:600;"><?= (int) ($audit['items_counted'] ?? 0) ?></td>
                    <td style="text-align:center;">
                        <?php $vc = (int) ($audit['variance_count'] ?? 0); ?>
                        <?php if ($vc > 0): ?>
                        <span style="display:inline-block;padding:3px 10px;border-radius:10px;font-size:12px;font-weight:600;background:#FEE2E2;color:#DC2626;"><?= $vc ?></span>
                        <?php else: ?>
                        <span style="color:#059669;font-weight:600;">0</span>
                        <?php endif; ?>
                    </td>
                    <td>
                        <a href="/inventory/audits/<?= (int) $audit['id'] ?>" class="btn btn-sm btn-outline" style="font-size:12px;padding:4px 10px;">
                            <i data-lucide="eye" style="width:13px;height:13px;vertical-align:middle;"></i> عرض
                        </a>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>

<?php if (isset($pagination) && ($pagination['last_page'] ?? 1) > 1): ?>
<div style="padding:15px;">
    <?php $__template->include('Shared.Components.pagination', ['pagination' => $pagination]); ?>
</div>
<?php endif; ?>

<?php else: ?>
<div class="card" style="padding:60px 20px;text-align:center;">
    <div style="margin-bottom:15px;">
        <i data-lucide="clipboard-list" style="width:48px;height:48px;color:#D1D5DB;"></i>
    </div>
    <h3 style="color:#6B7280;margin:0 0 8px;">لا توجد عمليات جرد</h3>
    <p style="color:#9CA3AF;font-size:14px;margin:0 0 20px;">
        <?php if (!empty($filters['status']) || !empty($filters['audit_type']) || !empty($filters['warehouse_id'])): ?>
            لا توجد نتائج مطابقة لبحثك. جرب تغيير معايير البحث.
        <?php else: ?>
            ابدأ بإنشاء عملية جرد جديدة.
        <?php endif; ?>
    </p>
    <a href="/inventory/audits/create" class="btn btn-primary"><i data-lucide="plus" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> جرد جديد</a>
</div>
<?php endif; ?>

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