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

<?php $__template->section('page_actions'); ?>
<a href="/training-groups" class="btn btn-outline"><i data-lucide="arrow-right" style="width:15px;height:15px;vertical-align:middle;margin-left:4px;"></i> المجموعات التدريبية</a>
<?php $__template->endSection(); ?>

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

<?php
$statusLabels = [
    'waiting'   => ['label' => 'في الانتظار', 'color' => '#D97706', 'bg' => '#FEF3C7'],
    'offered'   => ['label' => 'تم العرض',    'color' => '#2563EB', 'bg' => '#DBEAFE'],
    'accepted'  => ['label' => 'مقبول',       'color' => '#059669', 'bg' => '#D1FAE5'],
    'expired'   => ['label' => 'منتهي',       'color' => '#6B7280', 'bg' => '#F3F4F6'],
    'cancelled' => ['label' => 'ملغي',        'color' => '#DC2626', 'bg' => '#FEE2E2'],
];
$currentStatus = $filters['status'] ?? '';
?>

<!-- Status Filter Tabs -->
<div class="card" style="margin-bottom:20px;padding:0;">
    <div style="display:flex;align-items:center;gap:0;overflow-x:auto;border-bottom:2px solid #E5E7EB;">
        <a href="/waiting-list"
           style="padding:12px 20px;font-size:14px;font-weight:600;text-decoration:none;border-bottom:3px solid <?= $currentStatus === '' ? '#0D7377' : 'transparent' ?>;color:<?= $currentStatus === '' ? '#0D7377' : '#6B7280' ?>;white-space:nowrap;">
            الكل
        </a>
        <?php foreach ($statusLabels as $sKey => $sInfo): ?>
        <a href="/waiting-list?status=<?= e($sKey) ?>"
           style="padding:12px 20px;font-size:14px;font-weight:600;text-decoration:none;border-bottom:3px solid <?= $currentStatus === $sKey ? $sInfo['color'] : 'transparent' ?>;color:<?= $currentStatus === $sKey ? $sInfo['color'] : '#6B7280' ?>;white-space:nowrap;">
            <?= e($sInfo['label']) ?>
        </a>
        <?php endforeach; ?>
    </div>
</div>

<!-- Search Bar -->
<div class="card" style="margin-bottom:20px;padding:15px;">
    <form method="GET" action="/waiting-list" style="display:flex;gap:10px;align-items:end;flex-wrap:wrap;">
        <div style="flex:1;min-width:200px;">
            <label class="form-label" style="font-size:12px;">بحث</label>
            <input type="text" name="q" value="<?= e($filters['q'] ?? '') ?>" placeholder="ابحث باسم اللاعب أو الهاتف..." class="form-input" style="min-width:200px;">
        </div>
        <div style="min-width:160px;">
            <label class="form-label" style="font-size:12px;">النشاط الرياضي</label>
            <select name="discipline_id" class="form-input">
                <option value="">الكل</option>
                <?php foreach ($disciplines as $disc): ?>
                <option value="<?= (int) $disc['id'] ?>" <?= ($filters['discipline_id'] ?? '') == $disc['id'] ? 'selected' : '' ?>><?= e($disc['name_ar']) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <?php if ($currentStatus !== ''): ?>
        <input type="hidden" name="status" value="<?= e($currentStatus) ?>">
        <?php endif; ?>
        <button type="submit" class="btn btn-outline"><i data-lucide="search" style="width:16px;height:16px;vertical-align:middle;"></i> بحث</button>
        <a href="/waiting-list" class="btn btn-sm btn-outline" style="color:#6B7280;">مسح</a>
    </form>
</div>

