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

<?php $__template->section('title'); ?><?= $credit ? 'تعديل اعتماد مستندي' : 'اعتماد مستندي جديد' ?><?php $__template->endSection(); ?>

<?php $__template->section('content'); ?>
<div class="card">
    <div class="card-header"><h3 style="margin:0;font-size:16px;"><?= $credit ? 'تعديل اعتماد مستندي' : 'إنشاء اعتماد مستندي' ?></h3></div>
    <div style="padding:20px;">
        <form method="POST" action="<?= $credit ? '/accounting/documentary-credits/' . (int)$credit['id'] : '/accounting/documentary-credits' ?>">
            <?= 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="credit_number" value="<?= e($credit['credit_number'] ?? '') ?>" class="form-control" required>
                </div>
                <div>
                    <label class="form-label">تاريخ الفتح</label>
                    <input type="date" name="opening_date" value="<?= e($credit['opening_date'] ?? date('Y-m-d')) ?>" class="form-control" required>
                </div>
            </div>

            <div style="display:grid;grid-template-columns:1fr 1fr;gap:15px;margin-bottom:15px;">
                <div>
                    <label class="form-label">المورد</label>
                    <select name="supplier_id" class="form-select" required>
                        <option value="">— اختر مورد —</option>
                        <?php foreach ($suppliers as $s): ?>
                            <option value="<?= (int)$s['id'] ?>" <?= ((int)($credit['supplier_id'] ?? 0)) === (int)$s['id'] ? 'selected' : '' ?>><?= e($s['name_ar']) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <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)($credit['bank_account_id'] ?? 0)) === (int)$ba['id'] ? 'selected' : '' ?>><?= e($ba['account_name_ar']) ?></option>
                        <?php endforeach; ?>
                    </select>
                </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($credit['amount'] ?? '') ?>" class="form-control" required>
                </div>
                <div>
                    <label class="form-label">العملة</label>
                    <input type="text" name="currency" value="<?= e($credit['currency'] ?? 'EGP') ?>" class="form-control">
                </div>
                <div>
                    <label class="form-label">تاريخ الانتهاء</label>
                    <input type="date" name="expiry_date" value="<?= e($credit['expiry_date'] ?? '') ?>" class="form-control">
                </div>
            </div>

            <div style="margin-bottom:15px;">
                <label class="form-label">الوصف</label>
                <textarea name="description" class="form-control" rows="3"><?= e($credit['description'] ?? '') ?></textarea>
            </div>

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