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

<?php $__template->section('page_actions'); ?>
<a href="/disciplines/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
$currentCategory = $filters['category'] ?? '';
$allCategories = SportDiscipline::getCategories();
?>

<!-- Category 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="/disciplines"
           style="padding:12px 20px;font-size:14px;font-weight:600;text-decoration:none;border-bottom:3px solid <?= $currentCategory === '' ? '#0D7377' : 'transparent' ?>;color:<?= $currentCategory === '' ? '#0D7377' : '#6B7280' ?>;white-space:nowrap;">
            الكل
        </a>
        <?php foreach ($allCategories as $catKey => $catLabel): ?>
        <a href="/disciplines?category=<?= e($catKey) ?><?= ($filters['q'] ?? '') !== '' ? '&q=' . urlencode($filters['q']) : '' ?>"
           style="padding:12px 20px;font-size:14px;font-weight:600;text-decoration:none;border-bottom:3px solid <?= $currentCategory === $catKey ? '#0D7377' : 'transparent' ?>;color:<?= $currentCategory === $catKey ? '#0D7377' : '#6B7280' ?>;white-space:nowrap;">
            <?= e($catLabel) ?>
        </a>
        <?php endforeach; ?>
    </div>
</div>

<!-- Search Bar -->
<div class="card" style="margin-bottom:20px;padding:15px;">
    <form method="GET" action="/disciplines" style="display:flex;gap:10px;align-items:end;">
        <div style="flex:1;">
            <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:250px;">
        </div>
        <?php if ($currentCategory !== ''): ?>
        <input type="hidden" name="category" value="<?= e($currentCategory) ?>">
        <?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="/disciplines" class="btn btn-sm btn-outline" style="color:#6B7280;">مسح</a>
    </form>
</div>

<!-- Disciplines Grid -->
<?php if (!empty($disciplines)): ?>
<div style="display:grid;grid-template-columns:repeat(auto-fill, minmax(320px, 1fr));gap:20px;margin-bottom:20px;">
    <?php foreach ($disciplines as $d):
        $catColor = SportDiscipline::getCategoryColor($d['category'] ?? '');
        $catLabel = SportDiscipline::getCategoryLabel($d['category'] ?? '');
        $isActive = (int) ($d['is_active'] ?? 0);
    ?>
    <div class="card" style="transition:box-shadow 0.2s;position:relative;overflow:hidden;<?= !$isActive ? 'opacity:0.65;' : '' ?>">
        <?php if (!$isActive): ?>
        <div style="position:absolute;top:12px;left:12px;z-index:2;">
            <span class="badge" style="background:#FEE2E2;color:#DC2626;font-size:11px;padding:3px 10px;border-radius:10px;">معطّل</span>
        </div>
        <?php endif; ?>

        <a href="/disciplines/<?= (int) $d['id'] ?>" style="text-decoration:none;color:inherit;display:block;">
            <!-- Card Header with Icon -->
            <div style="padding:20px 20px 15px;display:flex;align-items:start;gap:15px;">
                <div style="width:52px;height:52px;border-radius:12px;background:linear-gradient(135deg, <?= $catColor ?>15, <?= $catColor ?>30);display:flex;align-items:center;justify-content:center;flex-shrink:0;">
                    <i data-lucide="<?= e($d['icon'] ?? 'activity') ?>" style="width:26px;height:26px;color:<?= $catColor ?>;"></i>
                </div>
                <div style="flex:1;min-width:0;">
                    <h3 style="margin:0 0 4px;font-size:16px;font-weight:700;color:#1A1A2E;"><?= e($d['name_ar']) ?></h3>
                    <?php if (!empty($d['name_en'])): ?>
                    <div style="font-size:12px;color:#9CA3AF;margin-bottom:6px;"><?= e($d['name_en']) ?></div>
                    <?php endif; ?>
                    <span style="display:inline-block;padding:2px 10px;border-radius:10px;font-size:11px;font-weight:600;background:<?= $catColor ?>15;color:<?= $catColor ?>;"><?= e($catLabel) ?></span>
                </div>
            </div>

            <!-- Card Body -->
            <div style="padding:0 20px 15px;font-size:12px;color:#6B7280;">
                <?php if (!empty($d['description_ar'])): ?>
                <span style="display:-webkit-box;-webkit-line-clamp:2;-webkit-box-orient:vertical;overflow:hidden;"><?= e($d['description_ar']) ?></span>
                <?php endif; ?>
            </div>
        </a>

        <!-- Card Footer -->
        <div style="padding:10px 20px;border-top:1px solid #F3F4F6;display:flex;justify-content:space-between;align-items:center;">
            <code style="font-size:11px;color:#9CA3AF;background:#F9FAFB;padding:2px 6px;border-radius:4px;"><?= e($d['code'] ?? '') ?></code>
            <div style="display:flex;gap:6px;">
                <a href="/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>
                <a href="/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>
            </div>
        </div>
    </div>
    <?php endforeach; ?>
</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="activity" 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;">
        <?php if (!empty($filters['q']) || !empty($filters['category'])): ?>
            لا توجد نتائج مطابقة لبحثك. جرب تغيير معايير البحث.
        <?php else: ?>
            ابدأ بإضافة نشاط رياضي جديد للنادي.
        <?php endif; ?>
    </p>
    <a href="/disciplines/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(); ?>
