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

<?php $__template->section('title'); ?><?= $guarantee ? 'تعديل خطاب ضمان' : 'خطاب ضمان جديد' ?><?php $__template->endSection(); ?>

<?php $__template->section('content'); ?>
<div class="card">
    <div class="card-header"><h3 style="margin:0;font-size:16px;"><?= $guarantee ? 'تعديل خطاب ضمان' : 'إنشاء خطاب ضمان' ?></h3></div>
    <div style="padding:20px;">
        <form method="POST" action="<?= $guarantee ? '/accounting/guarantees/' . (int)$guarantee['id'] : '/accounting/guarantees' ?>">
            <?= csrf_field() ?>

            <div style="display:grid;grid-template-columns:1fr 1fr;gap:15px;margin-bottom:15px;">
                <div>
                    <label class="form-label">رقم خطاب الضمان</label>
                    <input type="text" name="guarantee_number" value="<?= e($guarantee['guarantee_number'] ?? '') ?>" class="form-control" required>
                </div>
                <div>
                    <label class="form-label">نوع الضمان</label>
                    <select name="guarantee_type" class="form-select" required>
                        <option value="">— اختر —</option>
                        <option value="initial" <?= ($guarantee['guarantee_type'] ?? '') === 'initial' ? 'selected' : '' ?>>ابتدائي</option>
                        <option value="final" <?= ($guarantee['guarantee_type'] ?? '') === 'final' ? 'selected' : '' ?>>نهائي</option>
                        <option value="advance" <?= ($guarantee['guarantee_type'] ?? '') === 'advance' ? 'selected' : '' ?>>دفعة مقدمة</option>
                        <option value="maintenance" <?= ($guarantee['guarantee_type'] ?? '') === 'maintenance' ? 'selected' : '' ?>>صيانة</option>
                    </select>
                </div>
            </div>

            <div style="display:grid;grid-template-columns:1fr 1fr;gap:15px;margin-bottom:15px;">
                <div>
                    <label class="form-label">البنك</label>
                    <select name="bank_account_id" class="form-select" required>
                        <option value="">— اختر بنك —</option>
                        <?php foreach ($bankAccounts as $ba): ?>
                            <option value="<?= (int)$ba['id'] ?>" <?= ((int)($guarantee['bank_account_id'] ?? 0)) === (int)$ba['id'] ? 'selected' : '' ?>><?= e($ba['account_name_ar']) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div>
                    <label class="form-label">الجهة المستفيدة</label>
                    <input type="text" name="beneficiary" value="<?= e($guarantee['beneficiary'] ?? '') ?>" class="form-control" required>
                </div>
            </div>

            <div style="display:grid;grid-template-columns:1fr 1fr 1fr;gap:15px;margin-bottom:15px;">
                <div>
                    <label class="form-label">المبلغ</label>
                    <input type="number" name="amount" step="0.01" value="<?= e($guarantee['amount'] ?? '') ?>" class="form-control" required>
                </div>
                <div>
                    <label class="form-label">تاريخ الإصدار</label>
                    <input type="date" name="issue_date" value="<?= e($guarantee['issue_date'] ?? date('Y-m-d')) ?>" class="form-control" required>
                </div>
                <div>
                    <label class="form-label">تاريخ الانتهاء</label>
                    <input type="date" name="expiry_date" value="<?= e($guarantee['expiry_date'] ?? '') ?>" class="form-control" required>
                </div>
            </div>

            <div style="margin-bottom:15px;">
                <label class="form-label">ملاحظات</label>
                <textarea name="notes" class="form-control" rows="3"><?= e($guarantee['notes'] ?? '') ?></textarea>
            </div>

            <div style="display:flex;gap:10px;">
                <button type="submit" class="btn btn-primary">حفظ</button>
                <a href="/accounting/guarantees" class="btn btn-outline">إلغاء</a>
            </div>
        </form>
    </div>
</div>
<?php $__template->endSection(); ?>
