<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>الأنشطة الرياضية<?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<?php if (can('sa.discipline.manage')): ?>
<a href="/sa/disciplines/create" class="btn btn-primary"><i data-lucide="plus" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> إضافة نشاط رياضي</a>
<?php endif; ?>
<?php $__template->endSection(); ?>

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

<?php
$currentCategory = $filters['category'] ?? '';
$allCategories = $categories ?? [];
?>

<!-- Search & Filter Bar -->
<div class="card" style="margin-bottom:20px;padding:15px;">
    <form method="GET" action="/sa/disciplines" 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">
        </div>
        <div style="min-width:160px;">
            <label class="form-label" style="font-size:12px;">التصنيف</label>
            <select name="category" class="form-select">
                <option value="">-- الكل --</option>
                <?php foreach ($allCategories as $catKey => $catLabel): ?>
                <option value="<?= e($catKey) ?>" <?= $currentCategory === $catKey ? 'selected' : '' ?>><?= e($catLabel) ?></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/disciplines" class="btn btn-sm btn-outline" style="color:#6B7280;">مسح</a>
    </form>
</div>

<!-- Disciplines Table -->
<div class="card">
    <div class="table-responsive">
        <table class="data-table">
            <thead>
                <tr>
                    <th>الكود</th>
                    <th>الاسم</th>
                    <th>التصنيف</th>
                    <th>الحالة</th>
                    <th>إجراءات</th>
                </tr>
            </thead>
            <tbody>
                <?php if (!empty($disciplines)): ?>
                <?php foreach ($disciplines as $d): ?>
                <tr>
                    <td><code style="font-size:11px;background:#F3F4F6;padding:2px 6px;border-radius:4px;"><?= e($d['code']) ?></code></td>
                    <td>
                        <a href="/sa/disciplines/<?= (int) $d['id'] ?>" style="text-decoration:none;color:#1A1A2E;font-weight:600;">
                            <?= e($d['name_ar']) ?>
                        </a>
                        <?php if (!empty($d['name_en'])): ?>
                        <div style="font-size:11px;color:#9CA3AF;"><?= e($d['name_en']) ?></div>
                        <?php endif; ?>
                    </td>
                    <td>
                        <?php if (!empty($d['category']) && isset($allCategories[$d['category']])): ?>
                        <span style="background:#F3F4F6;padding:2px 8px;border-radius:4px;font-size:12px;"><?= e($allCategories[$d['category']]) ?></span>
                        <?php else: ?>
                        <span style="color:#9CA3AF;">—</span>
                        <?php endif; ?>
                    </td>
                    <td>
                        <?php if ((int) ($d['is_active'] ?? 0)): ?>
                        <span style="background:#ECFDF5;color:#059669;padding:3px 10px;border-radius:10px;font-size:12px;font-weight:600;">فعّال</span>
                        <?php else: ?>
                        <span style="background:#FEE2E2;color:#DC2626;padding:3px 10px;border-radius:10px;font-size:12px;font-weight:600;">معطّل</span>
                        <?php endif; ?>
                    </td>
                    <td>
                        <div style="display:flex;gap:6px;">
                            <a href="/sa/disciplines/<?= (int) $d['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>
                            <?php if (can('sa.discipline.manage')): ?>
                            <a href="/sa/disciplines/<?= (int) $d['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>
                            <?php endif; ?>
                        </div>
                    </td>
                </tr>
                <?php endforeach; ?>
                <?php else: ?>
                <tr>
                    <td colspan="5" style="text-align:center;padding:40px;color:#6B7280;">
                        <i data-lucide="activity" style="width:36px;height:36px;color:#D1D5DB;display:block;margin:0 auto 10px;"></i>
                        <?php if (!empty($filters['q']) || !empty($filters['category'])): ?>
                            لا توجد نتائج مطابقة لبحثك
                        <?php else: ?>
                            لا توجد أنشطة رياضية بعد
                        <?php endif; ?>
                    </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/disciplines?' . 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(); ?>