<!-- Entries Table -->
<?php if (!empty($entries)): ?>
<div class="card" style="margin-bottom:20px;overflow-x:auto;">
    <table style="width:100%;font-size:13px;border-collapse:collapse;">
        <thead>
            <tr style="background:#F9FAFB;color:#6B7280;font-size:12px;">
                <th style="padding:10px 12px;text-align:right;border-bottom:1px solid #E5E7EB;">#</th>
                <th style="padding:10px 12px;text-align:right;border-bottom:1px solid #E5E7EB;">اللاعب</th>
                <th style="padding:10px 12px;text-align:right;border-bottom:1px solid #E5E7EB;">النشاط</th>
                <th style="padding:10px 12px;text-align:right;border-bottom:1px solid #E5E7EB;">المستوى</th>
                <th style="padding:10px 12px;text-align:right;border-bottom:1px solid #E5E7EB;">الوقت المفضل</th>
                <th style="padding:10px 12px;text-align:center;border-bottom:1px solid #E5E7EB;">الأولوية</th>
                <th style="padding:10px 12px;text-align:center;border-bottom:1px solid #E5E7EB;">الحالة</th>
                <th style="padding:10px 12px;text-align:center;border-bottom:1px solid #E5E7EB;">إجراءات</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($entries as $i => $entry):
                $entryStatus = $entry['status'] ?? 'waiting';
                $sInfo = $statusLabels[$entryStatus] ?? $statusLabels['waiting'];
                $dayNames = [0=>'الأحد',1=>'الإثنين',2=>'الثلاثاء',3=>'الأربعاء',4=>'الخميس',5=>'الجمعة',6=>'السبت'];
            ?>
            <tr>
                <td style="padding:10px 12px;border-bottom:1px solid #F3F4F6;"><?= $i + 1 ?></td>
                <td style="padding:10px 12px;border-bottom:1px solid #F3F4F6;">
                    <div style="font-weight:600;"><?= e($entry['player_name'] ?? '—') ?></div>
                    <?php if (!empty($entry['player_phone'])): ?>
                    <div style="font-size:11px;color:#9CA3AF;direction:ltr;text-align:right;"><?= e($entry['player_phone']) ?></div>
                    <?php endif; ?>
                </td>
                <td style="padding:10px 12px;border-bottom:1px solid #F3F4F6;"><?= e($entry['discipline_name'] ?? '—') ?></td>
                <td style="padding:10px 12px;border-bottom:1px solid #F3F4F6;"><?= e($entry['level_name'] ?? '—') ?></td>
                <td style="padding:10px 12px;border-bottom:1px solid #F3F4F6;">
                    <?php if ($entry['preferred_day_of_week'] !== null): ?>
                    <?= e($dayNames[(int) $entry['preferred_day_of_week']] ?? '') ?>
                    <?php endif; ?>
                    <?php if (!empty($entry['preferred_time'])): ?>
                    <span style="font-size:11px;color:#6B7280;">(<?= e($entry['preferred_time']) ?>)</span>
                    <?php endif; ?>
                </td>
                <td style="padding:10px 12px;border-bottom:1px solid #F3F4F6;text-align:center;">
                    <span style="display:inline-block;min-width:30px;padding:2px 8px;border-radius:8px;background:#F3F4F6;font-weight:600;"><?= (int) $entry['priority'] ?></span>
                </td>
                <td style="padding:10px 12px;border-bottom:1px solid #F3F4F6;text-align:center;">
                    <span style="padding:3px 10px;border-radius:10px;font-size:11px;font-weight:600;background:<?= $sInfo['bg'] ?>;color:<?= $sInfo['color'] ?>;"><?= e($sInfo['label']) ?></span>
                    <?php if ($entryStatus === 'offered' && !empty($entry['offered_group_name'])): ?>
                    <div style="font-size:10px;color:#6B7280;margin-top:3px;"><?= e($entry['offered_group_name']) ?></div>
                    <?php endif; ?>
                </td>
                <td style="padding:10px 12px;border-bottom:1px solid #F3F4F6;text-align:center;">
                    <div style="display:flex;gap:4px;justify-content:center;">
                        <?php if ($entryStatus === 'waiting'): ?>
                        <!-- Offer -->
                        <form method="POST" action="/waiting-list/<?= (int) $entry['id'] ?>/offer" style="display:inline-flex;gap:4px;">
                            <?= csrf_field() ?>
                            <select name="group_id" class="form-input" style="font-size:11px;padding:3px 6px;min-width:100px;" required>
                                <option value="">مجموعة...</option>
                                <?php foreach ($availableGroups as $ag): ?>
                                <option value="<?= (int) $ag['id'] ?>"><?= e($ag['name_ar']) ?></option>
                                <?php endforeach; ?>
                            </select>
                            <button type="submit" class="btn btn-sm btn-outline" style="color:#059669;font-size:11px;padding:3px 8px;" title="عرض مقعد">
                                <i data-lucide="send" style="width:12px;height:12px;"></i>
                            </button>
                        </form>
                        <!-- Cancel -->
                        <form method="POST" action="/waiting-list/<?= (int) $entry['id'] ?>/cancel" style="display:inline;">
                            <?= csrf_field() ?>
                            <button type="submit" class="btn btn-sm btn-outline" style="color:#DC2626;font-size:11px;padding:3px 8px;" title="إلغاء" onclick="return confirm('هل أنت متأكد؟');">
                                <i data-lucide="x" style="width:12px;height:12px;"></i>
                            </button>
                        </form>
                        <?php elseif ($entryStatus === 'offered'): ?>
                        <form method="POST" action="/waiting-list/<?= (int) $entry['id'] ?>/cancel" style="display:inline;">
                            <?= csrf_field() ?>
                            <button type="submit" class="btn btn-sm btn-outline" style="color:#DC2626;font-size:11px;padding:3px 8px;" title="إلغاء العرض" onclick="return confirm('هل أنت متأكد؟');">
                                <i data-lucide="x" style="width:12px;height:12px;"></i> إلغاء
                            </button>
                        </form>
                        <?php endif; ?>
                    </div>
                </td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
</div>

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

<?php else: ?>
<div class="card" style="padding:60px 20px;text-align:center;">
    <div style="margin-bottom:15px;">
        <i data-lucide="clock" style="width:48px;height:48px;color:#D1D5DB;"></i>
    </div>
    <h3 style="color:#6B7280;margin:0 0 8px;">لا توجد طلبات انتظار</h3>
    <p style="color:#9CA3AF;font-size:14px;margin:0;">
        <?php if (!empty($filters['q']) || !empty($filters['status']) || !empty($filters['discipline_id'])): ?>
            لا توجد نتائج مطابقة. جرب تغيير معايير البحث.
        <?php else: ?>
            قائمة الانتظار فارغة حالياً.
        <?php endif; ?>
    </p>
</div>
<?php endif; ?>

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