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

<?php $__template->section('title'); ?>خطابات الضمان<?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<?php if (can('accounting.instruments.manage')): ?>
<a href="/accounting/guarantees/create" class="btn btn-primary">+ خطاب ضمان جديد</a>
<?php endif; ?>
<?php $__template->endSection(); ?>

<?php $__template->section('content'); ?>
<!-- Filters -->
<div class="card" style="margin-bottom:15px;">
    <div style="padding:15px 20px;">
        <form method="GET" action="/accounting/guarantees" style="display:flex;gap:10px;flex-wrap:wrap;align-items:end;">
            <div>
                <label class="form-label" style="font-size:12px;">الحالة</label>
                <select name="status" class="form-select" onchange="this.form.submit()">
                    <option value="">الكل</option>
                    <option value="active" <?= ($status ?? '') === 'active' ? 'selected' : '' ?>>نشط</option>
                    <option value="expired" <?= ($status ?? '') === 'expired' ? 'selected' : '' ?>>منتهي</option>
                    <option value="claimed" <?= ($status ?? '') === 'claimed' ? 'selected' : '' ?>>مُطالب به</option>
                    <option value="released" <?= ($status ?? '') === 'released' ? 'selected' : '' ?>>محرر</option>
                    <option value="cancelled" <?= ($status ?? '') === 'cancelled' ? 'selected' : '' ?>>ملغي</option>
                </select>
            </div>
            <div>
                <label class="form-label" style="font-size:12px;">النوع</label>
                <select name="type" class="form-select" onchange="this.form.submit()">
                    <option value="">الكل</option>
                    <option value="bid_bond" <?= ($type ?? '') === 'bid_bond' ? 'selected' : '' ?>>ضمان ابتدائي</option>
                    <option value="performance_bond" <?= ($type ?? '') === 'performance_bond' ? 'selected' : '' ?>>ضمان حسن تنفيذ</option>
                    <option value="advance_payment" <?= ($type ?? '') === 'advance_payment' ? 'selected' : '' ?>>ضمان دفعة مقدمة</option>
                    <option value="retention" <?= ($type ?? '') === 'retention' ? 'selected' : '' ?>>ضمان محتجزات</option>
                    <option value="customs" <?= ($type ?? '') === 'customs' ? 'selected' : '' ?>>ضمان جمركي</option>
                </select>
            </div>
        </form>
    </div>
</div>

<!-- Guarantees Table -->
<div class="card">
    <div class="table-responsive">
        <table class="data-table" style="width:100%;">
            <thead>
                <tr>
                    <th>رقم الخطاب</th>
                    <th>النوع</th>
                    <th>المستفيد</th>
                    <th>البنك المصدر</th>
                    <th>المبلغ</th>
                    <th>تاريخ الانتهاء</th>
                    <th>الحالة</th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($guarantees as $g): ?>
                    <?php
                    $statusColor = match($g['status'] ?? '') {
                        'active' => '#059669',
                        'expired' => '#DC2626',
                        'claimed' => '#D97706',
                        'released' => '#6B7280',
                        'cancelled' => '#9CA3AF',
                        default => '#374151',
                    };
                    $statusLabel = match($g['status'] ?? '') {
                        'active' => 'نشط',
                        'expired' => 'منتهي',
                        'claimed' => 'مُطالب به',
                        'released' => 'محرر',
                        'cancelled' => 'ملغي',
                        default => $g['status'] ?? '—',
                    };
                    $typeLabel = match($g['guarantee_type'] ?? $g['type'] ?? '') {
                        'bid_bond' => 'ضمان ابتدائي',
                        'performance_bond' => 'ضمان حسن تنفيذ',
                        'advance_payment' => 'ضمان دفعة مقدمة',
                        'retention' => 'ضمان محتجزات',
                        'customs' => 'ضمان جمركي',
                        default => $g['guarantee_type'] ?? $g['type'] ?? '—',
                    };
                    ?>
                    <tr>
                        <td style="font-weight:600;direction:ltr;text-align:right;"><?= e($g['guarantee_number'] ?? '') ?></td>
                        <td><?= $typeLabel ?></td>
                        <td><?= e($g['beneficiary_name'] ?? '—') ?></td>
                        <td><?= e($g['issuing_bank'] ?? '—') ?></td>
                        <td style="direction:ltr;text-align:right;font-weight:600;"><?= money($g['amount'] ?? 0) ?></td>
                        <td style="direction:ltr;text-align:right;"><?= e($g['expiry_date'] ?? '—') ?></td>
                        <td><span style="color:<?= $statusColor ?>;font-weight:600;"><?= $statusLabel ?></span></td>
                        <td><a href="/accounting/guarantees/<?= (int)$g['id'] ?>" class="btn btn-sm btn-outline">عرض</a></td>
                    </tr>
                <?php endforeach; ?>
                <?php if (empty($guarantees)): ?>
                    <tr><td colspan="8" style="text-align:center;color:#6B7280;padding:30px;">لا توجد خطابات ضمان</td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>
<?php $__template->endSection(); ?>
