<?php
$__template->layout('Layout.main');
?>
<?php $__template->section('title'); ?>الأكاديميات<?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<?php if (can('sa.academy.manage')): ?>
<a href="/sa/academies/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'); ?>

<!-- Search & Filters -->
<div class="card" style="margin-bottom:20px;padding:15px;">
    <form method="GET" action="/sa/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">
        </div>
        <div style="min-width:180px;">
            <label class="form-label" style="font-size:12px;">اللعبة</label>
            <select name="discipline_id" class="form-select">
                <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>
        <button type="submit" class="btn btn-primary"><i data-lucide="search" style="width:16px;height:16px;vertical-align:middle;"></i> بحث</button>
        <a href="/sa/academies" class="btn btn-outline">مسح</a>
    </form>
</div>

<!-- Academies Table -->
<?php if (!empty($academies)): ?>
<div class="card">
    <div class="table-responsive">
        <table class="data-table">
            <thead>
                <tr>
                    <th>الكود</th>
                    <th>الاسم</th>
                    <th>اللعبة</th>
                    <th>النوع</th>
                    <th>الحالة</th>
                    <th>العقود</th>
                    <th>إجراءات</th>
                </tr>
            </thead>
            <tbody>
                <?php
                $typeLabels = ['internal' => 'داخلية', 'external' => 'خارجية', 'partnership' => 'شراكة'];
                $typeColors = ['internal' => '#059669', 'external' => '#7C3AED', 'partnership' => '#1e3a5f'];
                foreach ($academies as $academy):
                    $aType = $academy['academy_type'] ?? 'internal';
                    $atColor = $typeColors[$aType] ?? '#6B7280';
                ?>
                <tr>
                    <td><code style="font-size:12px;"><?= e($academy['code'] ?? '') ?></code></td>
                    <td>
                        <a href="/sa/academies/<?= (int) $academy['id'] ?>" style="text-decoration:none;color:#1e3a5f;font-weight:600;">
                            <?= e($academy['name_ar'] ?? '') ?>
                        </a>
                        <?php if (!empty($academy['name_en'])): ?>
                        <div style="font-size:11px;color:#9CA3AF;" dir="ltr"><?= e($academy['name_en']) ?></div>
                        <?php endif; ?>
                    </td>
                    <td><?= e($academy['discipline_name'] ?? '-') ?></td>
                    <td>
                        <span style="padding:3px 10px;border-radius:10px;font-size:11px;font-weight:600;background:<?= $atColor ?>15;color:<?= $atColor ?>;">
                            <?= e($typeLabels[$aType] ?? $aType) ?>
                        </span>
                    </td>
                    <td>
                        <?php if ((int) ($academy['is_active'] ?? 0)): ?>
                        <span style="padding:3px 10px;border-radius:10px;font-size:11px;font-weight:600;background:#05966915;color:#059669;">فعالة</span>
                        <?php else: ?>
                        <span style="padding:3px 10px;border-radius:10px;font-size:11px;font-weight:600;background:#DC262615;color:#DC2626;">معطلة</span>
                        <?php endif; ?>
                    </td>
                    <td style="text-align:center;">
                        <span style="padding:3px 10px;border-radius:10px;font-size:12px;font-weight:600;background:#F3F4F6;">
                            <?= (int) ($academy['contract_count'] ?? 0) ?>
                        </span>
                    </td>
                    <td>
                        <div style="display:flex;gap:6px;">
                            <a href="/sa/academies/<?= (int) $academy['id'] ?>" class="btn btn-sm btn-outline" title="عرض">
                                <i data-lucide="eye" style="width:14px;height:14px;"></i>
                            </a>
                            <?php if (can('sa.academy.manage')): ?>
                            <a href="/sa/academies/<?= (int) $academy['id'] ?>/edit" class="btn btn-sm btn-outline" title="تعديل">
                                <i data-lucide="edit-3" style="width:14px;height:14px;"></i>
                            </a>
                            <a href="/sa/academies/<?= (int) $academy['id'] ?>/contracts" class="btn btn-sm btn-outline" title="العقود">
                                <i data-lucide="file-signature" style="width:14px;height:14px;"></i>
                            </a>
                            <?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, 'baseUrl' => '/sa/academies?' . http_build_query(array_filter($filters))]); ?>
    </div>
    <?php endif; ?>
</div>
<?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['discipline_id'])): ?>
            لا توجد نتائج مطابقة لبحثك.
        <?php else: ?>
            ابدأ بإضافة أكاديمية جديدة.
        <?php endif; ?>
    </p>
    <?php if (can('sa.academy.manage')): ?>
    <a href="/sa/academies/create" class="btn btn-primary"><i data-lucide="plus" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> إضافة أكاديمية</a>
    <?php endif; ?>
</div>
<?php endif; ?>

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