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

<?php $__template->section('title'); ?>التسويات<?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<a href="/accounting/settlements/create" class="btn btn-primary">+ تسوية جديدة</a>
<?php $__template->endSection(); ?>

<?php $__template->section('content'); ?>
<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 ($settlements as $s): ?>
                    <?php
                    $statusColor = match($s['status'] ?? '') {
                        'draft' => '#F59E0B',
                        'approved' => '#059669',
                        'posted' => '#0284C7',
                        'rejected' => '#DC2626',
                        default => '#6B7280',
                    };
                    $statusLabel = match($s['status'] ?? '') {
                        'draft' => 'مسودة',
                        'approved' => 'معتمدة',
                        'posted' => 'مرحّلة',
                        'rejected' => 'مرفوضة',
                        default => $s['status'] ?? '—',
                    };
                    $typeLabel = match($s['type'] ?? '') {
                        'debt_settlement' => 'تسوية ديون',
                        'advance_settlement' => 'تسوية سلف',
                        'account_settlement' => 'تسوية حسابات',
                        default => $s['type'] ?? '—',
                    };
                    ?>
                    <tr>
                        <td style="font-weight:600;direction:ltr;text-align:right;"><?= e($s['settlement_number'] ?? '') ?></td>
                        <td style="direction:ltr;text-align:right;"><?= e($s['settlement_date'] ?? $s['created_at'] ?? '—') ?></td>
                        <td><?= $typeLabel ?></td>
                        <td><?= e($s['description'] ?? '—') ?></td>
                        <td style="direction:ltr;text-align:right;font-weight:600;"><?= money($s['amount'] ?? 0) ?></td>
                        <td><span style="color:<?= $statusColor ?>;font-weight:600;"><?= $statusLabel ?></span></td>
                        <td><a href="/accounting/settlements/<?= (int)$s['id'] ?>" class="btn btn-sm btn-outline">عرض</a></td>
                    </tr>
                <?php endforeach; ?>
                <?php if (empty($settlements)): ?>
                    <tr><td colspan="7" style="text-align:center;color:#6B7280;padding:30px;">لا توجد تسويات</td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>
<?php $__template->endSection(); ?>
