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

<?php $__template->section('title'); ?>المطابقات البنكية<?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<a href="/accounting/bank-reconciliation/create" class="btn btn-primary">+ مطابقة جديدة</a>
<?php $__template->endSection(); ?>

<?php $__template->section('content'); ?>
<div class="card" style="margin-bottom:15px;">
    <div style="padding:15px 20px;">
        <form method="GET" action="/accounting/bank-reconciliation" style="display:flex;gap:10px;flex-wrap:wrap;align-items:end;">
            <div>
                <label class="form-label" style="font-size:12px;">الحساب البنكي</label>
                <select name="bank_account_id" class="form-select">
                    <option value="">الكل</option>
                    <?php foreach ($bank_accounts as $ba): ?>
                        <option value="<?= (int)$ba['id'] ?>" <?= (int)($filters['bank_account_id'] ?? 0) === (int)$ba['id'] ? 'selected' : '' ?>><?= e($ba['account_name_ar']) ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div>
                <label class="form-label" style="font-size:12px;">الحالة</label>
                <select name="status" class="form-select">
                    <option value="">الكل</option>
                    <option value="draft" <?= ($filters['status'] ?? '') === 'draft' ? 'selected' : '' ?>>مسودة</option>
                    <option value="in_progress" <?= ($filters['status'] ?? '') === 'in_progress' ? 'selected' : '' ?>>جاري العمل</option>
                    <option value="completed" <?= ($filters['status'] ?? '') === 'completed' ? 'selected' : '' ?>>مكتملة</option>
                    <option value="approved" <?= ($filters['status'] ?? '') === 'approved' ? 'selected' : '' ?>>معتمدة</option>
                </select>
            </div>
            <button type="submit" class="btn btn-outline">بحث</button>
        </form>
    </div>
</div>
<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>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($reconciliations as $r): ?>
                    <?php
                    $statusColor = match($r['status']) {
                        'draft' => '#F59E0B',
                        'in_progress' => '#0284C7',
                        'completed' => '#059669',
                        'approved' => '#0D7377',
                        default => '#6B7280',
                    };
                    $statusLabel = match($r['status']) {
                        'draft' => 'مسودة',
                        'in_progress' => 'جاري العمل',
                        'completed' => 'مكتملة',
                        'approved' => 'معتمدة',
                        default => $r['status'],
                    };
                    ?>
                    <tr>
                        <td><?= e($r['bank_account_name'] ?? '—') ?></td>
                        <td><?= e($r['reconciliation_date']) ?></td>
                        <td style="direction:ltr;text-align:right;"><?= money($r['statement_balance']) ?></td>
                        <td style="direction:ltr;text-align:right;"><?= money($r['book_balance']) ?></td>
                        <td style="direction:ltr;text-align:right;font-weight:600;color:<?= bccomp((string)$r['difference'], '0.00', 2) === 0 ? '#059669' : '#DC2626' ?>;"><?= money($r['difference']) ?></td>
                        <td><span style="color:<?= $statusColor ?>;font-weight:600;"><?= $statusLabel ?></span></td>
                        <td><a href="/accounting/bank-reconciliation/<?= (int)$r['id'] ?>" class="btn btn-sm btn-outline">عرض</a></td>
                    </tr>
                <?php endforeach; ?>
                <?php if (empty($reconciliations)): ?>
                    <tr><td colspan="7" style="text-align:center;color:#6B7280;padding:30px;">لا توجد مطابقات</td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>
<?php $__template->endSection(); ?>
