<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>إدارة الأقساط<?php $__template->endSection(); ?>
<?php $__template->section('page_actions'); ?>
<div style="padding:8px 16px;background:#EFF6FF;border:1px solid #BFDBFE;border-radius:6px;font-size:13px;color:#0284C7;">
    💡 لإنشاء خطة تقسيط جديدة، اذهب لصفحة العضو واختر "📅 خطة تقسيط"
</div>
<?php $__template->endSection(); ?>
<?php $__template->section('content'); ?>
<div class="card" style="margin-bottom:20px;padding:15px;">
    <form method="GET" action="/installments" style="display:flex;gap:10px;flex-wrap:wrap;align-items:end;">
        <div><label class="form-label" style="font-size:12px;">بحث</label><input type="text" name="q" value="<?= e($filters['search'] ?? '') ?>" placeholder="اسم العضو، رقم العضوية..." class="form-input" style="min-width:200px;"></div>
        <div><label class="form-label" style="font-size:12px;">الحالة</label>
            <select name="status" class="form-select"><option value="">الكل</option><option value="active" <?= ($filters['status'] ?? '') === 'active' ? 'selected' : '' ?>>نشط</option><option value="completed" <?= ($filters['status'] ?? '') === 'completed' ? 'selected' : '' ?>>مكتمل</option><option value="cancelled" <?= ($filters['status'] ?? '') === 'cancelled' ? 'selected' : '' ?>>ملغى</option></select></div>
        <div><label style="display:flex;align-items:center;gap:5px;font-size:13px;cursor:pointer;margin-top:20px;"><input type="checkbox" name="overdue_only" value="1" <?= ($filters['overdue_only'] ?? '') ? 'checked' : '' ?>> متأخرة فقط</label></div>
        <button type="submit" class="btn btn-outline">بحث</button>
        <a href="/installments" class="btn btn-sm btn-outline" style="color:#6B7280;">مسح</a>
    </form>
</div>
<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><th>الحالة</th><th>الإجراءات</th></tr></thead><tbody>
    <?php foreach ($rows as $r): ?>
    <tr>
        <td><?= (int) $r['id'] ?></td>
        <td><a href="/members/<?= (int) $r['member_id'] ?>" style="color:#1e3a5f;font-weight:600;"><?= e($r['member_name'] ?? '') ?></a><br><small style="color:#9CA3AF;"><?= e($r['membership_number'] ?? '') ?></small></td>
        <td style="font-weight:600;"><?= money($r['total_with_interest']) ?></td>
        <td><?= money($r['down_payment']) ?></td>
        <td><?= (int) $r['number_of_months'] ?> شهر</td>
        <td style="color:#059669;"><?= money($r['total_paid_installments'] ?? '0') ?></td>
        <td><?php if (($r['overdue_count'] ?? 0) > 0): ?><span style="color:#DC2626;font-weight:600;"><?= (int) $r['overdue_count'] ?> قسط</span><?php else: ?>—<?php endif; ?></td>
        <td><span style="color:<?= $r['status'] === 'active' ? '#059669' : ($r['status'] === 'completed' ? '#0284C7' : '#6B7280') ?>;font-weight:600;">● <?= $r['status'] === 'active' ? 'نشط' : ($r['status'] === 'completed' ? 'مكتمل' : $r['status']) ?></span></td>
        <td><a href="/installments/<?= (int) $r['id'] ?>" class="btn btn-sm btn-outline">عرض</a></td>
    </tr>
    <?php endforeach; ?>
    <?php if (empty($rows)): ?><tr><td colspan="9" style="text-align:center;padding:40px;color:#6B7280;">لا توجد خطط تقسيط</td></tr><?php endif; ?>
    </tbody></table></div>
</div>
<?php $__template->endSection(); ?>