<?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/groups/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/groups" 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="program_id" class="form-select">
                <option value="">-- الكل --</option>
                <?php foreach ($programs as $p): ?>
                <option value="<?= (int) $p['id'] ?>" <?= ($filters['program_id'] ?? '') == $p['id'] ? 'selected' : '' ?>><?= e($p['name_ar']) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div style="min-width:160px;">
            <label class="form-label" style="font-size:12px;">المدرب</label>
            <select name="coach_id" class="form-select">
                <option value="">-- الكل --</option>
                <?php foreach ($coaches as $c): ?>
                <option value="<?= (int) $c['id'] ?>" <?= ($filters['coach_id'] ?? '') == $c['id'] ? 'selected' : '' ?>><?= e($c['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/groups" class="btn btn-sm btn-outline" style="color:#6B7280;">مسح</a>
    </form>
</div>

<!-- Groups 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($groups)): ?>
                <?php
                $statusOptions = \App\Modules\SportsActivity\Models\Group::getStatusOptions();
                $statusColors = ['active' => '#059669', 'paused' => '#D97706', 'completed' => '#6B7280', 'cancelled' => '#DC2626'];
                $statusBgs = ['active' => '#ECFDF5', 'paused' => '#FEF3C7', 'completed' => '#F3F4F6', 'cancelled' => '#FEE2E2'];
                ?>
                <?php foreach ($groups as $g): ?>
                <tr>
                    <td><code style="font-size:11px;background:#F3F4F6;padding:2px 6px;border-radius:4px;"><?= e($g['code']) ?></code></td>
                    <td>
                        <a href="/sa/groups/<?= (int) $g['id'] ?>" style="text-decoration:none;color:#1A1A2E;font-weight:600;">
                            <?= e($g['name_ar']) ?>
                        </a>
                        <?php if (!empty($g['name_en'])): ?>
                        <div style="font-size:11px;color:#9CA3AF;"><?= e($g['name_en']) ?></div>
                        <?php endif; ?>
                    </td>
                    <td><?= e($g['program_name'] ?? '—') ?></td>
                    <td><?= e($g['coach_name'] ?? '—') ?></td>
                    <td>
                        <span style="font-weight:600;color:<?= (int)$g['current_count'] >= (int)$g['max_capacity'] ? '#DC2626' : '#059669' ?>;">
                            <?= (int) $g['current_count'] ?>
                        </span>
                        / <?= (int) $g['max_capacity'] ?>
                    </td>
                    <td style="direction:ltr;text-align:right;"><?= money((float) ($g['monthly_fee_member'] ?? 0)) ?></td>
                    <td>
                        <?php $st = $g['status'] ?? 'active'; ?>
                        <span style="background:<?= $statusBgs[$st] ?? '#F3F4F6' ?>;color:<?= $statusColors[$st] ?? '#6B7280' ?>;padding:3px 10px;border-radius:10px;font-size:12px;font-weight:600;">
                            <?= e($statusOptions[$st] ?? $st) ?>
                        </span>
                    </td>
                    <td>
                        <div style="display:flex;gap:6px;">
                            <a href="/sa/groups/<?= (int) $g['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/groups/<?= (int) $g['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="users" style="width:36px;height:36px;color:#D1D5DB;display:block;margin:0 auto 10px;"></i>
                        <?php if (!empty($filters['q']) || !empty($filters['program_id']) || !empty($filters['coach_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/groups?' . 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(); ?>
