<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>قائمة الانتظار<?php $__template->endSection(); ?>

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

<!-- Filters -->
<div class="card" style="margin-bottom:20px;padding:15px;">
    <form method="GET" action="/sa/waitlist" style="display:flex;gap:10px;align-items:end;flex-wrap:wrap;">
        <div style="min-width:180px;">
            <label class="form-label" style="font-size:12px;">المجموعة</label>
            <select name="group_id" class="form-select">
                <option value="">-- الكل --</option>
                <?php foreach ($groups as $g): ?>
                <option value="<?= (int) $g['id'] ?>" <?= ($filters['group_id'] ?? '') === (string) $g['id'] ? 'selected' : '' ?>><?= e($g['name_ar']) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div style="min-width:150px;">
            <label class="form-label" style="font-size:12px;">الحالة</label>
            <select name="status" class="form-select">
                <option value="">-- الكل --</option>
                <?php foreach ($statusOptions as $key => $label): ?>
                <option value="<?= e($key) ?>" <?= ($filters['status'] ?? '') === $key ? 'selected' : '' ?>><?= e($label) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <button type="submit" class="btn btn-outline"><i data-lucide="search" style="width:16px;height:16px;vertical-align:middle;"></i> بحث</button>
        <a href="/sa/waitlist" class="btn btn-sm btn-outline" style="color:#6B7280;">مسح</a>
    </form>
</div>

<!-- Waitlist Table -->
<div class="card">
    <div class="table-responsive">
        <table class="data-table">
            <thead>
                <tr>
                    <th>الترتيب</th>
                    <th>اللاعب</th>
                    <th>المجموعة</th>
                    <th>الحالة</th>
                    <th>تاريخ التسجيل</th>
                    <th>إجراءات</th>
                </tr>
            </thead>
            <tbody>
                <?php if (!empty($entries)): ?>
                <?php foreach ($entries as $entry): ?>
                <tr>
                    <td>
                        <span style="background:#F3F4F6;padding:4px 10px;border-radius:50%;font-weight:700;font-size:13px;">
                            <?= (int) ($entry['position'] ?? 0) ?>
                        </span>
                    </td>
                    <td>
                        <div style="font-weight:600;"><?= e($entry['player_name'] ?? '—') ?></div>
                        <?php if (!empty($entry['player_code'])): ?>
                        <div style="font-size:11px;color:#9CA3AF;"><?= e($entry['player_code']) ?></div>
                        <?php endif; ?>
                    </td>
                    <td><?= e($entry['group_name'] ?? '—') ?></td>
                    <td>
                        <?php
                        $wStatusColors = [
                            'waiting'   => 'background:#FEF3C7;color:#92400E;',
                            'offered'   => 'background:#DBEAFE;color:#1D4ED8;',
                            'accepted'  => 'background:#ECFDF5;color:#059669;',
                            'expired'   => 'background:#F3F4F6;color:#6B7280;',
                            'cancelled' => 'background:#FEE2E2;color:#DC2626;',
                        ];
                        $wStyle = $wStatusColors[$entry['status'] ?? ''] ?? 'background:#F3F4F6;color:#374151;';
                        ?>
                        <span style="<?= $wStyle ?>padding:3px 10px;border-radius:10px;font-size:12px;font-weight:600;">
                            <?= e($statusOptions[$entry['status'] ?? ''] ?? $entry['status'] ?? '') ?>
                        </span>
                        <?php if (!empty($entry['expires_at']) && $entry['status'] === 'offered'): ?>
                        <div style="font-size:10px;color:#9CA3AF;margin-top:2px;">ينتهي: <?= e($entry['expires_at']) ?></div>
                        <?php endif; ?>
                    </td>
                    <td style="font-size:12px;"><?= e($entry['created_at'] ?? '') ?></td>
                    <td>
                        <div style="display:flex;gap:6px;">
                            <?php if (($entry['status'] ?? '') === 'waiting'): ?>
                            <form method="POST" action="/sa/waitlist/<?= (int) $entry['id'] ?>/offer" style="display:inline;">
                                <?= csrf_field() ?>
                                <button type="submit" class="btn btn-sm btn-primary" style="font-size:12px;padding:4px 10px;" onclick="return confirm('تأكيد تقديم العرض؟');">
                                    <i data-lucide="send" style="width:12px;height:12px;vertical-align:middle;margin-left:3px;"></i> عرض
                                </button>
                            </form>
                            <?php endif; ?>
                            <?php if (in_array($entry['status'] ?? '', ['waiting', 'offered'], true)): ?>
                            <form method="POST" action="/sa/waitlist/<?= (int) $entry['id'] ?>/cancel" style="display:inline;">
                                <?= csrf_field() ?>
                                <button type="submit" class="btn btn-sm btn-outline" style="font-size:12px;padding:4px 10px;color:#DC2626;border-color:#DC2626;" onclick="return confirm('تأكيد الإلغاء؟');">
                                    <i data-lucide="x-circle" style="width:12px;height:12px;vertical-align:middle;margin-left:3px;"></i> إلغاء
                                </button>
                            </form>
                            <?php endif; ?>
                        </div>
                    </td>
                </tr>
                <?php endforeach; ?>
                <?php else: ?>
                <tr>
                    <td colspan="6" style="text-align:center;padding:40px;color:#6B7280;">
                        <i data-lucide="list" style="width:36px;height:36px;color:#D1D5DB;display:block;margin:0 auto 10px;"></i>
                        لا توجد سجلات في قائمة الانتظار
                    </td>
                </tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>

    <?php if (isset($pagination) && ($pagination['last_page'] ?? 1) > 1): ?>
    <div style="padding:15px;">
        <?php $__template->include('Shared.Components.pagination', ['pagination' => $pagination, 'baseUrl' => '/sa/waitlist?' . http_build_query(array_filter($filters))]); ?>
    </div>
    <?php endif; ?>
</div>

<script>
document.addEventListener('DOMContentLoaded', function() {
    if (typeof lucide !== 'undefined') { lucide.createIcons(); }
});
</script>
<?php $__template->endSection(); ?>
