<?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;display:flex;justify-content:space-between;align-items:center;">
        <span style="font-weight:600;">سجل التسويات</span>
        <a href="/treasury/settlements/create" class="btn btn-primary" style="font-size:13px;">تسوية جديدة</a>
    </div>
    <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 if (empty($settlements)): ?>
                    <tr><td colspan="8" style="text-align:center;padding:40px;color:#9CA3AF;">لا توجد تسويات</td></tr>
                <?php else: ?>
                    <?php foreach ($settlements as $s):
                        $statusColor = match($s['status']) {
                            'pending'  => '#F59E0B',
                            'received' => '#059669',
                            'rejected' => '#DC2626',
                            default    => '#6B7280',
                        };
                        $statusLabel = match($s['status']) {
                            'pending'  => 'في الانتظار',
                            'received' => 'تم الاستلام',
                            'rejected' => 'مرفوضة',
                            default    => $s['status'],
                        };
                    ?>
                    <tr>
                        <td style="font-family:monospace;font-size:12px;">
                            <a href="/treasury/settlements/<?= (int) $s['id'] ?>" style="color:#3B82F6;"><?= e($s['settlement_number']) ?></a>
                        </td>
                        <td style="font-size:12px;"><?= e($s['session_number'] ?? '—') ?></td>
                        <td style="font-weight:600;"><?= 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['received_by_name'] ?? '—') ?></td>
                        <td><span style="color:<?= $statusColor ?>;font-weight:600;font-size:12px;"><?= e($statusLabel) ?></span></td>
                        <td style="font-size:12px;"><?= e($s['settled_at'] ?? '') ?></td>
                    </tr>
                    <?php endforeach; ?>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>
<?php $__template->endSection(); ?>
