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

<?php $__template->section('page_actions'); ?>
<a href="/pricing/regulatory-discounts" 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/applications" style="display:flex;gap:10px;align-items:end;">
        <div class="form-group" style="min-width:180px;">
            <label class="form-label">الحالة</label>
            <select name="status" class="form-input">
                <option value="">— الكل —</option>
                <option value="pending" <?= ($statusFilter ?? '') === 'pending' ? 'selected' : '' ?>>معلق</option>
                <option value="approved" <?= ($statusFilter ?? '') === 'approved' ? 'selected' : '' ?>>معتمد</option>
                <option value="rejected" <?= ($statusFilter ?? '') === 'rejected' ? 'selected' : '' ?>>مرفوض</option>
            </select>
        </div>
        <button type="submit" class="btn btn-primary" style="padding:8px 20px;">
            <i data-lucide="filter" 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="file-check" style="width:18px;height:18px;color:#0D7377;"></i>
        <h3 style="margin:0;color:#0D7377;font-size:15px;">طلبات تطبيق الخصم اللائحي</h3>
        <?php if (!empty($pagination['total'])): ?>
        <span style="margin-right:auto;background:#F3F4F6;padding:2px 10px;border-radius:10px;font-size:12px;color:#6B7280;"><?= $pagination['total'] ?> طلب</span>
        <?php endif; ?>
    </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>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($rows as $row): ?>
                <tr>
                    <td style="color:#9CA3AF;font-size:12px;"><?= (int) $row['id'] ?></td>
                    <td>
                        <div style="font-weight:600;font-size:13px;"><?= e($row['rule_name'] ?? '—') ?></div>
                        <div style="font-size:11px;color:#6B7280;">مادة <?= e($row['article_number'] ?? '') ?></div>
                    </td>
                    <td style="font-size:13px;"><?= e($row['applicant_name'] ?? '—') ?></td>
                    <td style="font-size:13px;direction:ltr;text-align:right;"><?= money($row['original_amount'] ?? 0) ?></td>
                    <td style="font-weight:700;color:#DC2626;font-size:13px;direction:ltr;text-align:right;">
                        -<?= money($row['discount_amount'] ?? 0) ?>
                        <div style="font-size:11px;color:#6B7280;">(<?= e($row['discount_percentage'] ?? 0) ?>%)</div>
                    </td>
                    <td style="font-weight:700;color:#059669;font-size:13px;direction:ltr;text-align:right;"><?= money($row['final_amount'] ?? 0) ?></td>
                    <td>
                        <?= match($row['status']) {
                            'pending'  => '<span style="display:inline-block;padding:3px 10px;border-radius:10px;font-size:12px;font-weight:600;background:#FEF3C7;color:#92400E;">معلق</span>',
                            'approved' => '<span style="display:inline-block;padding:3px 10px;border-radius:10px;font-size:12px;font-weight:600;background:#ECFDF5;color:#059669;">معتمد</span>',
                            'rejected' => '<span style="display:inline-block;padding:3px 10px;border-radius:10px;font-size:12px;font-weight:600;background:#FEF2F2;color:#DC2626;">مرفوض</span>',
                            default    => $row['status']
                        } ?>
                    </td>
                    <td style="font-size:12px;color:#6B7280;"><?= e(substr($row['created_at'] ?? '', 0, 10)) ?></td>
                    <td style="white-space:nowrap;">
                        <?php if ($row['status'] === 'pending'): ?>
                            <form method="POST" action="/pricing/regulatory-discounts/applications/<?= (int) $row['id'] ?>/approve" style="display:inline;">
                                <?= csrf_field() ?>
                                <button type="submit" class="btn btn-sm" style="font-size:11px;padding:3px 8px;background:#ECFDF5;color:#059669;border:1px solid #A7F3D0;">اعتماد</button>
                            </form>
                            <button type="button" class="btn btn-sm" style="font-size:11px;padding:3px 8px;background:#FEF2F2;color:#DC2626;border:1px solid #FECACA;" onclick="rejectApp(<?= (int) $row['id'] ?>)">رفض</button>
                        <?php elseif ($row['status'] === 'rejected' && $row['rejection_reason']): ?>
                            <span title="<?= e($row['rejection_reason']) ?>" style="cursor:help;color:#DC2626;font-size:11px;">سبب الرفض</span>
                        <?php endif; ?>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
    <?php if ($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 ?>&status=<?= urlencode($statusFilter ?? '') ?>" 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="file-check" 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;">تظهر هنا طلبات تطبيق الخصم اللائحي عند تقديمها من شاشة العضويات.</p>
    </div>
    <?php endif; ?>
</div>

<!-- Rejection Modal -->
<div id="reject-modal" style="display:none;position:fixed;top:0;left:0;right:0;bottom:0;background:rgba(0,0,0,0.5);z-index:9999;align-items:center;justify-content:center;">
    <div style="background:#fff;border-radius:12px;padding:25px;max-width:400px;width:90%;">
        <h4 style="margin:0 0 15px;color:#DC2626;">رفض طلب الخصم</h4>
        <form method="POST" id="reject-form">
            <?= csrf_field() ?>
            <div class="form-group" style="margin-bottom:15px;">
                <label class="form-label">سبب الرفض (اختياري)</label>
                <textarea name="rejection_reason" class="form-input" rows="3" placeholder="أدخل سبب الرفض..."></textarea>
            </div>
            <div style="display:flex;gap:10px;">
                <button type="submit" class="btn" style="background:#DC2626;color:#fff;padding:8px 20px;">تأكيد الرفض</button>
                <button type="button" class="btn btn-outline" onclick="closeRejectModal()">إلغاء</button>
            </div>
        </form>
    </div>
</div>

<script>
document.addEventListener('DOMContentLoaded', function() { if (typeof lucide !== 'undefined') lucide.createIcons(); });

function rejectApp(id) {
    document.getElementById('reject-form').action = '/pricing/regulatory-discounts/applications/' + id + '/reject';
    document.getElementById('reject-modal').style.display = 'flex';
}
function closeRejectModal() {
    document.getElementById('reject-modal').style.display = 'none';
}
</script>
<?php $__template->endSection(); ?>
