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

<?php $__template->section('page_actions'); ?>
<?php if (can('pricing.regulatory_discounts.create')): ?>
<a href="/pricing/regulatory-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/regulatory-discounts/applications" class="btn btn-outline">
    <i data-lucide="file-check" style="width:15px;height:15px;vertical-align:middle;margin-left:4px;"></i> طلبات الخصم
</a>
<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'); ?>

<!-- Filters -->
<div class="card" style="margin-bottom:15px;padding:15px;">
    <form method="GET" action="/pricing/regulatory-discounts" style="display:flex;gap:10px;align-items:end;flex-wrap:wrap;">
        <div class="form-group" style="flex:1;min-width:200px;">
            <label class="form-label">بحث</label>
            <input type="text" name="q" value="<?= e($filters['search'] ?? '') ?>" class="form-input" placeholder="اسم الخصم أو رقم المادة...">
        </div>
        <div class="form-group" style="min-width:180px;">
            <label class="form-label">نوع الاستحقاق</label>
            <select name="eligibility_type" class="form-input">
                <option value="">— الكل —</option>
                <option value="cross_branch_member" <?= ($filters['eligibility_type'] ?? '') === 'cross_branch_member' ? 'selected' : '' ?>>عضو فرع آخر</option>
                <option value="government_employee" <?= ($filters['eligibility_type'] ?? '') === 'government_employee' ? 'selected' : '' ?>>موظف حكومي</option>
                <option value="ministry_youth_employee" <?= ($filters['eligibility_type'] ?? '') === 'ministry_youth_employee' ? 'selected' : '' ?>>موظف وزارة الشباب</option>
                <option value="board_of_trustees" <?= ($filters['eligibility_type'] ?? '') === 'board_of_trustees' ? 'selected' : '' ?>>عضو مجلس أمناء</option>
                <option value="club_employee" <?= ($filters['eligibility_type'] ?? '') === 'club_employee' ? 'selected' : '' ?>>موظف النادي</option>
                <option value="group_membership" <?= ($filters['eligibility_type'] ?? '') === 'group_membership' ? 'selected' : '' ?>>عضوية مجمعة</option>
            </select>
        </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="scale" style="width:18px;height:18px;color:#0D7377;"></i>
        <h3 style="margin:0;color:#0D7377;font-size:15px;">خصومات اللائحة (المواد 97-102، 110)</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>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($rows as $row): ?>
                <tr>
                    <td style="font-weight:700;color:#0D7377;">م. <?= e($row['article_number']) ?></td>
                    <td style="font-weight:600;"><?= e($row['name_ar']) ?></td>
                    <td style="font-size:12px;">
                        <?= match($row['eligibility_type']) {
                            'cross_branch_member' => '<span style="background:#EEF2FF;color:#4F46E5;padding:2px 8px;border-radius:4px;">عضو فرع آخر</span>',
                            'government_employee' => '<span style="background:#FEF3C7;color:#92400E;padding:2px 8px;border-radius:4px;">موظف حكومي</span>',
                            'ministry_youth_employee' => '<span style="background:#DBEAFE;color:#1E40AF;padding:2px 8px;border-radius:4px;">وزارة الشباب</span>',
                            'board_of_trustees' => '<span style="background:#FDE68A;color:#92400E;padding:2px 8px;border-radius:4px;">مجلس أمناء</span>',
                            'club_employee' => '<span style="background:#D1FAE5;color:#065F46;padding:2px 8px;border-radius:4px;">موظف النادي</span>',
                            'group_membership' => '<span style="background:#E0E7FF;color:#3730A3;padding:2px 8px;border-radius:4px;">عضوية مجمعة</span>',
                            default => $row['eligibility_type']
                        } ?>
                    </td>
                    <td style="font-weight:700;color:#0D7377;">
                        <?php if ($row['eligibility_type'] === 'group_membership'): ?>
                            متدرج
                        <?php else: ?>
                            <?= e($row['discount_percentage']) ?>%
                            <?php if ($row['max_discount_percentage']): ?>
                                <small style="color:#9CA3AF;">(أقصى <?= e($row['max_discount_percentage']) ?>%)</small>
                            <?php endif; ?>
                        <?php endif; ?>
                    </td>
                    <td style="font-size:12px;">
                        <?php
                        $targetBranch = null;
                        if ($row['target_branch_id']) {
                            foreach ($branches as $b) {
                                if ((int) $b['id'] === (int) $row['target_branch_id']) {
                                    $targetBranch = $b['name_ar'];
                                    break;
                                }
                            }
                        }
                        echo $targetBranch ? e($targetBranch) : '<span style="color:#9CA3AF;">الكل</span>';
                        ?>
                    </td>
                    <td>
                        <?php if ($row['allows_installment']): ?>
                            <span style="color:#059669;font-weight:600;font-size:12px;">
                                <?= $row['installment_max_years'] ?> سنة
                                <?= bccomp($row['installment_interest_rate'], '0', 2) === 0 ? '(بدون فائدة)' : '(' . e($row['installment_interest_rate']) . '%)' ?>
                            </span>
                        <?php else: ?>
                            <span style="color:#9CA3AF;font-size:12px;">—</span>
                        <?php endif; ?>
                    </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/regulatory-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/regulatory-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'] ?? '') ?>&eligibility_type=<?= urlencode($filters['eligibility_type'] ?? '') ?>" 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="scale" 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;">أضف قواعد الخصم حسب اللائحة (مواد 97-102، 110).</p>
        <a href="/pricing/regulatory-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(); ?>
