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

<?= $this->section('content') ?>
    <div class="space-y-6">
        <section class="page-header">
            <div>
                <p class="page-kicker"><?= esc($scopeType['slug'] === 'quick-check' ? 'Quick Check' : 'Evaluations') ?></p>
                <h2 class="section-title text-2xl"><?= esc($pageTitle) ?></h2>
                <p class="section-copy mt-2 max-w-3xl"><?= esc($pageDescription) ?></p>
            </div>
            <div class="rounded-2xl border border-slate-200 bg-white px-4 py-3 text-sm text-slate-500">
                Creezi o evaluare noua din profilul copilului, unde contextul si istoricul sunt deja disponibile.
            </div>
        </section>

        <form method="get" class="surface-card p-5">
            <div class="grid gap-4 xl:grid-cols-[1.1fr_220px_220px_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="Copil sau evaluator">
                </div>
                <?php if (! $filters['coach_locked']) : ?>
                    <div>
                        <label class="mb-2 block text-xs font-semibold uppercase tracking-[0.16em] text-slate-500">Trainer</label>
                        <select name="coach_id" class="form-select">
                            <option value="0">Toti trainerii</option>
                            <?php foreach ($filters['coach_options'] as $coach) : ?>
                                <option value="<?= esc((string) $coach['id']) ?>" <?= (int) $filters['selected_coach_id'] === (int) $coach['id'] ? 'selected' : '' ?>>
                                    <?= esc($coach['full_name']) ?>
                                </option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                <?php endif; ?>
                <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>
                    <label class="mb-2 block text-xs font-semibold uppercase tracking-[0.16em] text-slate-500">Perioada</label>
                    <select name="period" class="form-select">
                        <?php foreach ($filters['period_options'] as $key => $label) : ?>
                            <option value="<?= esc($key) ?>" <?= $filters['selected_period'] === $key ? 'selected' : '' ?>><?= esc($label) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="flex items-end gap-3">
                    <button type="submit" class="btn-primary w-full xl:w-auto">Filtreaza</button>
                    <a href="<?= esc($scopeType['slug'] === 'quick-check' ? url_to('quickChecks.index') : url_to('evaluations.index')) ?>" class="btn-secondary w-full xl:w-auto">Reset</a>
                </div>
            </div>
            <?php if ($filters['coach_locked']) : ?>
                <div class="mt-4 rounded-2xl bg-slate-100 px-4 py-3 text-sm text-slate-500">
                    Lista este limitata la copiii trainerului curent.
                </div>
            <?php endif; ?>
        </form>

        <section class="grid gap-4 sm:grid-cols-2 xl:grid-cols-4">
            <?php foreach ($stats as $stat) : ?>
                <?= view('components/stat_card', $stat) ?>
            <?php endforeach; ?>
        </section>

        <section class="surface-card p-6">
            <div class="mb-4">
                <h3 class="section-title">Rezultate</h3>
                <p class="section-copy">Scorul final si statusul sunt calculate in backend, folosind regulile active din tabela `evaluation_rules`.</p>
            </div>
            <?php if ($evaluations === []) : ?>
                <?= view('components/empty_state', [
                    'title' => 'Nu exista rezultate pentru filtrul curent',
                    'description' => 'Porneste o evaluare noua din profilul copilului sau schimba filtrele de selectie.',
                ]) ?>
            <?php else : ?>
                <div class="table-shell">
                    <table>
                        <thead>
                            <tr>
                                <th>Copil</th>
                                <th>Data</th>
                                <th>Evaluator</th>
                                <th>Scor</th>
                                <th>Status</th>
                                <th>Promovare</th>
                                <th></th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($evaluations as $evaluation) : ?>
                                <tr>
                                    <td>
                                        <a href="<?= url_to('children.show', $evaluation['child_id']) ?>" class="font-semibold text-slate-900 hover:text-indigo-600">
                                            <?= esc($evaluation['child_name']) ?>
                                        </a>
                                    </td>
                                    <td><?= esc($evaluation['evaluation_date']) ?></td>
                                    <td><?= esc($evaluation['evaluator_coach_name'] ?: '-') ?></td>
                                    <td><?= esc(format_score($evaluation['final_score'])) ?></td>
                                    <td><?= view('components/status_badge', ['status' => $evaluation['final_status']]) ?></td>
                                    <td><?= view('components/status_badge', ['status' => (int) $evaluation['promotion_signal'] === 1 ? 'READY' : 'HOLD']) ?></td>
                                    <td><a href="<?= url_to('evaluations.show', $evaluation['id']) ?>" class="btn-ghost">Detaliu</a></td>
                                </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            <?php endif; ?>
        </section>
    </div>
<?= $this->endSection() ?>
