<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>السلف والقروض<?php $__template->endSection(); ?>
<?php $__template->section('page_actions'); ?>
<?php if (can('hr.loan.manage')): ?>
<a href="/hr/loans/create" class="btn btn-primary" style="display:inline-flex;align-items:center;gap:6px;"><i data-lucide="plus" style="width:16px;height:16px;"></i> طلب سلفة</a>
<?php endif; ?>
<?php $__template->endSection(); ?>
<?php $__template->section('content'); ?>
<div class="card" style="margin-bottom:20px;">
    <form method="GET" action="/hr/loans" style="display:flex;gap:10px;flex-wrap:wrap;align-items:end;padding:16px;">
        <div style="flex:1;min-width:160px;"><label style="display:block;margin-bottom:4px;font-size:13px;color:#6B7280;">بحث</label><input type="text" name="q" value="<?= e($filters['q']) ?>" placeholder="اسم الموظف..." class="form-control"></div>
        <div style="min-width:130px;"><label style="display:block;margin-bottom:4px;font-size:13px;color:#6B7280;">الحالة</label>
            <select name="status" class="form-control"><option value="">الكل</option>
            <option value="pending" <?= $filters['status'] === 'pending' ? 'selected' : '' ?>>معلق</option>
            <option value="approved" <?= $filters['status'] === 'approved' ? 'selected' : '' ?>>معتمد</option>
            <option value="disbursed" <?= $filters['status'] === 'disbursed' ? 'selected' : '' ?>>مصروف</option>
            <option value="repaying" <?= $filters['status'] === 'repaying' ? 'selected' : '' ?>>جاري السداد</option>
            <option value="settled" <?= $filters['status'] === 'settled' ? 'selected' : '' ?>>مسدد</option>
            </select>
        </div>
        <button type="submit" class="btn btn-secondary">بحث</button>
    </form>
</div>
<div class="card">
    <?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']; ?>
    <div class="table-responsive">
        <table class="data-table">
            <thead><tr><th>الموظف</th><th>النوع</th><?php if (can('hr.employee.view_salary')): ?><th>المبلغ</th><th>المتبقي</th><?php endif; ?><th>الأقساط</th><th>الحالة</th><th>إجراءات</th></tr></thead>
            <tbody>
                <?php if (empty($loans)): ?>
                <tr><td colspan="<?= can('hr.employee.view_salary') ? '7' : '5' ?>" style="text-align:center;padding:40px;color:#9CA3AF;">لا توجد سلف</td></tr>
                <?php else: ?>
                <?php $typeLabels = ['salary_advance'=>'سلفة راتب','personal_loan'=>'قرض شخصي','emergency_loan'=>'قرض طوارئ']; ?>
                <?php foreach ($loans as $l): ?>
                <tr>
                    <td><a href="/hr/employees/<?= (int) $l['employee_profile_id'] ?>"><?= e(($l['first_name_ar'] ?? '') . ' ' . ($l['last_name_ar'] ?? '')) ?></a></td>
                    <td><?= e($typeLabels[$l['loan_type']] ?? $l['loan_type']) ?></td>
                    <?php if (can('hr.employee.view_salary')): ?>
                    <td style="font-weight:600;"><?= number_format((float) $l['loan_amount'], 2) ?></td>
                    <td><?= number_format((float) $l['remaining_amount'], 2) ?></td>
                    <?php endif; ?>
                    <td><?= (int) $l['paid_installments'] ?>/<?= (int) $l['number_of_installments'] ?></td>
                    <td><span style="display:inline-block;padding:2px 8px;border-radius:9999px;font-size:12px;background:<?= $sc[$l['status']] ?? '#E5E7EB;color:#374151' ?>;"><?= e($sl[$l['status']] ?? $l['status']) ?></span></td>
                    <td><a href="/hr/loans/<?= (int) $l['id'] ?>" style="color:#2563EB;"><i data-lucide="eye" style="width:16px;height:16px;"></i></a></td>
                </tr>
                <?php endforeach; ?>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
    <?php if (!empty($pagination)): ?><?php $__template->include('Shared.Components.pagination', ['pagination' => $pagination]); ?><?php endif; ?>
</div>
<script>if(typeof lucide!=='undefined')lucide.createIcons();</script>
<?php $__template->endSection(); ?>
