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

<?= $this->section('content') ?>
    <div class="space-y-6">
        <section class="page-header">
            <div>
                <p class="page-kicker">Attendance Setup</p>
                <h2 class="section-title text-2xl"><?= esc($pageTitle) ?></h2>
                <p class="section-copy mt-2 max-w-3xl"><?= esc($pageDescription) ?></p>
            </div>
            <a href="<?= esc($cancelUrl) ?>" class="btn-ghost">Inapoi la prezenta</a>
        </section>

        <?php if (! empty($errors['general'])) : ?>
            <div class="rounded-2xl border border-rose-200 bg-rose-50 px-4 py-3 text-sm text-rose-700"><?= esc($errors['general']) ?></div>
        <?php endif; ?>

        <form method="post" action="<?= esc($submitUrl) ?>" class="surface-card p-6" x-data="{ status: '<?= esc((string) $values['status']) ?>' }">
            <div class="grid gap-5 md:grid-cols-2 xl:grid-cols-3">
                <div>
                    <label class="mb-2 block text-sm font-medium text-slate-700">Grupa</label>
                    <select name="academy_group_id" class="form-select">
                        <?php foreach ($groupOptions as $group) : ?>
                            <option value="<?= esc((string) $group['id']) ?>" <?= (string) $values['academy_group_id'] === (string) $group['id'] ? 'selected' : '' ?>>
                                <?= esc($group['name']) ?><?= ! empty($group['coach_name']) ? ' / ' . esc($group['coach_name']) : '' ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                    <?php if (! empty($errors['academy_group_id'])) : ?><p class="field-error"><?= esc($errors['academy_group_id']) ?></p><?php endif; ?>
                </div>

                <div>
                    <label class="mb-2 block text-sm font-medium text-slate-700">Trainer sesiune</label>
                    <?php if ($coachLocked) : ?>
                        <input type="hidden" name="coach_id" value="<?= esc((string) $values['coach_id']) ?>">
                        <div class="surface-muted px-4 py-3 text-sm font-medium text-slate-900">
                            <?= esc($coachOptions[0]['full_name'] ?? '-') ?>
                        </div>
                    <?php else : ?>
                        <select name="coach_id" class="form-select">
                            <option value="">Fara trainer dedicat</option>
                            <?php foreach ($coachOptions as $coach) : ?>
                                <option value="<?= esc((string) $coach['id']) ?>" <?= (string) $values['coach_id'] === (string) $coach['id'] ? 'selected' : '' ?>>
                                    <?= esc($coach['full_name']) ?>
                                </option>
                            <?php endforeach; ?>
                        </select>
                    <?php endif; ?>
                    <?php if (! empty($errors['coach_id'])) : ?><p class="field-error"><?= esc($errors['coach_id']) ?></p><?php endif; ?>
                </div>

                <div>
                    <label class="mb-2 block text-sm font-medium text-slate-700">Data</label>
                    <input type="date" name="session_date" class="form-input" value="<?= esc((string) $values['session_date']) ?>">
                    <?php if (! empty($errors['session_date'])) : ?><p class="field-error"><?= esc($errors['session_date']) ?></p><?php endif; ?>
                </div>

                <div>
                    <label class="mb-2 block text-sm font-medium text-slate-700">Ora start</label>
                    <input type="time" name="start_time" class="form-input" value="<?= esc((string) $values['start_time']) ?>">
                    <?php if (! empty($errors['start_time'])) : ?><p class="field-error"><?= esc($errors['start_time']) ?></p><?php endif; ?>
                </div>

                <div>
                    <label class="mb-2 block text-sm font-medium text-slate-700">Ora final</label>
                    <input type="time" name="end_time" class="form-input" value="<?= esc((string) $values['end_time']) ?>">
                    <?php if (! empty($errors['end_time'])) : ?><p class="field-error"><?= esc($errors['end_time']) ?></p><?php endif; ?>
                </div>

                <div>
                    <label class="mb-2 block text-sm font-medium text-slate-700">Status sesiune</label>
                    <select name="status" class="form-select" x-model="status">
                        <?php foreach ($statusOptions as $key => $label) : ?>
                            <option value="<?= esc($key) ?>" <?= (string) $values['status'] === (string) $key ? 'selected' : '' ?>><?= esc($label) ?></option>
                        <?php endforeach; ?>
                    </select>
                    <?php if (! empty($errors['status'])) : ?><p class="field-error"><?= esc($errors['status']) ?></p><?php endif; ?>
                </div>
            </div>

            <div class="mt-6 grid gap-5 lg:grid-cols-[1fr_1fr]">
                <div x-show="status === 'cancelled'" x-cloak>
                    <label class="mb-2 block text-sm font-medium text-slate-700">Motiv anulare</label>
                    <textarea name="cancelled_reason" rows="4" class="form-input"><?= esc((string) $values['cancelled_reason']) ?></textarea>
                    <?php if (! empty($errors['cancelled_reason'])) : ?><p class="field-error"><?= esc($errors['cancelled_reason']) ?></p><?php endif; ?>
                </div>

                <div x-show="status === 'recovery'" x-cloak>
                    <label class="mb-2 block text-sm font-medium text-slate-700">Sesiune anulata recuperata</label>
                    <select name="recovery_for_session_id" class="form-select">
                        <option value="">Selecteaza sedinta sursa</option>
                        <?php foreach ($recoveryOptions as $session) : ?>
                            <option value="<?= esc((string) $session['id']) ?>" <?= (string) $values['recovery_for_session_id'] === (string) $session['id'] ? 'selected' : '' ?>>
                                <?= esc($session['session_date'] . ' / ' . $session['group_name']) ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                    <?php if (! empty($errors['recovery_for_session_id'])) : ?><p class="field-error"><?= esc($errors['recovery_for_session_id']) ?></p><?php endif; ?>
                </div>
            </div>

            <div class="mt-6">
                <label class="mb-2 block text-sm font-medium text-slate-700">Note</label>
                <textarea name="notes" rows="4" class="form-input"><?= esc((string) $values['notes']) ?></textarea>
                <?php if (! empty($errors['notes'])) : ?><p class="field-error"><?= esc($errors['notes']) ?></p><?php endif; ?>
            </div>

            <div class="mt-6 flex flex-wrap gap-3">
                <button type="submit" class="btn-primary">Salveaza sesiunea</button>
                <a href="<?= esc($cancelUrl) ?>" class="btn-secondary">Renunta</a>
            </div>
        </form>
    </div>
<?= $this->endSection() ?>
