<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>طلب سلفة جديد<?php $__template->endSection(); ?>
<?php $__template->section('page_actions'); ?><a href="/hr/loans" class="btn btn-secondary">رجوع</a><?php $__template->endSection(); ?>
<?php $__template->section('content'); ?>
<form method="POST" action="/hr/loans">
    <?= csrf_field() ?>
    <div class="card" style="margin-bottom:20px;">
        <div style="padding:16px;display:grid;grid-template-columns:repeat(auto-fit,minmax(250px,1fr));gap:16px;">
            <div><label style="display:block;margin-bottom:4px;font-size:13px;">الموظف <span style="color:#DC2626;">*</span></label>
                <select name="employee_profile_id" class="form-control" required><option value="">-- اختر --</option>
                <?php foreach ($employees as $emp): ?><option value="<?= (int) $emp['id'] ?>" <?= old('employee_profile_id') == $emp['id'] ? 'selected' : '' ?>><?= e($emp['first_name_ar'] . ' ' . $emp['last_name_ar']) ?></option><?php endforeach; ?>
                </select>
            </div>
            <div><label style="display:block;margin-bottom:4px;font-size:13px;">نوع السلفة <span style="color:#DC2626;">*</span></label>
                <select name="loan_type" id="loan_type_select" class="form-control" required>
                    <option value="salary_advance" <?= old('loan_type') === 'salary_advance' ? 'selected' : '' ?>>سلفة راتب</option>
                    <option value="personal_loan" <?= old('loan_type') === 'personal_loan' ? 'selected' : '' ?>>قرض شخصي</option>
                    <option value="emergency_loan" <?= old('loan_type') === 'emergency_loan' ? 'selected' : '' ?>>قرض طوارئ</option>
                    <option value="financing_company" <?= old('loan_type') === 'financing_company' ? 'selected' : '' ?>>شركة تمويل</option>
                    <option value="bank_loan" <?= old('loan_type') === 'bank_loan' ? 'selected' : '' ?>>قرض بنكي</option>
                </select>
            </div>
            <div><label style="display:block;margin-bottom:4px;font-size:13px;">المبلغ <span style="color:#DC2626;">*</span></label><input type="number" name="loan_amount" value="<?= e(old('loan_amount') ?? '') ?>" class="form-control" step="0.01" min="0.01" placeholder="0.00" required></div>
            <div><label style="display:block;margin-bottom:4px;font-size:13px;">عدد الأقساط <span style="color:#DC2626;">*</span></label><input type="number" name="number_of_installments" value="<?= e(old('number_of_installments') ?? '1') ?>" class="form-control" min="1" max="60" required></div>
            <div><label style="display:block;margin-bottom:4px;font-size:13px;">تاريخ الطلب</label><input type="date" name="request_date" value="<?= e(old('request_date') ?? date('Y-m-d')) ?>" class="form-control"></div>
            <div><label style="display:block;margin-bottom:4px;font-size:13px;">بداية الخصم <span style="color:#DC2626;">*</span></label><input type="date" name="start_deduction_date" value="<?= e(old('start_deduction_date') ?? date('Y-m-01', strtotime('+1 month'))) ?>" class="form-control" required></div>
            <div id="external_fields" style="grid-column:1/-1;display:none;">
                <div style="display:grid;grid-template-columns:repeat(auto-fit,minmax(250px,1fr));gap:16px;padding-top:8px;border-top:1px dashed #E5E7EB;">
                    <div><label style="display:block;margin-bottom:4px;font-size:13px;">اسم الجهة</label><input type="text" name="external_entity_name" value="<?= e(old('external_entity_name') ?? '') ?>" class="form-control" placeholder="اسم البنك أو شركة التمويل"></div>
                    <div><label style="display:block;margin-bottom:4px;font-size:13px;">رقم المرجع</label><input type="text" name="external_reference" value="<?= e(old('external_reference') ?? '') ?>" class="form-control" placeholder="رقم العقد أو المرجع"></div>
                    <div><label style="display:block;margin-bottom:4px;font-size:13px;">المبلغ الشهري المستقطع من الراتب</label><input type="number" name="monthly_salary_impact" value="<?= e(old('monthly_salary_impact') ?? '') ?>" class="form-control" step="0.01" min="0" placeholder="0.00"></div>
                </div>
            </div>
            <div style="grid-column:1/-1;"><label style="display:block;margin-bottom:4px;font-size:13px;">السبب</label><textarea name="reason" class="form-control" rows="2"><?= e(old('reason') ?? '') ?></textarea></div>
            <div style="grid-column:1/-1;"><label style="display:block;margin-bottom:4px;font-size:13px;">ملاحظات</label><textarea name="notes" class="form-control" rows="2"><?= e(old('notes') ?? '') ?></textarea></div>
        </div>
    </div>
    <div style="display:flex;gap:10px;justify-content:flex-end;">
        <a href="/hr/loans" class="btn btn-secondary">إلغاء</a>
        <button type="submit" class="btn btn-primary">تقديم الطلب</button>
    </div>
</form>
<script>
(function(){
    var sel = document.getElementById('loan_type_select');
    var extFields = document.getElementById('external_fields');
    function toggle(){
        var v = sel.value;
        extFields.style.display = (v === 'financing_company' || v === 'bank_loan') ? '' : 'none';
    }
    sel.addEventListener('change', toggle);
    toggle();
})();
</script>
<?php $__template->endSection(); ?>
