<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>الخصومات الخاصة<?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<?php if (can('pricing.special_discounts.create')): ?>
<a href="/pricing/special-discounts/create" class="btn btn-primary">
    <i data-lucide="plus" style="width:15px;height:15px;vertical-align:middle;margin-left:4px;"></i> إضافة خصم جديد
</a>
<?php endif; ?>
<a href="/pricing" 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'); ?>

<!-- Search -->
<div class="card" style="margin-bottom:15px;padding:15px;">
    <form method="GET" action="/pricing/special-discounts" style="display:flex;gap:10px;align-items:end;">
        <div class="form-group" style="flex:1;">
            <label class="form-label">بحث</label>
            <input type="text" name="q" value="<?= e($filters['search'] ?? '') ?>" class="form-input" placeholder="اسم الخصم...">
        </div>
        <button type="submit" class="btn btn-primary" style="padding:8px 20px;">
            <i data-lucide="search" style="width:14px;height:14px;vertical-align:middle;"></i> بحث
        </button>
    </form>
</div>

<!-- Table -->
<div class="card">
    <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;display:flex;align-items:center;gap:8px;">
        <i data-lucide="percent" style="width:18px;height:18px;color:#0D7377;"></i>
        <h3 style="margin:0;color:#0D7377;font-size:15px;">قائمة الخصومات الخاصة</h3>
    </div>
    <?php if (!empty($rows)): ?>
    <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>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($rows as $row): ?>
                <tr>
                    <td><?= (int) $row['id'] ?></td>
                    <td style="font-weight:600;"><?= e($row['name_ar']) ?></td>
                    <td style="font-size:12px;">
                        <?= match($row['discount_type'] ?? 'percentage') {
                            'percentage' => 'نسبة مئوية',
                            'fixed_amount' => 'مبلغ ثابت',
                            'free_subscription' => 'اشتراك مجاني',
                            default => $row['discount_type'] ?? '—'
                        } ?>
                    </td>
                    <td style="font-weight:700;color:#0D7377;">
                        <?php if (($row['discount_type'] ?? 'percentage') === 'percentage'): ?>
                            <?= e($row['discount_percentage']) ?>%
                        <?php elseif (($row['discount_type'] ?? '') === 'fixed_amount'): ?>
                            <?= money($row['fixed_amount'] ?? '0') ?>
                        <?php else: ?>
                            —
                        <?php endif; ?>
                    </td>
                    <td style="font-size:12px;">
                        <?= match($row['applies_to'] ?? 'membership_fee') {
                            'membership_fee' => 'العضوية',
                            'subscription' => 'الاشتراك',
                            'all' => 'الكل',
                            default => '—'
                        } ?>
                    </td>
                    <td style="font-size:12px;">
                        <?= match($row['condition_type'] ?? 'none') {
                            'none' => '<span style="color:#9CA3AF;">بدون شرط</span>',
                            'full_payment' => 'سداد كامل',
                            'min_payment' => 'حد أدنى ' . money($row['condition_min_amount'] ?? '0'),
                            default => '—'
                        } ?>
                    </td>
                    <td style="font-size:12px;">
                        <?php $bonus = (int) ($row['bonus_free_subscription_years'] ?? 0); ?>
                        <?php if ($bonus > 0): ?>
                            <span style="color:#059669;font-weight:600;"><?= $bonus ?> سنة مجانية</span>
                        <?php else: ?>
                            <span style="color:#9CA3AF;">—</span>
                        <?php endif; ?>
                    </td>
                    <td style="font-size:11px;color:#6B7280;">
                        <?php
                        $from = $row['effective_from'] ?? null;
                        $to = $row['effective_to'] ?? null;
                        if ($from && $to) {
                            echo e($from) . '<br>' . e($to);
                        } elseif ($from) {
                            echo 'من ' . e($from);
                        } elseif ($to) {
                            echo 'حتى ' . e($to);
                        } else {
                            echo '<span style="color:#9CA3AF;">دائم</span>';
                        }
                        ?>
                    </td>
                    <td>
                        <?php if ($row['is_active']): ?>
                            <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>
                    <td style="white-space:nowrap;">
                        <a href="/pricing/special-discounts/<?= (int) $row['id'] ?>/edit" class="btn btn-sm btn-outline" style="font-size:12px;padding:4px 10px;">
                            <i data-lucide="edit" style="width:12px;height:12px;vertical-align:middle;"></i> تعديل
                        </a>
                        <form method="POST" action="/pricing/special-discounts/<?= (int) $row['id'] ?>/toggle" style="display:inline;">
                            <?= csrf_field() ?>
                            <button type="submit" class="btn btn-sm btn-outline" style="font-size:12px;padding:4px 10px;<?= $row['is_active'] ? 'color:#DC2626;' : 'color:#059669;' ?>">
                                <?= $row['is_active'] ? 'تعطيل' : 'تفعيل' ?>
                            </button>
                        </form>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
    <?php if (!empty($pagination) && $pagination['last_page'] > 1): ?>
    <div style="padding:15px 20px;border-top:1px solid #E5E7EB;text-align:center;">
        <?php for ($i = 1; $i <= $pagination['last_page']; $i++): ?>
            <?php if ($i == $pagination['current_page']): ?>
                <span style="display:inline-block;padding:4px 12px;background:#0D7377;color:#fff;border-radius:4px;font-size:13px;margin:0 2px;"><?= $i ?></span>
            <?php else: ?>
                <a href="?page=<?= $i ?>&q=<?= urlencode($filters['search'] ?? '') ?>" style="display:inline-block;padding:4px 12px;color:#0D7377;text-decoration:none;font-size:13px;margin:0 2px;"><?= $i ?></a>
            <?php endif; ?>
        <?php endfor; ?>
    </div>
    <?php endif; ?>
    <?php else: ?>
    <div style="padding:60px 20px;text-align:center;">
        <div style="margin-bottom:15px;"><i data-lucide="percent" 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 15px;">أضف أنواع الخصومات الخاصة لتطبيقها على الأعضاء تلقائياً أو يدوياً.</p>
        <a href="/pricing/special-discounts/create" class="btn btn-primary">إضافة خصم جديد</a>
    </div>
    <?php endif; ?>
</div>

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