<?= $this->extend('layouts/dashboard') ?>

<?= $this->section('content') ?>
    <div class="space-y-6">
        <section class="page-header">
            <div>
                <p class="page-kicker">Academy Groups</p>
                <h2 class="section-title text-2xl"><?= esc($pageTitle) ?></h2>
                <p class="section-copy mt-2 max-w-3xl"><?= esc($pageDescription) ?></p>
            </div>
            <?php if ($canManage) : ?>
                <a href="<?= url_to('groups.new') ?>" class="btn-primary">Adauga grupa</a>
            <?php endif; ?>
        </section>

        <form method="get" class="surface-card p-5">
            <div class="grid gap-4 lg:grid-cols-[1fr_auto]">
                <div>
                    <label class="mb-2 block text-xs font-semibold uppercase tracking-[0.16em] text-slate-500">Cautare</label>
                    <input type="text" name="q" class="form-input" value="<?= esc($query) ?>" placeholder="Nume, cod sau trainer">
                </div>
                <div class="flex items-end gap-3">
                    <button type="submit" class="btn-primary w-full lg:w-auto">Cauta</button>
                    <a href="<?= url_to('groups.index') ?>" class="btn-secondary w-full lg:w-auto">Reset</a>
                </div>
            </div>
        </form>

        <section class="surface-card p-6">
            <div class="mb-4">
                <h3 class="section-title">Grupe active</h3>
                <p class="section-copy">Fiecare grupa este legata de nivel, trainer principal si capacitate operationala.</p>
            </div>
            <?php if ($groups === []) : ?>
                <?= view('components/empty_state', ['title' => 'Nu exista grupe', 'description' => 'Adauga o grupa noua sau schimba filtrul de cautare.']) ?>
            <?php else : ?>
                <div class="table-shell">
                    <table>
                        <thead>
                            <tr>
                                <th>Grupa</th>
                                <th>Cod</th>
                                <th>Nivel</th>
                                <th>Trainer</th>
                                <th>Program</th>
                                <th>Capacitate</th>
                                <th>Copii activi</th>
                                <th>Status</th>
                                <?php if ($canManage) : ?><th>Actiuni</th><?php endif; ?>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($groups as $group) : ?>
                                <tr>
                                    <td class="font-medium text-slate-900"><?= esc($group['name']) ?></td>
                                    <td><?= esc($group['code']) ?></td>
                                    <td>
                                        <?php if (! empty($group['level_name'])) : ?>
                                            <span class="inline-flex rounded-full border px-3 py-1 text-xs font-semibold" style="<?= esc(level_chip_style($group['color_hex']), 'attr') ?>">
                                                <?= esc($group['level_name']) ?>
                                            </span>
                                        <?php else : ?>
                                            —
                                        <?php endif; ?>
                                    </td>
                                    <td><?= esc($group['coach_name'] ?: '—') ?></td>
                                    <td><?= esc($group['schedule_summary'] ?: '—') ?></td>
                                    <td><?= esc((string) $group['capacity']) ?></td>
                                    <td><?= esc((string) $group['active_children']) ?></td>
                                    <td><?= view('components/status_badge', ['status' => strtoupper((string) $group['status'])]) ?></td>
                                    <?php if ($canManage) : ?>
                                        <td><a href="<?= url_to('groups.edit', $group['id']) ?>" class="btn-ghost">Edit</a></td>
                                    <?php endif; ?>
                                </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            <?php endif; ?>
        </section>
    </div>
<?= $this->endSection() ?>
