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

<?php $__template->section('page_actions'); ?>
<?php if (can('supplier.manage')): ?>
<a href="/inventory/suppliers/create" class="btn btn-primary"><i data-lucide="plus" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> إضافة مورد</a>
<?php endif; ?>
<?php $__template->endSection(); ?>

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

<!-- Search & Filters -->
<div class="card" style="margin-bottom:20px;padding:15px;">
    <form method="GET" action="/inventory/suppliers" 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="اسم المورد، الكود، الهاتف..." class="form-input">
        </div>
        <div style="min-width:140px;">
            <label class="form-label" style="font-size:12px;">الحالة</label>
            <select name="is_active" class="form-select">
                <option value="">الكل</option>
                <option value="1" <?= ($filters['is_active'] ?? '') === '1' ? 'selected' : '' ?>>نشط</option>
                <option value="0" <?= ($filters['is_active'] ?? '') === '0' ? 'selected' : '' ?>>معطل</option>
            </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/suppliers" class="btn btn-sm btn-outline" style="color:#6B7280;">مسح</a>
    </form>
</div>

<!-- Suppliers Table -->
<?php if (!empty($suppliers)): ?>
<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>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($suppliers as $s): ?>
                <tr>
                    <td>
                        <code style="font-size:12px;background:#F3F4F6;padding:2px 8px;border-radius:4px;"><?= e($s['code'] ?? '—') ?></code>
                    </td>
                    <td>
                        <a href="/inventory/suppliers/<?= (int) $s['id'] ?>" style="color:#0D7377;font-weight:600;text-decoration:none;"><?= e($s['name_ar']) ?></a>
                    </td>
                    <td style="direction:ltr;text-align:right;font-size:13px;"><?= e($s['phone'] ?? '—') ?></td>
                    <td style="direction:ltr;text-align:right;font-size:13px;"><?= e($s['email'] ?? '—') ?></td>
                    <td>
                        <?php
                        $rating = (int) ($s['rating'] ?? 0);
                        for ($i = 1; $i <= 5; $i++):
                        ?>
                            <i data-lucide="star" style="width:14px;height:14px;vertical-align:middle;<?= $i <= $rating ? 'color:#D97706;fill:#D97706;' : 'color:#D1D5DB;' ?>"></i>
                        <?php endfor; ?>
                    </td>
                    <td>
                        <?php if ((int) ($s['is_active'] ?? 0)): ?>
                        <span style="color:#059669;font-weight:600;font-size:13px;">● نشط</span>
                        <?php else: ?>
                        <span style="color:#DC2626;font-weight:600;font-size:13px;">● معطل</span>
                        <?php endif; ?>
                    </td>
                    <td>
                        <div style="display:flex;gap:6px;">
                            <a href="/inventory/suppliers/<?= (int) $s['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>
                            <?php if (can('supplier.manage')): ?>
                            <a href="/inventory/suppliers/<?= (int) $s['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>
                            <?php endif; ?>
                        </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="truck" 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']) || ($filters['is_active'] ?? '') !== ''): ?>
            لا توجد نتائج مطابقة لبحثك. جرب تغيير معايير البحث.
        <?php else: ?>
            ابدأ بإضافة مورد جديد للنظام.
        <?php endif; ?>
    </p>
    <a href="/inventory/suppliers/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(); ?>
