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

<?php $__template->section('page_actions'); ?>
<?php if (can('sa.group.manage')): ?>
<a href="/sa/programs/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 & Filter Bar -->
<div class="card" style="margin-bottom:20px;padding:15px;">
    <form method="GET" action="/sa/programs" 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 $d): ?>
                <option value="<?= (int) $d['id'] ?>" <?= ($filters['discipline_id'] ?? '') == $d['id'] ? 'selected' : '' ?>><?= e($d['name_ar']) ?></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/programs" class="btn btn-sm btn-outline" style="color:#6B7280;">مسح</a>
    </form>
</div>

<!-- Programs Table -->
<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>
                    <th>إجراءات</th>
                </tr>
            </thead>
            <tbody>
                <?php if (!empty($programs)): ?>
                <?php
                $typeOptions = \App\Modules\SportsActivity\Models\Program::getProgramTypeOptions();
                ?>
                <?php foreach ($programs as $p): ?>
                <tr>
                    <td><code style="font-size:11px;background:#F3F4F6;padding:2px 6px;border-radius:4px;"><?= e($p['code']) ?></code></td>
                    <td>
                        <a href="/sa/programs/<?= (int) $p['id'] ?>" style="text-decoration:none;color:#1A1A2E;font-weight:600;">
                            <?= e($p['name_ar']) ?>
                        </a>
                        <?php if (!empty($p['name_en'])): ?>
                        <div style="font-size:11px;color:#9CA3AF;"><?= e($p['name_en']) ?></div>
                        <?php endif; ?>
                    </td>
                    <td><?= e($p['discipline_name'] ?? '—') ?></td>
                    <td>
                        <?php if (!empty($p['program_type']) && isset($typeOptions[$p['program_type']])): ?>
                        <span style="background:#F3F4F6;padding:2px 8px;border-radius:4px;font-size:12px;"><?= e($typeOptions[$p['program_type']]) ?></span>
                        <?php else: ?>
                        <span style="color:#9CA3AF;">—</span>
                        <?php endif; ?>
                    </td>
                    <td>
                        <?php if ($p['age_from'] !== null || $p['age_to'] !== null): ?>
                        <?= $p['age_from'] ?? '?' ?> - <?= $p['age_to'] ?? '?' ?> سنة
                        <?php else: ?>
                        <span style="color:#9CA3AF;">—</span>
                        <?php endif; ?>
                    </td>
                    <td>
                        <span style="background:#EEF2FF;color:#4F46E5;padding:2px 8px;border-radius:10px;font-size:12px;font-weight:600;">
                            <?= (int) ($p['group_count'] ?? 0) ?>
                        </span>
                    </td>
                    <td>
                        <?php if ((int) ($p['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/programs/<?= (int) $p['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.group.manage')): ?>
                            <a href="/sa/programs/<?= (int) $p['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="8" style="text-align:center;padding:40px;color:#6B7280;">
                        <i data-lucide="book-open" style="width:36px;height:36px;color:#D1D5DB;display:block;margin:0 auto 10px;"></i>
                        <?php if (!empty($filters['q']) || !empty($filters['discipline_id'])): ?>
                            لا توجد نتائج مطابقة لبحثك
                        <?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/programs?' . 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(); ?>
