<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>الباقات<?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<?php if (can('sales.create')): ?>
<a href="/sales/packages/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="/sales/packages" 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="/sales/packages" class="btn btn-sm btn-outline" style="color:#6B7280;">مسح</a>
    </form>
</div>

<!-- Packages Table -->
<?php if (!empty($packages)): ?>
<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 ($packages as $pkg): ?>
                <?php
                $memberPrice = (float) ($pkg['package_price_member'] ?? 0);
                $itemsTotal = (float) ($pkg['items_total_price'] ?? 0);
                $saving = $itemsTotal > 0 ? $itemsTotal - $memberPrice : 0;
                $savingPct = $itemsTotal > 0 ? round(($saving / $itemsTotal) * 100) : 0;
                ?>
                <tr>
                    <td style="font-family:monospace;font-weight:600;direction:ltr;text-align:left;"><?= e($pkg['code'] ?? '') ?></td>
                    <td>
                        <a href="/sales/packages/<?= (int) $pkg['id'] ?>" style="color:#0D7377;font-weight:600;text-decoration:none;">
                            <?= e($pkg['name_ar'] ?? '') ?>
                        </a>
                        <?php if (!empty($pkg['name_en'])): ?>
                        <div style="font-size:11px;color:#9CA3AF;"><?= e($pkg['name_en']) ?></div>
                        <?php endif; ?>
                    </td>
                    <td style="font-weight:700;direction:ltr;text-align:left;color:#059669;"><?= money($pkg['package_price_member'] ?? 0) ?></td>
                    <td style="font-weight:600;direction:ltr;text-align:left;"><?= money($pkg['package_price_nonmember'] ?? 0) ?></td>
                    <td>
                        <?php if ($saving > 0): ?>
                        <div style="font-weight:700;color:#059669;direction:ltr;text-align:left;"><?= money($saving) ?></div>
                        <div style="font-size:11px;color:#059669;">(<?= $savingPct ?>%)</div>
                        <?php else: ?>
                        <span style="color:#9CA3AF;">—</span>
                        <?php endif; ?>
                    </td>
                    <td>
                        <?php if ((int) ($pkg['is_active'] ?? 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="/sales/packages/<?= (int) $pkg['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="/sales/packages/<?= (int) $pkg['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="gift" 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="/sales/packages/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(); ?>
