<?php
use App\Modules\Academies\Models\Academy;
use App\Modules\Academies\Models\AcademyLevel;
$__template->layout('Layout.main');
?>
<?php $__template->section('title'); ?>الأكاديميات<?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<a href="/academies/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
$currentType = $filters['academy_type'] ?? '';
$allTypes = Academy::getAcademyTypes();
?>

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

<!-- Search Bar -->
<div class="card" style="margin-bottom:20px;padding:15px;">
    <form method="GET" action="/academies" 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:180px;">
            <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 ($currentType !== ''): ?>
        <input type="hidden" name="academy_type" value="<?= e($currentType) ?>">
        <?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="/academies" class="btn btn-sm btn-outline" style="color:#6B7280;">مسح</a>
    </form>
</div>

<!-- Academies Grid -->
<?php if (!empty($academies)): ?>
<div style="display:grid;grid-template-columns:repeat(auto-fill, minmax(320px, 1fr));gap:20px;margin-bottom:20px;">
    <?php foreach ($academies as $a):
        $typeColor = Academy::getTypeColor($a['academy_type'] ?? '');
        $typeLabel = Academy::getTypeLabel($a['academy_type'] ?? '');
        $isActive = (int) ($a['is_active'] ?? 0);
        $levels = AcademyLevel::getForAcademy((int) $a['id']);
        $levelCount = count($levels);
        $descExcerpt = mb_substr($a['description_ar'] ?? '', 0, 80);
        if (mb_strlen($a['description_ar'] ?? '') > 80) {
            $descExcerpt .= '...';
        }
        // Find discipline name from the passed disciplines array
        $discName = '';
        foreach ($disciplines as $disc) {
            if ((int) $disc['id'] === (int) ($a['discipline_id'] ?? 0)) {
                $discName = $disc['name_ar'];
                break;
            }
        }
    ?>
    <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="/academies/<?= (int) $a['id'] ?>" style="text-decoration:none;color:inherit;display:block;">
            <!-- Card Header -->
            <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, <?= $typeColor ?>15, <?= $typeColor ?>30);display:flex;align-items:center;justify-content:center;flex-shrink:0;">
                    <i data-lucide="graduation-cap" style="width:26px;height:26px;color:<?= $typeColor ?>;"></i>
                </div>
                <div style="flex:1;min-width:0;">
                    <h3 style="margin:0 0 4px;font-size:16px;font-weight:700;color:#1A1A2E;"><?= e($a['name_ar']) ?></h3>
                    <?php if (!empty($a['name_en'])): ?>
                    <div style="font-size:12px;color:#9CA3AF;margin-bottom:6px;"><?= e($a['name_en']) ?></div>
                    <?php endif; ?>
                    <span style="display:inline-block;padding:2px 10px;border-radius:10px;font-size:11px;font-weight:600;background:<?= $typeColor ?>15;color:<?= $typeColor ?>;"><?= e($typeLabel) ?></span>
                </div>
            </div>

            <!-- Card Body Stats -->
            <div style="padding:0 20px 10px;display:flex;gap:15px;font-size:12px;color:#6B7280;">
                <?php if ($discName !== ''): ?>
                <span style="display:flex;align-items:center;gap:4px;">
                    <i data-lucide="activity" style="width:14px;height:14px;"></i>
                    <?= e($discName) ?>
                </span>
                <?php endif; ?>
                <span style="display:flex;align-items:center;gap:4px;">
                    <i data-lucide="layers" style="width:14px;height:14px;"></i>
                    <?= $levelCount ?> مستوى
                </span>
            </div>

            <!-- Description Excerpt -->
            <?php if (!empty($descExcerpt)): ?>
            <div style="padding:0 20px 15px;font-size:13px;color:#6B7280;line-height:1.6;">
                <?= e($descExcerpt) ?>
            </div>
            <?php endif; ?>
        </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($a['code'] ?? '') ?></code>
            <div style="display:flex;gap:6px;">
                <a href="/academies/<?= (int) $a['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="/academies/<?= (int) $a['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="graduation-cap" 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['academy_type']) || !empty($filters['discipline_id'])): ?>
            لا توجد نتائج مطابقة لبحثك. جرب تغيير معايير البحث.
        <?php else: ?>
            ابدأ بإضافة أكاديمية جديدة للنادي.
        <?php endif; ?>
    </p>
    <a href="/academies/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(); ?>
