<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>سلفة #<?= (int) $loan['id'] ?><?php $__template->endSection(); ?>
<?php $__template->section('page_actions'); ?><a href="/hr/loans" class="btn btn-secondary">رجوع</a><?php $__template->endSection(); ?>
<?php $__template->section('content'); ?>
<?php $sl = ['pending'=>'معلق','approved'=>'معتمد','rejected'=>'مرفوض','disbursed'=>'مصروف','repaying'=>'جاري السداد','settled'=>'مسدد']; ?>
<?php $sc = ['pending'=>'#FEF3C7;color:#92400E','approved'=>'#DBEAFE;color:#1E40AF','rejected'=>'#FDE8E8;color:#9B1C1C','disbursed'=>'#E0E7FF;color:#3730A3','repaying'=>'#D1FAE5;color:#065F46','settled'=>'#E5E7EB;color:#374151']; ?>
<?php $typeLabels = ['salary_advance'=>'سلفة راتب','personal_loan'=>'قرض شخصي','emergency_loan'=>'قرض طوارئ']; ?>

<div class="card" style="margin-bottom:20px;">
    <div style="padding:16px;border-bottom:1px solid #E5E7EB;"><h3 style="margin:0;font-size:16px;display:flex;align-items:center;gap:8px;">تفاصيل السلفة <span style="display:inline-block;padding:2px 8px;border-radius:9999px;font-size:12px;background:<?= $sc[$loan['status']] ?? '#E5E7EB;color:#374151' ?>;"><?= e($sl[$loan['status']] ?? $loan['status']) ?></span></h3></div>
    <div style="padding:16px;display:grid;grid-template-columns:repeat(auto-fit,minmax(180px,1fr));gap:16px;">
        <div><span style="color:#6B7280;font-size:13px;">الموظف</span><div style="font-weight:600;"><?= e($loan['first_name_ar'] . ' ' . $loan['last_name_ar']) ?></div></div>
        <div><span style="color:#6B7280;font-size:13px;">النوع</span><div><?= e($typeLabels[$loan['loan_type']] ?? $loan['loan_type']) ?></div></div>
        <div><span style="color:#6B7280;font-size:13px;">المبلغ</span><div style="font-weight:700;font-size:18px;"><?= number_format((float) $loan['loan_amount'], 2) ?> ج.م</div></div>
        <div><span style="color:#6B7280;font-size:13px;">المتبقي</span><div style="font-weight:700;color:#DC2626;"><?= number_format((float) $loan['remaining_amount'], 2) ?> ج.م</div></div>
        <div><span style="color:#6B7280;font-size:13px;">قيمة القسط</span><div><?= number_format((float) $loan['installment_amount'], 2) ?></div></div>
        <div><span style="color:#6B7280;font-size:13px;">الأقساط</span><div><?= (int) $loan['paid_installments'] ?> / <?= (int) $loan['number_of_installments'] ?></div></div>
        <div><span style="color:#6B7280;font-size:13px;">تاريخ الطلب</span><div><?= e($loan['request_date']) ?></div></div>
        <div><span style="color:#6B7280;font-size:13px;">بداية الخصم</span><div><?= e($loan['start_deduction_date'] ?? '-') ?></div></div>
        <?php if ($approver): ?><div><span style="color:#6B7280;font-size:13px;">معتمد بواسطة</span><div><?= e($approver['name']) ?></div></div><?php endif; ?>
    </div>
</div>

<?php if (!empty($installments)): ?>
<div class="card" style="margin-bottom:20px;">
    <div style="padding:16px;border-bottom:1px solid #E5E7EB;"><h3 style="margin:0;font-size:16px;">جدول الأقساط</h3></div>
    <div class="table-responsive">
        <table class="data-table">
            <thead><tr><th>#</th><th>تاريخ الاستحقاق</th><th>المبلغ</th><th>المدفوع</th><th>الحالة</th></tr></thead>
            <tbody>
                <?php $instSl = ['pending'=>'معلق','deducted'=>'مخصوم','skipped'=>'تم تخطيه','cancelled'=>'ملغي']; ?>
                <?php $instSc = ['pending'=>'#FEF3C7;color:#92400E','deducted'=>'#DEF7EC;color:#03543F','skipped'=>'#DBEAFE;color:#1E40AF','cancelled'=>'#E5E7EB;color:#374151']; ?>
                <?php foreach ($installments as $inst): ?>
                <tr>
                    <td><?= (int) $inst['installment_number'] ?></td>
                    <td><?= e($inst['due_date']) ?></td>
                    <td><?= number_format((float) $inst['amount'], 2) ?></td>
                    <td><?= $inst['deducted_at'] ? e(date('Y-m-d', strtotime($inst['deducted_at']))) : '-' ?></td>
                    <td><span style="display:inline-block;padding:2px 8px;border-radius:9999px;font-size:12px;background:<?= $instSc[$inst['status']] ?? '#E5E7EB;color:#374151' ?>;"><?= e($instSl[$inst['status']] ?? $inst['status']) ?></span></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>
<?php endif; ?>

<div style="display:flex;gap:10px;">
    <?php if ($loan['status'] === 'pending'): ?>
    <form method="POST" action="/hr/loans/<?= (int) $loan['id'] ?>/approve"><?= csrf_field() ?><button type="submit" class="btn btn-primary">اعتماد</button></form>
    <form method="POST" action="/hr/loans/<?= (int) $loan['id'] ?>/reject" style="display:flex;gap:8px;">
        <?= csrf_field() ?>
        <input type="text" name="rejection_reason" placeholder="سبب الرفض..." class="form-control" style="min-width:200px;">
        <button type="submit" class="btn btn-danger">رفض</button>
    </form>
    <?php endif; ?>
    <?php if ($loan['status'] === 'approved'): ?>
    <form method="POST" action="/hr/loans/<?= (int) $loan['id'] ?>/disburse"><?= csrf_field() ?><button type="submit" class="btn btn-primary">صرف السلفة</button></form>
    <?php endif; ?>
</div>
<?php $__template->endSection(); ?>
