<?php $__template->layout('Layout.main'); ?>

<?php $__template->section('title'); ?>القروض البنكية<?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<a href="/accounting/loans/create" class="btn btn-primary">+ قرض جديد</a>
<?php $__template->endSection(); ?>

<?php $__template->section('content'); ?>
<!-- Filters -->
<div class="card" style="margin-bottom:15px;">
    <div style="padding:15px 20px;">
        <form method="GET" action="/accounting/loans" style="display:flex;gap:10px;flex-wrap:wrap;align-items:end;">
            <div>
                <label class="form-label" style="font-size:12px;">الحالة</label>
                <select name="status" class="form-select" onchange="this.form.submit()">
                    <option value="">الكل</option>
                    <option value="active" <?= ($status ?? '') === 'active' ? 'selected' : '' ?>>نشط</option>
                    <option value="paid_off" <?= ($status ?? '') === 'paid_off' ? 'selected' : '' ?>>مسدد</option>
                    <option value="defaulted" <?= ($status ?? '') === 'defaulted' ? 'selected' : '' ?>>متعثر</option>
                    <option value="restructured" <?= ($status ?? '') === 'restructured' ? 'selected' : '' ?>>مُعاد هيكلته</option>
                </select>
            </div>
        </form>
    </div>
</div>

<!-- Loans Table -->
<div class="card">
    <div class="table-responsive">
        <table class="data-table" style="width:100%;">
            <thead>
                <tr>
                    <th>رقم القرض</th>
                    <th>البنك</th>
                    <th>المبلغ الأصلي</th>
                    <th>سعر الفائدة</th>
                    <th>الرصيد القائم</th>
                    <th>الحالة</th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($loans as $loan): ?>
                    <?php
                    $statusColor = match($loan['status'] ?? '') {
                        'active' => '#059669',
                        'paid_off' => '#6B7280',
                        'defaulted' => '#DC2626',
                        'restructured' => '#D97706',
                        default => '#374151',
                    };
                    $statusLabel = match($loan['status'] ?? '') {
                        'active' => 'نشط',
                        'paid_off' => 'مسدد',
                        'defaulted' => 'متعثر',
                        'restructured' => 'مُعاد هيكلته',
                        default => $loan['status'] ?? '—',
                    };
                    ?>
                    <tr>
                        <td style="font-weight:600;direction:ltr;text-align:right;"><?= e($loan['loan_number'] ?? '') ?></td>
                        <td><?= e($loan['bank_name'] ?? '—') ?></td>
                        <td style="direction:ltr;text-align:right;"><?= money($loan['principal_amount'] ?? 0) ?></td>
                        <td style="direction:ltr;text-align:right;"><?= e($loan['interest_rate'] ?? 0) ?>%</td>
                        <td style="direction:ltr;text-align:right;font-weight:600;"><?= money($loan['outstanding_balance'] ?? 0) ?></td>
                        <td><span style="color:<?= $statusColor ?>;font-weight:600;"><?= $statusLabel ?></span></td>
                        <td><a href="/accounting/loans/<?= (int)$loan['id'] ?>" class="btn btn-sm btn-outline">عرض</a></td>
                    </tr>
                <?php endforeach; ?>
                <?php if (empty($loans)): ?>
                    <tr><td colspan="7" style="text-align:center;color:#6B7280;padding:30px;">لا توجد قروض</td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>
<?php $__template->endSection(); ?>
