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

<?= $this->section('content') ?>
    <div class="space-y-6">
        <section class="page-header">
            <div>
                <p class="page-kicker">Traineri</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('coaches.new') ?>" class="btn-primary">Adauga trainer</a>
            <?php endif; ?>
        </section>

        <form method="get" class="surface-card p-5">
            <div class="grid gap-4 lg:grid-cols-[1fr_220px_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($filters['query']) ?>" placeholder="Nume, email sau rol">
                </div>
                <div>
                    <label class="mb-2 block text-xs font-semibold uppercase tracking-[0.16em] text-slate-500">Status</label>
                    <select name="status" class="form-select">
                        <?php foreach ($filters['status_options'] as $key => $label) : ?>
                            <option value="<?= esc($key) ?>" <?= $filters['selected_status'] === $key ? 'selected' : '' ?>><?= esc($label) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="flex items-end gap-3">
                    <button type="submit" class="btn-primary w-full lg:w-auto">Filtreaza</button>
                    <a href="<?= url_to('coaches.index') ?>" class="btn-secondary w-full lg:w-auto">Reset</a>
                </div>
            </div>
        </form>

        <section class="surface-card p-6">
            <div class="mb-4 flex items-start justify-between gap-4">
                <div>
                    <h3 class="section-title">Lista traineri</h3>
                    <p class="section-copy">Adminul vede toata echipa. Un trainer isi vede doar propriul profil si metricele aferente.</p>
                </div>
            </div>
            <?php if ($coaches === []) : ?>
                <?= view('components/empty_state', ['title' => 'Nu exista traineri pentru filtrul curent', 'description' => 'Adauga un trainer nou sau schimba filtrele de cautare.']) ?>
            <?php else : ?>
                <div class="table-shell">
                    <table>
                        <thead>
                            <tr>
                                <th>Trainer</th>
                                <th>Rol</th>
                                <th>Telefon</th>
                                <th>Email</th>
                                <th>Copii activi</th>
                                <th>Evaluari efectuate</th>
                                <th>Scor mediu copii</th>
                                <th>Program</th>
                                <th>Actiuni</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($coaches as $coach) : ?>
                                <tr>
                                    <td>
                                        <div class="flex items-center gap-3">
                                            <div class="flex h-11 w-11 items-center justify-center rounded-2xl bg-indigo-50 text-sm font-semibold text-indigo-700">
                                                <?= esc(strtoupper(substr((string) $coach['full_name'], 0, 1))) ?>
                                            </div>
                                            <div>
                                                <a href="<?= url_to('coaches.show', $coach['id']) ?>" class="font-semibold text-slate-900 hover:text-indigo-600"><?= esc($coach['full_name']) ?></a>
                                                <p class="mt-1 text-xs text-slate-400">@<?= esc($coach['username'] ?: 'coach') ?></p>
                                            </div>
                                        </div>
                                    </td>
                                    <td>
                                        <div class="space-y-2">
                                            <span class="text-sm font-medium text-slate-900"><?= esc($coach['display_role']) ?></span>
                                            <?= view('components/status_badge', ['status' => (int) $coach['is_active'] === 1 ? 'ACTIVE' : 'INACTIVE']) ?>
                                        </div>
                                    </td>
                                    <td><?= esc($coach['phone'] ?: '—') ?></td>
                                    <td><?= esc($coach['email']) ?></td>
                                    <td><?= esc((string) $coach['active_children']) ?></td>
                                    <td><?= esc((string) $coach['evaluations_count']) ?></td>
                                    <td><?= esc($coach['avg_child_score_label']) ?></td>
                                    <td class="max-w-xs"><?= esc($coach['availability_summary']) ?></td>
                                    <td>
                                        <div class="flex gap-2">
                                            <a href="<?= url_to('coaches.show', $coach['id']) ?>" class="btn-ghost">Profil</a>
                                            <?php if ($canManage) : ?>
                                                <a href="<?= url_to('coaches.edit', $coach['id']) ?>" class="btn-ghost">Edit</a>
                                            <?php endif; ?>
                                        </div>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            <?php endif; ?>
        </section>
    </div>
<?= $this->endSection() ?>
