<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>الغرامات والعقوبات<?php $__template->endSection(); ?>
<?php $__template->section('content'); ?>
<div class="card" style="margin-bottom:20px;padding:15px;">
    <form method="GET" action="/fines" 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'] ?? '') ?>" 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="imposed" <?= ($filters['status'] ?? '') === 'imposed' ? 'selected' : '' ?>>مفروضة</option><option value="paid" <?= ($filters['status'] ?? '') === 'paid' ? 'selected' : '' ?>>مدفوعة</option><option value="appealed" <?= ($filters['status'] ?? '') === 'appealed' ? 'selected' : '' ?>>متظلم</option></select></div>
        <button type="submit" class="btn btn-outline">بحث</button>
    </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></tr></thead><tbody>
<?php foreach ($rows as $r): ?>
<tr>
    <td><a href="/members/<?= (int) $r['member_id'] ?>" style="color:#1e3a5f;font-weight:600;"><?= e($r['member_name'] ?? '') ?></a></td>
    <td style="font-weight:600;color:<?= in_array($r['penalty_type'], ['suspension','ban','termination']) ? '#DC2626' : '#D97706' ?>;"><?= e(\App\Modules\Fines\Models\Fine::getPenaltyTypeLabel($r['penalty_type'])) ?></td>
    <td style="font-weight:700;"><?= bccomp($r['amount'] ?? '0', '0', 2) > 0 ? money($r['amount']) : '—' ?></td>
    <td style="color:#059669;"><?= bccomp($r['paid_amount'] ?? '0', '0', 2) > 0 ? money($r['paid_amount']) : '—' ?></td>
    <td style="font-size:12px;"><?= ($r['suspension_from'] && $r['suspension_to']) ? e($r['suspension_from']) . ' → ' . e($r['suspension_to']) : '—' ?></td>
    <td><span style="color:<?= match($r['status']) { 'imposed' => '#DC2626', 'paid' => '#059669', 'appealed' => '#D97706', 'cancelled' => '#6B7280', 'waived' => '#0284C7', default => '#6B7280' } ?>;font-weight:600;">● <?= match($r['status']) { 'imposed' => 'مفروضة', 'paid' => 'مدفوعة', 'appealed' => 'متظلم', 'appeal_upheld' => 'تأييد', 'cancelled' => 'ملغاة', 'waived' => 'معفاة', default => $r['status'] } ?></span></td>
    <td>
        <div style="display:flex;gap:5px;flex-wrap:wrap;">
        <?php if (in_array($r['status'], ['imposed', 'appeal_upheld']) && $r['penalty_type'] === 'fine' && bccomp($r['amount'], $r['paid_amount'] ?? '0', 2) > 0 && can('fine.collect')): ?>
            <form method="POST" action="/fines/<?= (int) $r['id'] ?>/pay" style="display:flex;gap:5px;">
                <?= csrf_field() ?>
                <input type="hidden" name="payment_method" value="cash">
                <button type="submit" class="btn btn-sm btn-primary" onclick="return confirm('دفع غرامة <?= money(bcsub($r['amount'], $r['paid_amount'] ?? '0', 2)) ?>؟')">💰 ادفع <?= money(bcsub($r['amount'], $r['paid_amount'] ?? '0', 2)) ?></button>
            </form>
        <?php endif; ?>
        <?php if ($r['status'] === 'imposed' && !$r['appeal_submitted']): ?>
            <a href="/fines/<?= (int) $r['id'] ?>/appeal" class="btn btn-sm btn-outline">تظلم</a>
        <?php endif; ?>
        <?php if ($r['status'] === 'appealed' && can('fine.waive')): ?>
            <form method="POST" action="/fines/<?= (int) $r['id'] ?>/appeal-decide" style="display:flex;gap:3px;">
                <?= csrf_field() ?>
                <button type="submit" name="appeal_decision" value="upheld" class="btn btn-sm" style="background:#DC2626;color:#fff;border:none;padding:3px 8px;border-radius:4px;cursor:pointer;font-size:11px;">تأييد</button>
                <button type="submit" name="appeal_decision" value="cancelled" class="btn btn-sm" style="background:#059669;color:#fff;border:none;padding:3px 8px;border-radius:4px;cursor:pointer;font-size:11px;">إلغاء</button>
            </form>
        <?php endif; ?>
        </div>
    </td>
</tr>
<?php endforeach; ?>
<?php if (empty($rows)): ?><tr><td colspan="7" style="text-align:center;padding:40px;color:#6B7280;">لا توجد غرامات</td></tr><?php endif; ?>
</tbody></table></div></div>
<?php $__template->endSection(); ?>