<?php
use App\Modules\Tournaments\Models\Tournament;
$__template->layout('Layout.main');
?>
<?php $__template->section('title'); ?>البطولات والمسابقات<?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<a href="/tournaments/create" class="btn btn-primary"><i data-lucide="plus" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> إنشاء بطولة</a>
<?php $__template->endSection(); ?>

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

<?php
$statuses = Tournament::getStatuses();
$types = Tournament::getTypes();
$formats = Tournament::getFormats();
?>

<!-- Filter Bar -->
<div class="card" style="margin-bottom:20px;padding:15px;">
    <form method="GET" action="/tournaments" style="display:flex;gap:10px;align-items:end;flex-wrap:wrap;">
        <div style="flex:1;min-width:180px;">
            <label class="form-label" style="font-size:12px;">بحث</label>
            <input type="text" name="q" value="<?= e($filters['q'] ?? '') ?>" placeholder="اسم البطولة..." class="form-input">
        </div>
        <div style="min-width:140px;">
            <label class="form-label" style="font-size:12px;">الرياضة</label>
            <select name="discipline_id" class="form-input">
                <option value="">الكل</option>
                <?php foreach ($disciplines as $d): ?>
                <option value="<?= (int) $d['id'] ?>" <?= ($filters['discipline_id'] ?? '') == $d['id'] ? 'selected' : '' ?>><?= e($d['name_ar']) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div style="min-width:130px;">
            <label class="form-label" style="font-size:12px;">الحالة</label>
            <select name="status" class="form-input">
                <option value="">الكل</option>
                <?php foreach ($statuses as $key => $label): ?>
                <option value="<?= e($key) ?>" <?= ($filters['status'] ?? '') === $key ? 'selected' : '' ?>><?= e($label) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div style="min-width:130px;">
            <label class="form-label" style="font-size:12px;">النوع</label>
            <select name="tournament_type" class="form-input">
                <option value="">الكل</option>
                <?php foreach ($types as $key => $label): ?>
                <option value="<?= e($key) ?>" <?= ($filters['tournament_type'] ?? '') === $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="/tournaments" class="btn btn-sm btn-outline" style="color:#6B7280;">مسح</a>
    </form>
</div>

<!-- Tournaments Table -->
<?php if (!empty($tournaments)): ?>
<div class="card" style="padding:0;overflow-x:auto;">
    <table style="width:100%;border-collapse:collapse;font-size:14px;">
        <thead>
            <tr style="background:#F9FAFB;border-bottom:2px solid #E5E7EB;">
                <th style="padding:12px 15px;text-align:right;font-weight:600;color:#374151;">#</th>
                <th style="padding:12px 15px;text-align:right;font-weight:600;color:#374151;">اسم البطولة</th>
                <th style="padding:12px 15px;text-align:right;font-weight:600;color:#374151;">الرياضة</th>
                <th style="padding:12px 15px;text-align:right;font-weight:600;color:#374151;">النوع</th>
                <th style="padding:12px 15px;text-align:right;font-weight:600;color:#374151;">النظام</th>
                <th style="padding:12px 15px;text-align:right;font-weight:600;color:#374151;">تاريخ البدء</th>
                <th style="padding:12px 15px;text-align:right;font-weight:600;color:#374151;">الحالة</th>
                <th style="padding:12px 15px;text-align:right;font-weight:600;color:#374151;">إجراءات</th>
            </tr>
        </thead>
        <tbody>
            <?php
            $startIndex = (($pagination['current_page'] ?? 1) - 1) * ($pagination['per_page'] ?? 25);
            foreach ($tournaments as $i => $t):
                $status = $t['status'] ?? 'draft';
                $statusLabel = $statuses[$status] ?? $status;
                $statusColor = Tournament::getStatusColor($status);
                $typeLabel = $types[$t['tournament_type'] ?? ''] ?? ($t['tournament_type'] ?? '');
                $formatLabel = $formats[$t['format'] ?? ''] ?? ($t['format'] ?? '');
            ?>
            <tr style="border-bottom:1px solid #F3F4F6;" onmouseover="this.style.background='#F9FAFB'" onmouseout="this.style.background='transparent'">
                <td style="padding:12px 15px;color:#6B7280;"><?= $startIndex + $i + 1 ?></td>
                <td style="padding:12px 15px;">
                    <div style="font-weight:600;color:#1A1A2E;"><?= e($t['name_ar'] ?? '') ?></div>
                    <?php if (!empty($t['age_group'])): ?>
                    <div style="font-size:12px;color:#9CA3AF;">الفئة: <?= e($t['age_group']) ?></div>
                    <?php endif; ?>
                </td>
                <td style="padding:12px 15px;"><?= e($t['discipline_name'] ?? '') ?></td>
                <td style="padding:12px 15px;font-size:13px;"><?= e($typeLabel) ?></td>
                <td style="padding:12px 15px;font-size:13px;"><?= e($formatLabel) ?></td>
                <td style="padding:12px 15px;"><?= e($t['start_date'] ?? '') ?></td>
                <td style="padding:12px 15px;">
                    <span style="display:inline-block;padding:2px 10px;border-radius:10px;font-size:11px;font-weight:600;background:<?= $statusColor ?>15;color:<?= $statusColor ?>;"><?= e($statusLabel) ?></span>
                </td>
                <td style="padding:12px 15px;white-space:nowrap;">
                    <div style="display:flex;gap:6px;">
                        <a href="/tournaments/<?= (int) $t['id'] ?>" class="btn btn-sm btn-outline" style="font-size:12px;padding:4px 10px;">
                            <i data-lucide="eye" style="width:13px;height:13px;vertical-align:middle;"></i> عرض
                        </a>
                        <a href="/tournaments/<?= (int) $t['id'] ?>/edit" class="btn btn-sm btn-outline" style="font-size:12px;padding:4px 10px;">
                            <i data-lucide="edit-3" style="width:13px;height:13px;vertical-align:middle;"></i>
                        </a>
                    </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="trophy" 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 0 20px;">ابدأ بإنشاء بطولة جديدة للنادي.</p>
    <a href="/tournaments/create" class="btn btn-primary"><i data-lucide="plus" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> إنشاء بطولة</a>
</div>
<?php endif; ?>

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