<?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/documentary-credits/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/documentary-credits" 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="draft" <?= ($status ?? '') === 'draft' ? 'selected' : '' ?>>مسودة</option>
                    <option value="opened" <?= ($status ?? '') === 'opened' ? 'selected' : '' ?>>مفتوح</option>
                    <option value="partially_utilized" <?= ($status ?? '') === 'partially_utilized' ? 'selected' : '' ?>>مستخدم جزئياً</option>
                    <option value="fully_utilized" <?= ($status ?? '') === 'fully_utilized' ? 'selected' : '' ?>>مستخدم بالكامل</option>
                    <option value="expired" <?= ($status ?? '') === 'expired' ? 'selected' : '' ?>>منتهي</option>
                    <option value="cancelled" <?= ($status ?? '') === 'cancelled' ? 'selected' : '' ?>>ملغي</option>
                </select>
            </div>
        </form>
    </div>
</div>

<!-- Credits 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 ($credits as $lc): ?>
                    <?php
                    $statusColor = match($lc['status'] ?? '') {
                        'draft' => '#F59E0B',
                        'opened' => '#059669',
                        'partially_utilized' => '#0284C7',
                        'fully_utilized' => '#6B7280',
                        'expired' => '#DC2626',
                        'cancelled' => '#9CA3AF',
                        default => '#374151',
                    };
                    $statusLabel = match($lc['status'] ?? '') {
                        'draft' => 'مسودة',
                        'opened' => 'مفتوح',
                        'partially_utilized' => 'مستخدم جزئياً',
                        'fully_utilized' => 'مستخدم بالكامل',
                        'expired' => 'منتهي',
                        'cancelled' => 'ملغي',
                        default => $lc['status'] ?? '—',
                    };
                    ?>
                    <tr>
                        <td style="font-weight:600;direction:ltr;text-align:right;"><?= e($lc['credit_number'] ?? '') ?></td>
                        <td><?= e($lc['beneficiary_name'] ?? '—') ?></td>
                        <td><?= e($lc['issuing_bank'] ?? '—') ?></td>
                        <td style="direction:ltr;text-align:right;font-weight:600;"><?= money($lc['amount'] ?? 0) ?></td>
                        <td style="direction:ltr;text-align:right;"><?= e($lc['opening_date'] ?? '—') ?></td>
                        <td style="direction:ltr;text-align:right;"><?= e($lc['expiry_date'] ?? '—') ?></td>
                        <td><span style="color:<?= $statusColor ?>;font-weight:600;"><?= $statusLabel ?></span></td>
                        <td><a href="/accounting/documentary-credits/<?= (int)$lc['id'] ?>" class="btn btn-sm btn-outline">عرض</a></td>
                    </tr>
                <?php endforeach; ?>
                <?php if (empty($credits)): ?>
                    <tr><td colspan="8" style="text-align:center;color:#6B7280;padding:30px;">لا توجد اعتمادات مستندية</td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>
<?php $__template->endSection(); ?>
