<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>الأصناف<?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<a href="/inventory/items/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'); ?>

<!-- Search & Filters -->
<div class="card" style="margin-bottom:20px;padding:15px;">
    <form method="GET" action="/inventory/items" style="display:flex;gap:10px;flex-wrap:wrap;align-items:end;">
        <div style="flex:1;min-width:200px;">
            <label class="form-label" style="font-size:12px;">بحث</label>
            <input type="text" name="q" value="<?= e($filters['q'] ?? '') ?>" placeholder="اسم الصنف، SKU، باركود..." class="form-input">
        </div>
        <div style="min-width:160px;">
            <label class="form-label" style="font-size:12px;">التصنيف</label>
            <select name="category_id" class="form-select">
                <option value="">الكل</option>
                <?php foreach ($categories as $cat): ?>
                <option value="<?= (int) $cat['id'] ?>" <?= ($filters['category_id'] ?? '') == $cat['id'] ? 'selected' : '' ?>><?= e($cat['name_ar']) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div style="min-width:150px;">
            <label class="form-label" style="font-size:12px;">نوع التتبع</label>
            <select name="tracking_type" class="form-select">
                <option value="">الكل</option>
                <?php foreach ($trackingTypes as $key => $label): ?>
                <option value="<?= e($key) ?>" <?= ($filters['tracking_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/items" class="btn btn-sm btn-outline" style="color:#6B7280;">مسح</a>
    </form>
</div>

<!-- Items Table -->
<?php if (!empty($items)): ?>
<div class="card">
    <div class="table-responsive">
        <table class="data-table">
            <thead>
                <tr>
                    <th>SKU</th>
                    <th>الاسم</th>
                    <th>التصنيف</th>
                    <th>نوع التتبع</th>
                    <th>الوحدة</th>
                    <th>سعر البيع (عضو)</th>
                    <th>للبيع</th>
                    <th>الإجراءات</th>
                </tr>
            </thead>
            <tbody>
                <?php
                $trackingColors = [
                    'standard' => ['bg' => '#F3F4F6', 'color' => '#6B7280'],
                    'expiry'   => ['bg' => '#FFF7ED', 'color' => '#D97706'],
                    'asset'    => ['bg' => '#EFF6FF', 'color' => '#2563EB'],
                ];
                $unitLabels = [
                    'piece' => 'قطعة', 'kg' => 'كيلوجرام', 'liter' => 'لتر',
                    'box' => 'صندوق', 'pack' => 'عبوة', 'meter' => 'متر', 'set' => 'طقم',
                ];
                ?>
                <?php foreach ($items as $item): ?>
                <tr>
                    <td style="direction:ltr;text-align:left;font-family:monospace;font-weight:600;"><?= e($item['sku'] ?? '') ?></td>
                    <td>
                        <a href="/inventory/items/<?= (int) $item['id'] ?>" style="color:#0D7377;font-weight:600;text-decoration:none;">
                            <?= e($item['name_ar'] ?? '') ?>
                        </a>
                        <?php if (!empty($item['name_en'])): ?>
                        <div style="font-size:11px;color:#9CA3AF;"><?= e($item['name_en']) ?></div>
                        <?php endif; ?>
                    </td>
                    <td><?= (int) ($item['category_id'] ?? 0) ?: '—' ?></td>
                    <td>
                        <?php
                        $tt = $item['tracking_type'] ?? 'standard';
                        $ttColors = $trackingColors[$tt] ?? $trackingColors['standard'];
                        $ttLabel = $trackingTypes[$tt] ?? $tt;
                        ?>
                        <span style="display:inline-block;padding:3px 10px;border-radius:10px;font-size:12px;font-weight:600;background:<?= $ttColors['bg'] ?>;color:<?= $ttColors['color'] ?>;">
                            <?= e($ttLabel) ?>
                        </span>
                    </td>
                    <td><?= e($unitLabels[$item['unit_of_measure'] ?? ''] ?? ($item['unit_of_measure'] ?? '')) ?></td>
                    <td style="font-weight:700;direction:ltr;text-align:left;"><?= money($item['sale_price_member'] ?? 0) ?></td>
                    <td>
                        <?php if ((int) ($item['is_sellable'] ?? 0)): ?>
                        <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:#FEE2E2;color:#DC2626;">لا</span>
                        <?php endif; ?>
                    </td>
                    <td>
                        <div style="display:flex;gap:6px;">
                            <a href="/inventory/items/<?= (int) $item['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>
                            <a href="/inventory/items/<?= (int) $item['id'] ?>/edit" class="btn btn-sm btn-outline" style="font-size:12px;padding:4px 10px;">
                                <i data-lucide="edit-3" style="width:13px;height:13px;vertical-align:middle;"></i> تعديل
                            </a>
                        </div>
                    </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="package" 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['q']) || !empty($filters['category_id']) || !empty($filters['tracking_type']) || !empty($filters['warehouse_id'])): ?>
            لا توجد نتائج مطابقة لبحثك. جرب تغيير معايير البحث.
        <?php else: ?>
            ابدأ بإضافة صنف جديد للمخزون.
        <?php endif; ?>
    </p>
    <a href="/inventory/items/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(); ?>
