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

<?php $__template->section('title'); ?><?= !empty($loan['id']) ? 'تعديل قرض' : 'قرض جديد' ?><?php $__template->endSection(); ?>

<?php $__template->section('content'); ?>
<div class="card">
    <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;">
        <h3 style="margin:0;"><?= !empty($loan['id']) ? 'تعديل قرض' : 'إضافة قرض بنكي جديد' ?></h3>
    </div>
    <div style="padding:20px;">
        <form method="POST" action="<?= !empty($loan['id']) ? '/accounting/loans/' . (int)$loan['id'] : '/accounting/loans' ?>">
            <?= csrf_field() ?>
            <div style="display:grid;grid-template-columns:1fr 1fr;gap:15px;">
                <div>
                    <label class="form-label">الحساب البنكي <span style="color:#DC2626;">*</span></label>
                    <select name="bank_account_id" class="form-select" required>
                        <option value="">— اختر حساب بنكي —</option>
                        <?php foreach ($bankAccounts as $ba): ?>
                            <option value="<?= (int)$ba['id'] ?>" <?= (int)($loan['bank_account_id'] ?? 0) === (int)$ba['id'] ? 'selected' : '' ?>><?= e($ba['account_name_ar'] ?? $ba['bank_name'] ?? '') ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div>
                    <label class="form-label">نوع القرض <span style="color:#DC2626;">*</span></label>
                    <select name="loan_type" class="form-select" required>
                        <option value="">— اختر —</option>
                        <option value="term_loan" <?= ($loan['loan_type'] ?? '') === 'term_loan' ? 'selected' : '' ?>>قرض لأجل</option>
                        <option value="revolving" <?= ($loan['loan_type'] ?? '') === 'revolving' ? 'selected' : '' ?>>تسهيل ائتماني متجدد</option>
                        <option value="overdraft" <?= ($loan['loan_type'] ?? '') === 'overdraft' ? 'selected' : '' ?>>سحب على المكشوف</option>
                        <option value="mortgage" <?= ($loan['loan_type'] ?? '') === 'mortgage' ? 'selected' : '' ?>>قرض عقاري</option>
                        <option value="equipment" <?= ($loan['loan_type'] ?? '') === 'equipment' ? 'selected' : '' ?>>تمويل معدات</option>
                    </select>
                </div>
                <div>
                    <label class="form-label">المبلغ الأصلي <span style="color:#DC2626;">*</span></label>
                    <input type="number" name="principal_amount" class="form-input" step="0.01" min="0.01" required dir="ltr" value="<?= e($loan['principal_amount'] ?? '') ?>">
                </div>
                <div>
                    <label class="form-label">سعر الفائدة (%) <span style="color:#DC2626;">*</span></label>
                    <input type="number" name="interest_rate" class="form-input" step="0.01" min="0" required dir="ltr" value="<?= e($loan['interest_rate'] ?? '') ?>">
                </div>
                <div>
                    <label class="form-label">مدة القرض (أشهر) <span style="color:#DC2626;">*</span></label>
                    <input type="number" name="term_months" class="form-input" min="1" required dir="ltr" value="<?= e($loan['term_months'] ?? '') ?>">
                </div>
                <div>
                    <label class="form-label">تاريخ البداية <span style="color:#DC2626;">*</span></label>
                    <input type="date" name="start_date" class="form-input" required dir="ltr" value="<?= e($loan['start_date'] ?? date('Y-m-d')) ?>">
                </div>
                <div>
                    <label class="form-label">رقم القرض</label>
                    <input type="text" name="loan_number" class="form-input" dir="ltr" value="<?= e($loan['loan_number'] ?? '') ?>">
                </div>
                <div>
                    <label class="form-label">اسم البنك</label>
                    <input type="text" name="bank_name" class="form-input" value="<?= e($loan['bank_name'] ?? '') ?>">
                </div>
                <div style="grid-column:1/-1;">
                    <label class="form-label">الضمان</label>
                    <textarea name="collateral" class="form-textarea" rows="2"><?= e($loan['collateral'] ?? '') ?></textarea>
                </div>
                <div style="grid-column:1/-1;">
                    <label class="form-label">ملاحظات</label>
                    <textarea name="notes" class="form-textarea" rows="2"><?= e($loan['notes'] ?? '') ?></textarea>
                </div>
            </div>
            <div style="margin-top:20px;display:flex;gap:8px;">
                <button type="submit" class="btn btn-primary">حفظ</button>
                <a href="/accounting/loans" class="btn btn-outline">إلغاء</a>
            </div>
        </form>
    </div>
</div>
<?php $__template->endSection(); ?>
