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

<?= $this->section('content') ?>
    <div class="space-y-6">
        <section class="page-header">
            <div>
                <p class="page-kicker">Attendance</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 ($canCreate) : ?>
                <a href="<?= url_to('attendance.new') ?>" class="btn-primary">Sesiune noua</a>
            <?php endif; ?>
        </section>

        <form method="get" class="surface-card p-5">
            <div class="grid gap-4 xl:grid-cols-[1.2fr_repeat(4,minmax(0,1fr))_auto]">
                <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>
                    <label class="mb-2 block text-xs font-semibold uppercase tracking-[0.16em] text-slate-500">Grupa</label>
                    <select name="group_id" class="form-select">
                        <option value="0">Toate grupele</option>
                        <?php foreach ($filters['group_options'] as $group) : ?>
                            <option value="<?= esc((string) $group['id']) ?>" <?= (int) $filters['selected_group_id'] === (int) $group['id'] ? 'selected' : '' ?>>
                                <?= esc($group['name']) ?>
                            </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">Status sesiune</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 xl:w-auto">Filtreaza</button>
                    <a href="<?= url_to('attendance.index') ?>" class="btn-secondary w-full xl:w-auto">Reset</a>
                </div>
            </div>
        </form>

        <section class="grid gap-4 sm:grid-cols-2 xl:grid-cols-5">
            <?php foreach ($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>

        <section class="surface-card p-6">
            <div class="mb-4 flex flex-wrap items-start justify-between gap-4">
                <div>
                    <h3 class="section-title">Sesiuni & check-in</h3>
                    <p class="section-copy">Operare rapida pe sedinte recurente, recovery si sesiuni anulate.</p>
                </div>
                <span class="rounded-full bg-slate-100 px-3 py-1 text-xs font-semibold uppercase tracking-[0.16em] text-slate-500">
                    <?= esc($filters['date_start']) ?> - <?= esc($filters['date_end']) ?>
                </span>
            </div>

            <?php if ($sessions === []) : ?>
                <?= view('components/empty_state', ['title' => 'Nu exista sesiuni', 'description' => 'Creeaza prima sesiune sau ajusteaza filtrele pentru a vedea sedintele disponibile.']) ?>
            <?php else : ?>
                <div class="table-shell">
                    <table>
                        <thead>
                            <tr>
                                <th>Sesiune</th>
                                <th>Data</th>
                                <th>Trainer</th>
                                <th>Status</th>
                                <th>Participanti</th>
                                <th>Prezenta</th>
                                <th></th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($sessions as $session) : ?>
                                <tr>
                                    <td>
                                        <p class="font-semibold text-slate-900"><?= esc($session['group_name']) ?></p>
                                        <p class="mt-1 text-xs text-slate-500"><?= esc(($session['group_code'] ?: '-') . ' / ' . substr((string) $session['start_time'], 0, 5) . ' - ' . substr((string) $session['end_time'], 0, 5)) ?></p>
                                        <?php if (! empty($session['cancelled_reason'])) : ?>
                                            <p class="mt-2 text-xs text-rose-600"><?= esc($session['cancelled_reason']) ?></p>
                                        <?php endif; ?>
                                    </td>
                                    <td><?= esc($session['session_date']) ?></td>
                                    <td><?= esc($session['coach_name'] ?: '-') ?></td>
                                    <td><?= view('components/status_badge', ['status' => strtoupper((string) $session['status'])]) ?></td>
                                    <td><?= esc((string) $session['participants']) ?></td>
                                    <td>
                                        <p class="font-semibold text-slate-900"><?= esc($session['attendance_label']) ?></p>
                                        <p class="mt-1 text-xs text-slate-500">
                                            <?= esc((string) ($session['breakdown']['attended'] ?? 0)) ?> prezenti / <?= esc((string) ($session['breakdown']['total'] ?? 0)) ?> operati
                                        </p>
                                    </td>
                                    <td><a href="<?= url_to('attendance.show', $session['id']) ?>" class="btn-ghost">Check-in</a></td>
                                </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            <?php endif; ?>
        </section>
    </div>
<?= $this->endSection() ?>
