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

<?php $__template->section('title'); ?>استلام التسويات — الخزنة الرئيسية<?php $__template->endSection(); ?>

<?php $__template->section('content'); ?>
<div class="card">
    <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;font-weight:600;">
        تسويات واردة من الخزنة الفرعية
    </div>

    <?php if (empty($settlements)): ?>
        <div style="padding:40px;text-align:center;color:#9CA3AF;">
            لا توجد تسويات معلقة
        </div>
    <?php else: ?>
        <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 ($settlements as $s): ?>
                    <tr>
                        <td style="font-family:monospace;font-size:12px;"><?= e($s['settlement_number']) ?></td>
                        <td><?= e($s['from_treasury_name'] ?? '—') ?></td>
                        <td style="font-weight:700;font-size:16px;color:#059669;"><?= money($s['amount']) ?></td>
                        <td><?= (int) $s['receipt_count'] ?></td>
                        <td style="font-size:12px;"><?= e($s['settled_by_name'] ?? '—') ?></td>
                        <td style="font-size:12px;"><?= e($s['settled_at'] ?? '') ?></td>
                        <td>
                            <div style="display:flex;gap:5px;">
                                <form method="POST" action="/cashier/settlements/<?= (int) $s['id'] ?>/receive" style="display:inline;">
                                    <?= csrf_field() ?>
                                    <button type="submit" class="btn btn-primary" style="font-size:12px;padding:4px 12px;"
                                            onclick="return confirm('تأكيد استلام <?= money($s['amount']) ?>؟')">
                                        استلام
                                    </button>
                                </form>
                                <button type="button" class="btn btn-outline" style="font-size:12px;padding:4px 8px;color:#DC2626;border-color:#DC2626;"
                                        onclick="document.getElementById('reject-<?= (int) $s['id'] ?>').style.display='block'">
                                    رفض
                                </button>
                            </div>
                            <form id="reject-<?= (int) $s['id'] ?>" method="POST" action="/cashier/settlements/<?= (int) $s['id'] ?>/reject" style="display:none;margin-top:8px;">
                                <?= csrf_field() ?>
                                <input type="text" name="reason" class="form-input" placeholder="سبب الرفض..." required style="font-size:12px;margin-bottom:5px;">
                                <button type="submit" class="btn btn-outline" style="font-size:11px;padding:3px 8px;color:#DC2626;border-color:#DC2626;">تأكيد الرفض</button>
                            </form>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    <?php endif; ?>
</div>
<?php $__template->endSection(); ?>
