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

<?= $this->section('content') ?>
    <div class="space-y-6">
        <section class="page-header">
            <div>
                <p class="page-kicker">Reports</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="flex flex-wrap items-center gap-3">
                <a href="<?= esc($exportUrls['csv']) ?>" class="btn-secondary">Export CSV</a>
                <a href="<?= esc($exportUrls['pdf']) ?>" class="btn-primary">Export PDF</a>
            </div>
        </section>

        <form method="get" class="surface-card p-5">
            <div class="grid gap-4 xl:grid-cols-[1.4fr_220px_220px_220px_auto]">
                <div>
                    <label class="mb-2 block text-xs font-semibold uppercase tracking-[0.16em] text-slate-500">Raport</label>
                    <select name="report" class="form-select">
                        <?php foreach ($filters['report_options'] as $key => $label) : ?>
                            <option value="<?= esc($key) ?>" <?= $filters['selected_report'] === $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>
                <?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">Nivel</label>
                    <select name="level_id" class="form-select">
                        <option value="0">Toate nivelele</option>
                        <?php foreach ($filters['level_options'] as $level) : ?>
                            <option value="<?= esc((string) $level['id']) ?>" <?= (int) $filters['selected_level_id'] === (int) $level['id'] ? 'selected' : '' ?>>
                                <?= esc($level['name']) ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="flex items-end gap-3">
                    <button type="submit" class="btn-primary w-full xl:w-auto">Genereaza</button>
                    <a href="<?= url_to('reports.index') ?>" class="btn-secondary w-full xl:w-auto">Reset</a>
                </div>
            </div>

            <div class="mt-4 grid gap-4 lg:grid-cols-[220px_220px_auto]">
                <div>
                    <label class="mb-2 block text-xs font-semibold uppercase tracking-[0.16em] text-slate-500">Status copil</label>
                    <select name="child_status" class="form-select">
                        <?php foreach ($filters['status_options'] as $key => $label) : ?>
                            <option value="<?= esc($key) ?>" <?= $filters['selected_child_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">Copil pentru progres</label>
                    <select name="child_id" class="form-select">
                        <option value="0">Auto-select</option>
                        <?php foreach ($filters['child_options'] as $child) : ?>
                            <option value="<?= esc((string) $child['id']) ?>" <?= (int) $filters['selected_child_id'] === (int) $child['id'] ? 'selected' : '' ?>>
                                <?= esc($child['full_name']) ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>
            </div>

            <?php if (! empty($appliedFilters)) : ?>
                <div class="mt-5 border-t border-slate-200 pt-4">
                    <p class="text-xs font-semibold uppercase tracking-[0.16em] text-slate-500">Selectie curenta</p>
                    <div class="mt-3 flex flex-wrap gap-2">
                        <?php foreach ($appliedFilters as $filter) : ?>
                            <span class="inline-flex items-center gap-2 rounded-full border border-slate-200 bg-slate-50 px-3 py-1.5 text-xs text-slate-600">
                                <span class="font-semibold text-slate-700"><?= esc($filter['label']) ?>:</span>
                                <span><?= esc($filter['value']) ?></span>
                            </span>
                        <?php endforeach; ?>
                    </div>
                    <p class="mt-3 text-sm text-slate-500">Exporturile folosesc exact aceeasi selectie aplicata in tabel.</p>
                </div>
            <?php endif; ?>
        </form>

        <?php if (! empty($report['summary'])) : ?>
            <section class="grid gap-4 sm:grid-cols-2 xl:grid-cols-3">
                <?php foreach ($report['summary'] as $item) : ?>
                    <article class="mini-stat">
                        <p class="text-sm font-medium text-slate-500"><?= esc($item['label']) ?></p>
                        <p class="mt-3 text-3xl font-semibold text-slate-900"><?= esc((string) $item['value']) ?></p>
                        <p class="mt-2 text-sm text-slate-500"><?= esc($item['meta']) ?></p>
                    </article>
                <?php endforeach; ?>
            </section>
        <?php endif; ?>

        <section class="surface-card p-6">
            <div class="mb-4 flex flex-wrap items-start justify-between gap-4">
                <div>
                    <h3 class="section-title"><?= esc($report['title']) ?></h3>
                    <p class="section-copy"><?= esc($report['description']) ?></p>
                </div>
                <?php if (! empty($report['detailUrl'])) : ?>
                    <a href="<?= esc($report['detailUrl']) ?>" class="btn-ghost">Deschide profilul</a>
                <?php endif; ?>
            </div>

            <?php if (($report['rows'] ?? []) === []) : ?>
                <?= view('components/empty_state', ['title' => 'Raport gol', 'description' => $report['empty'] ?? 'Nu exista date pentru configuratia selectata.']) ?>
            <?php else : ?>
                <div class="table-shell">
                    <table>
                        <thead>
                            <tr>
                                <?php foreach ($report['columns'] as $column) : ?>
                                    <th><?= esc($column) ?></th>
                                <?php endforeach; ?>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($report['rows'] as $row) : ?>
                                <tr>
                                    <?php foreach ($report['columns'] as $column) : ?>
                                        <td><?= esc((string) ($row[$column] ?? '-')) ?></td>
                                    <?php endforeach; ?>
                                </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            <?php endif; ?>
        </section>
    </div>
<?= $this->endSection() ?>
