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

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

<?php $__template->section('content'); ?>
<?php
$statusLabel = match($recon['status']) {
    'draft' => 'مسودة',
    'in_progress' => 'جاري العمل',
    'completed' => 'مكتملة',
    'approved' => 'معتمدة',
    default => $recon['status'],
};
?>

<!-- Summary -->
<div class="card" style="margin-bottom:15px;">
    <div style="padding:20px;">
        <h2 style="margin:0 0 15px;">مطابقة: <?= e($bank_account['account_name_ar'] ?? '—') ?></h2>
        <div style="display:grid;grid-template-columns:repeat(5,1fr);gap:15px;">
            <div class="card" style="padding:15px;text-align:center;">
                <div style="font-size:12px;color:#6B7280;">رصيد كشف البنك</div>
                <div style="font-size:20px;font-weight:700;direction:ltr;"><?= money($recon['statement_balance']) ?></div>
            </div>
            <div class="card" style="padding:15px;text-align:center;">
                <div style="font-size:12px;color:#6B7280;">رصيد الدفاتر</div>
                <div style="font-size:20px;font-weight:700;direction:ltr;"><?= money($recon['book_balance']) ?></div>
            </div>
            <div class="card" style="padding:15px;text-align:center;">
                <div style="font-size:12px;color:#6B7280;">الرصيد المعدل</div>
                <div style="font-size:20px;font-weight:700;direction:ltr;"><?= money($recon['adjusted_balance']) ?></div>
            </div>
            <div class="card" style="padding:15px;text-align:center;background:<?= bccomp((string)$recon['difference'], '0.00', 2) === 0 ? '#F0FDF4' : '#FEF2F2' ?>;">
                <div style="font-size:12px;color:#6B7280;">الفرق</div>
                <div style="font-size:20px;font-weight:700;color:<?= bccomp((string)$recon['difference'], '0.00', 2) === 0 ? '#059669' : '#DC2626' ?>;direction:ltr;"><?= money($recon['difference']) ?></div>
            </div>
            <div class="card" style="padding:15px;text-align:center;">
                <div style="font-size:12px;color:#6B7280;">الحالة</div>
                <div style="font-size:16px;font-weight:700;"><?= $statusLabel ?></div>
            </div>
        </div>

        <?php if ($recon['status'] !== 'completed' && $recon['status'] !== 'approved' && can('accounting.bank_recon.manage')): ?>
            <div style="margin-top:15px;display:flex;gap:8px;">
                <?php if (bccomp((string)$recon['difference'], '0.00', 2) === 0): ?>
                    <form method="POST" action="/accounting/bank-reconciliation/<?= (int)$recon['id'] ?>/complete" style="display:inline;">
                        <?= csrf_field() ?>
                        <button type="submit" class="btn btn-primary" onclick="return confirm('هل تريد إكمال المطابقة؟')">إكمال المطابقة</button>
                    </form>
                <?php endif; ?>
            </div>
        <?php endif; ?>
    </div>
</div>

<!-- Reconciliation Items -->
<div style="display:grid;grid-template-columns:1fr 1fr;gap:15px;">
    <!-- Items List -->
    <div class="card">
        <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;">
            <h3 style="margin:0;">بنود المطابقة</h3>
        </div>
        <div class="table-responsive">
            <table class="data-table" style="width:100%;">
                <thead>
                    <tr>
                        <th>النوع</th>
                        <th>الوصف</th>
                        <th>المبلغ</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($items as $item): ?>
                        <?php
                        $typeLabel = match($item['item_type']) {
                            'outstanding_check' => 'شيك معلق',
                            'deposit_in_transit' => 'إيداع في الطريق',
                            'bank_charge' => 'مصاريف بنكية',
                            'bank_interest' => 'فوائد بنكية',
                            'book_error' => 'خطأ دفتري',
                            'bank_error' => 'خطأ بنكي',
                            default => $item['item_type'],
                        };
                        ?>
                        <tr>
                            <td style="font-size:12px;"><?= $typeLabel ?></td>
                            <td style="font-size:13px;"><?= e($item['description_ar']) ?></td>
                            <td style="direction:ltr;text-align:right;font-weight:600;"><?= money($item['amount']) ?></td>
                        </tr>
                    <?php endforeach; ?>
                    <?php if (empty($items)): ?>
                        <tr><td colspan="3" style="text-align:center;color:#6B7280;padding:20px;">لا توجد بنود</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>

    <!-- Add Item Form -->
    <?php if ($recon['status'] !== 'completed' && $recon['status'] !== 'approved' && can('accounting.bank_recon.manage')): ?>
        <div class="card">
            <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;">
                <h3 style="margin:0;">إضافة بند</h3>
            </div>
            <div style="padding:20px;">
                <form method="POST" action="/accounting/bank-reconciliation/<?= (int)$recon['id'] ?>/add-item">
                    <?= csrf_field() ?>
                    <div style="display:grid;gap:12px;">
                        <div>
                            <label class="form-label">النوع <span style="color:#DC2626;">*</span></label>
                            <select name="item_type" class="form-select" required>
                                <option value="">— اختر —</option>
                                <option value="outstanding_check">شيك معلق</option>
                                <option value="deposit_in_transit">إيداع في الطريق</option>
                                <option value="bank_charge">مصاريف بنكية</option>
                                <option value="bank_interest">فوائد بنكية</option>
                                <option value="book_error">خطأ دفتري</option>
                                <option value="bank_error">خطأ بنكي</option>
                            </select>
                        </div>
                        <div>
                            <label class="form-label">الوصف <span style="color:#DC2626;">*</span></label>
                            <input type="text" name="description_ar" class="form-input" required>
                        </div>
                        <div>
                            <label class="form-label">المبلغ <span style="color:#DC2626;">*</span></label>
                            <input type="number" name="amount" class="form-input" step="0.01" required dir="ltr">
                        </div>
                        <div>
                            <label class="form-label">رقم المرجع</label>
                            <input type="text" name="reference_number" class="form-input" dir="ltr">
                        </div>
                        <button type="submit" class="btn btn-primary">إضافة</button>
                    </div>
                </form>
            </div>
        </div>
    <?php endif; ?>
</div>
<?php $__template->endSection(); ?>
