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

<?php $__template->section('title'); ?>تفاصيل القرض<?php $__template->endSection(); ?>

<?php $__template->section('content'); ?>
<?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'] ?? '—',
};
?>

<!-- Loan Details -->
<div class="card" style="margin-bottom:15px;">
    <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;display:flex;justify-content:space-between;align-items:center;">
        <h3 style="margin:0;">قرض رقم: <?= e($loan['loan_number'] ?? '') ?></h3>
        <span style="color:<?= $statusColor ?>;font-weight:700;font-size:14px;"><?= $statusLabel ?></span>
    </div>
    <div style="padding:20px;">
        <div style="display:grid;grid-template-columns:repeat(4,1fr);gap:15px;">
            <div class="card" style="padding:15px;text-align:center;">
                <div style="font-size:12px;color:#6B7280;">البنك</div>
                <div style="font-size:16px;font-weight:700;"><?= e($loan['bank_name'] ?? '—') ?></div>
            </div>
            <div class="card" style="padding:15px;text-align:center;">
                <div style="font-size:12px;color:#6B7280;">المبلغ الأصلي</div>
                <div style="font-size:18px;font-weight:700;direction:ltr;"><?= money($loan['principal_amount'] ?? 0) ?></div>
            </div>
            <div class="card" style="padding:15px;text-align:center;">
                <div style="font-size:12px;color:#6B7280;">سعر الفائدة</div>
                <div style="font-size:18px;font-weight:700;direction:ltr;"><?= e($loan['interest_rate'] ?? 0) ?>%</div>
            </div>
            <div class="card" style="padding:15px;text-align:center;">
                <div style="font-size:12px;color:#6B7280;">الرصيد القائم</div>
                <div style="font-size:18px;font-weight:700;direction:ltr;color:#DC2626;"><?= money($loan['outstanding_balance'] ?? 0) ?></div>
            </div>
        </div>
        <div style="display:grid;grid-template-columns:repeat(4,1fr);gap:15px;margin-top:15px;">
            <div>
                <span style="font-size:12px;color:#6B7280;">نوع القرض:</span>
                <span style="font-weight:600;"><?= e($loan['loan_type'] ?? '—') ?></span>
            </div>
            <div>
                <span style="font-size:12px;color:#6B7280;">مدة القرض:</span>
                <span style="font-weight:600;"><?= (int)($loan['term_months'] ?? 0) ?> شهر</span>
            </div>
            <div>
                <span style="font-size:12px;color:#6B7280;">تاريخ البداية:</span>
                <span style="font-weight:600;direction:ltr;display:inline-block;"><?= e($loan['start_date'] ?? '—') ?></span>
            </div>
            <div>
                <span style="font-size:12px;color:#6B7280;">الضمان:</span>
                <span style="font-weight:600;"><?= e($loan['collateral'] ?? '—') ?></span>
            </div>
        </div>
    </div>
</div>

<!-- Amortization Schedule -->
<div class="card">
    <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;">
        <h3 style="margin:0;">جدول السداد</h3>
    </div>
    <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 ($schedule as $inst): ?>
                    <?php
                    $instStatusColor = match($inst['status'] ?? '') {
                        'paid' => '#059669',
                        'due' => '#D97706',
                        'overdue' => '#DC2626',
                        'upcoming' => '#6B7280',
                        default => '#374151',
                    };
                    $instStatusLabel = match($inst['status'] ?? '') {
                        'paid' => 'مسدد',
                        'due' => 'مستحق',
                        'overdue' => 'متأخر',
                        'upcoming' => 'قادم',
                        default => $inst['status'] ?? '—',
                    };
                    ?>
                    <tr>
                        <td style="text-align:center;"><?= (int)($inst['installment_number'] ?? 0) ?></td>
                        <td style="direction:ltr;text-align:right;"><?= e($inst['due_date'] ?? '—') ?></td>
                        <td style="direction:ltr;text-align:right;"><?= money($inst['principal'] ?? 0) ?></td>
                        <td style="direction:ltr;text-align:right;"><?= money($inst['interest'] ?? 0) ?></td>
                        <td style="direction:ltr;text-align:right;font-weight:600;"><?= money($inst['total'] ?? 0) ?></td>
                        <td><span style="color:<?= $instStatusColor ?>;font-weight:600;"><?= $instStatusLabel ?></span></td>
                        <td style="direction:ltr;text-align:right;"><?= e($inst['paid_date'] ?? '—') ?></td>
                    </tr>
                <?php endforeach; ?>
                <?php if (empty($schedule)): ?>
                    <tr><td colspan="7" style="text-align:center;color:#6B7280;padding:30px;">لا يوجد جدول سداد</td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

<div style="margin-top:15px;">
    <a href="/accounting/loans" class="btn btn-outline">رجوع للقائمة</a>
</div>
<?php $__template->endSection(); ?>
