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

<?= $this->section('content') ?>
    <div class="space-y-6">
        <section class="page-header">
            <div>
                <p class="page-kicker">Attendance Session</p>
                <div class="mt-2 flex flex-wrap items-center gap-3">
                    <h2 class="section-title text-2xl"><?= esc($session['group_name']) ?></h2>
                    <?= view('components/status_badge', ['status' => strtoupper((string) $session['status'])]) ?>
                    <span class="rounded-full bg-slate-100 px-3 py-1 text-xs font-semibold uppercase tracking-[0.16em] text-slate-500"><?= esc($session['session_date']) ?></span>
                </div>
                <p class="section-copy mt-2 max-w-3xl"><?= esc($pageDescription) ?></p>
            </div>
            <a href="<?= esc($backUrl) ?>" class="btn-ghost">Inapoi la lista</a>
        </section>

        <section class="grid gap-4 sm:grid-cols-2 xl:grid-cols-5">
            <article class="mini-stat">
                <p class="text-sm font-medium text-slate-500">Participanti</p>
                <p class="mt-3 text-3xl font-semibold text-slate-900"><?= esc((string) $breakdown['participants']) ?></p>
            </article>
            <article class="mini-stat">
                <p class="text-sm font-medium text-slate-500">Inregistrari salvate</p>
                <p class="mt-3 text-3xl font-semibold text-slate-900"><?= esc((string) $breakdown['saved_records']) ?></p>
            </article>
            <article class="mini-stat">
                <p class="text-sm font-medium text-slate-500">Prezenti</p>
                <p class="mt-3 text-3xl font-semibold text-slate-900"><?= esc((string) ($breakdown['present'] + $breakdown['recovery'])) ?></p>
            </article>
            <article class="mini-stat">
                <p class="text-sm font-medium text-slate-500">Absente</p>
                <p class="mt-3 text-3xl font-semibold text-slate-900"><?= esc((string) ($breakdown['absent_excused'] + $breakdown['absent_unexcused'])) ?></p>
            </article>
            <article class="mini-stat">
                <p class="text-sm font-medium text-slate-500">Rata prezenta</p>
                <p class="mt-3 text-3xl font-semibold text-slate-900"><?= esc(format_percentage((float) $breakdown['attendance_rate'], 1)) ?></p>
            </article>
        </section>

        <section class="grid gap-6 lg:grid-cols-[1.05fr_0.95fr]">
            <article class="surface-card p-6">
                <h3 class="section-title">Context sesiune</h3>
                <dl class="info-list mt-5 grid gap-5 md:grid-cols-2">
                    <div>
                        <dt>Grupa</dt>
                        <dd><?= esc($session['group_name']) ?></dd>
                    </div>
                    <div>
                        <dt>Trainer</dt>
                        <dd><?= esc($session['coach_name'] ?: '-') ?></dd>
                    </div>
                    <div>
                        <dt>Interval</dt>
                        <dd><?= esc(substr((string) $session['start_time'], 0, 5) . ' - ' . substr((string) $session['end_time'], 0, 5)) ?></dd>
                    </div>
                    <div>
                        <dt>Status</dt>
                        <dd><?= esc(status_label($session['status'])) ?></dd>
                    </div>
                    <?php if (! empty($session['recovery_source_date'])) : ?>
                        <div>
                            <dt>Recovery pentru</dt>
                            <dd><?= esc($session['recovery_source_date']) ?></dd>
                        </div>
                    <?php endif; ?>
                    <div class="md:col-span-2">
                        <dt>Note</dt>
                        <dd><?= esc($session['notes'] ?: $session['cancelled_reason'] ?: 'Nu exista note operationale.') ?></dd>
                    </div>
                </dl>
            </article>

            <article class="surface-card p-6">
                <h3 class="section-title">Reguli check-in</h3>
                <div class="mt-5 space-y-3 text-sm text-slate-600">
                    <p>Randurile fara inregistrare salvata pornesc cu selectie implicita pe <strong>Prezent</strong> pentru operare rapida.</p>
                    <p>Orice absenta care impinge copilul peste pragul configurat poate genera automat eveniment in journey si notificare.</p>
                    <p>Sesiunile anulate nu intra in calculul rapoartelor de prezenta.</p>
                </div>
            </article>
        </section>

        <form method="post" action="<?= esc($saveUrl) ?>" class="surface-card p-6">
            <div class="mb-4 flex flex-wrap items-start justify-between gap-4">
                <div>
                    <h3 class="section-title">Check-in rapid pe lista</h3>
                    <p class="section-copy">Operare pe copil cu status real de prezenta, absenta sau recuperare.</p>
                </div>
            </div>

            <?php if ($roster === []) : ?>
                <?= view('components/empty_state', ['title' => 'Nu exista copii in grupa', 'description' => 'Adauga copii activi in grupa pentru a putea salva prezenta.']) ?>
            <?php else : ?>
                <div class="table-shell">
                    <table>
                        <thead>
                            <tr>
                                <th>Copil</th>
                                <th>Nivel</th>
                                <th>Status copil</th>
                                <th>Status prezenta</th>
                                <th>Note</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($roster as $child) : ?>
                                <tr>
                                    <td class="font-semibold text-slate-900"><?= esc($child['full_name']) ?></td>
                                    <td><?= esc($child['level_name'] ?: '-') ?></td>
                                    <td><?= view('components/status_badge', ['status' => strtoupper((string) $child['child_status'])]) ?></td>
                                    <td class="min-w-[190px]">
                                        <select name="records[<?= esc((string) $child['id']) ?>][status]" class="form-select">
                                            <?php foreach ($recordOptions as $key => $label) : ?>
                                                <option value="<?= esc($key) ?>" <?= (string) $child['selected_status'] === (string) $key ? 'selected' : '' ?>><?= esc($label) ?></option>
                                            <?php endforeach; ?>
                                        </select>
                                    </td>
                                    <td class="min-w-[260px]">
                                        <input type="text" name="records[<?= esc((string) $child['id']) ?>][notes]" class="form-input" value="<?= esc((string) ($child['attendance_notes'] ?? '')) ?>" placeholder="Optional">
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>

                <div class="mt-6 flex flex-wrap gap-3">
                    <button type="submit" class="btn-primary">Salveaza check-in</button>
                    <a href="<?= esc($backUrl) ?>" class="btn-secondary">Inapoi</a>
                </div>
            <?php endif; ?>
        </form>
    </div>
<?= $this->endSection() ?>
