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

<?php $__template->section('title'); ?>طابور التحصيل — الخزنة الفرعية<?php $__template->endSection(); ?>

<?php $__template->section('content'); ?>

<!-- Session Info Bar -->
<div style="background:#ECFDF5;border:1px solid #A7F3D0;border-radius:8px;padding:10px 20px;margin-bottom:15px;display:flex;justify-content:space-between;align-items:center;">
    <div style="font-size:13px;color:#065F46;">
        <strong>الوردية:</strong> <?= e($currentSession['session_number']) ?>
        &nbsp;|&nbsp;
        <strong>الإجمالي:</strong> <?= money($currentSession['total_collected'] ?? '0.00') ?>
        &nbsp;|&nbsp;
        <strong>الإيصالات:</strong> <?= (int) ($currentSession['total_receipts'] ?? 0) ?>
    </div>
</div>

<!-- Filters -->
<div class="card" style="margin-bottom:15px;">
    <div style="padding:15px 20px;">
        <form method="GET" action="/treasury/queue" style="display:flex;gap:10px;flex-wrap:wrap;align-items:end;">
            <div>
                <label class="form-label" style="font-size:12px;">بحث</label>
                <input type="text" name="search" class="form-input" value="<?= e($filters['search'] ?? '') ?>" placeholder="اسم العضو، رقم الاستمارة...">
            </div>
            <div>
                <label class="form-label" style="font-size:12px;">الحالة</label>
                <select name="status" class="form-select">
                    <option value="">معلق + قيد التنفيذ</option>
                    <option value="pending" <?= ($filters['status'] ?? '') === 'pending' ? 'selected' : '' ?>>معلق</option>
                    <option value="completed" <?= ($filters['status'] ?? '') === 'completed' ? 'selected' : '' ?>>مكتمل</option>
                </select>
            </div>
            <button type="submit" class="btn btn-outline">بحث</button>
            <?php if (!empty($filters['search']) || !empty($filters['status'])): ?>
                <a href="/treasury/queue" class="btn btn-outline" style="color:#DC2626;border-color:#DC2626;">مسح</a>
            <?php endif; ?>
        </form>
    </div>
</div>

<!-- Queue Table -->
<div class="card">
    <?php
    $pendingCount = 0;
    foreach ($requests as $r) {
        if ($r['status'] === 'pending') $pendingCount++;
    }
    ?>
    <?php if ($pendingCount > 0): ?>
        <div style="padding:12px 20px;background:#FEF2F2;border-bottom:1px solid #FECACA;font-size:14px;color:#DC2626;font-weight:600;">
            <?= $pendingCount ?> طلب في الانتظار
        </div>
    <?php endif; ?>

    <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 if (empty($requests)): ?>
                    <tr><td colspan="7" style="text-align:center;padding:40px;color:#9CA3AF;">لا توجد طلبات</td></tr>
                <?php else: ?>
                    <?php foreach ($requests as $r):
                        $statusColor = match($r['status']) {
                            'pending'    => '#F59E0B',
                            'processing' => '#3B82F6',
                            'completed'  => '#059669',
                            default      => '#6B7280',
                        };
                        $statusLabel = match($r['status']) {
                            'pending'    => 'معلق',
                            'processing' => 'قيد التنفيذ',
                            'completed'  => 'مكتمل',
                            default      => $r['status'],
                        };
                        $typeLabel = \App\Modules\Cashier\Services\PaymentRequestService::getPaymentTypeLabel($r['payment_type']);
                        $waitMinutes = (int) ((time() - strtotime($r['created_at'])) / 60);
                    ?>
                    <tr>
                        <td style="font-family:monospace;font-size:12px;"><?= e($r['request_number']) ?></td>
                        <td>
                            <div style="font-weight:600;"><?= e($r['member_name'] ?? $r['description_ar'] ?? '—') ?></div>
                            <div style="font-size:11px;color:#9CA3AF;"><?= e($r['form_number'] ?? '') ?></div>
                        </td>
                        <td><?= e($typeLabel) ?></td>
                        <td style="font-weight:600;"><?= money($r['amount']) ?></td>
                        <td><span style="color:<?= $statusColor ?>;font-weight:600;font-size:12px;"><?= e($statusLabel) ?></span></td>
                        <td style="font-size:12px;<?= $waitMinutes > 30 ? 'color:#DC2626;font-weight:700;' : 'color:#6B7280;' ?>">
                            <?= $waitMinutes ?> د
                        </td>
                        <td>
                            <?php if ($r['status'] === 'pending'): ?>
                                <a href="/treasury/queue/<?= (int) $r['id'] ?>" class="btn btn-primary" style="font-size:12px;padding:4px 12px;">تحصيل</a>
                            <?php endif; ?>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

<script>
setTimeout(function(){ location.reload(); }, 30000);
</script>
<?php $__template->endSection(); ?>
