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

<?= $this->section('content') ?>
    <div class="space-y-6">
        <section class="page-header">
            <div>
                <p class="page-kicker">Niveluri inot</p>
                <h2 class="section-title text-2xl"><?= esc($pageTitle) ?></h2>
                <p class="section-copy mt-2 max-w-3xl"><?= esc($pageDescription) ?></p>
            </div>
        </section>

        <form method="post" action="<?= esc($submitUrl) ?>" class="surface-card p-6">
            <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">Nume nivel</label>
                    <input type="text" name="name" class="form-input" value="<?= esc((string) $values['name']) ?>">
                    <?php if (! empty($errors['name'])) : ?><p class="field-error"><?= esc($errors['name']) ?></p><?php endif; ?>
                </div>
                <div>
                    <label class="mb-2 block text-sm font-medium text-slate-700">Slug</label>
                    <input type="text" name="slug" class="form-input" value="<?= esc((string) $values['slug']) ?>">
                    <?php if (! empty($errors['slug'])) : ?><p class="field-error"><?= esc($errors['slug']) ?></p><?php endif; ?>
                </div>
                <div>
                    <label class="mb-2 block text-sm font-medium text-slate-700">Ordine</label>
                    <input type="number" name="sort_order" class="form-input" value="<?= esc((string) $values['sort_order']) ?>" min="1">
                    <?php if (! empty($errors['sort_order'])) : ?><p class="field-error"><?= esc($errors['sort_order']) ?></p><?php endif; ?>
                </div>
                <div>
                    <label class="mb-2 block text-sm font-medium text-slate-700">Culoare</label>
                    <input type="text" name="color_hex" class="form-input" value="<?= esc((string) $values['color_hex']) ?>" placeholder="#4f46e5">
                    <?php if (! empty($errors['color_hex'])) : ?><p class="field-error"><?= esc($errors['color_hex']) ?></p><?php endif; ?>
                </div>
                <div>
                    <label class="mb-2 block text-sm font-medium text-slate-700">Benchmark minim</label>
                    <input type="text" name="benchmark_minimum" class="form-input" value="<?= esc((string) $values['benchmark_minimum']) ?>">
                    <?php if (! empty($errors['benchmark_minimum'])) : ?><p class="field-error"><?= esc($errors['benchmark_minimum']) ?></p><?php endif; ?>
                </div>
                <div>
                    <label class="mb-2 block text-sm font-medium text-slate-700">Benchmark target</label>
                    <input type="text" name="benchmark_target" class="form-input" value="<?= esc((string) $values['benchmark_target']) ?>">
                    <?php if (! empty($errors['benchmark_target'])) : ?><p class="field-error"><?= esc($errors['benchmark_target']) ?></p><?php endif; ?>
                </div>
            </div>
            <div class="mt-5 grid gap-5 xl:grid-cols-2">
                <div>
                    <label class="mb-2 block text-sm font-medium text-slate-700">Descriere interna</label>
                    <textarea name="internal_description" rows="5" class="form-input"><?= esc((string) $values['internal_description']) ?></textarea>
                    <?php if (! empty($errors['internal_description'])) : ?><p class="field-error"><?= esc($errors['internal_description']) ?></p><?php endif; ?>
                </div>
                <div>
                    <label class="mb-2 block text-sm font-medium text-slate-700">Descriere pentru parinte</label>
                    <textarea name="parent_description" rows="5" class="form-input"><?= esc((string) $values['parent_description']) ?></textarea>
                    <?php if (! empty($errors['parent_description'])) : ?><p class="field-error"><?= esc($errors['parent_description']) ?></p><?php endif; ?>
                </div>
            </div>
            <div class="mt-5">
                <label class="mb-2 block text-sm font-medium text-slate-700">Reguli promovare</label>
                <textarea name="promotion_rules" rows="4" class="form-input"><?= esc((string) $values['promotion_rules']) ?></textarea>
                <?php if (! empty($errors['promotion_rules'])) : ?><p class="field-error"><?= esc($errors['promotion_rules']) ?></p><?php endif; ?>
            </div>

            <section class="mt-8 border-t border-slate-200 pt-6">
                <div class="flex flex-col gap-2 lg:flex-row lg:items-end lg:justify-between">
                    <div>
                        <h3 class="section-title">Intrebari si punctaje</h3>
                        <p class="section-copy mt-2 max-w-3xl">Criteriile de mai jos sunt folosite in formularele Quick Check si Full Evaluation pentru acest nivel. Stergerea scoate intrebarea din nivelul curent, fara sa afecteze istoricul evaluarilor vechi.</p>
                    </div>
                    <div class="rounded-2xl bg-sky-50 px-4 py-3 text-sm font-medium text-sky-700">Scara recomandata: 1 - 5</div>
                </div>

                <div class="mt-5 space-y-4">
                    <?php foreach ($criteriaRows as $index => $criterion) : ?>
                        <?php
                        $isNew = (string) ($criterion['id'] ?? '') === '';
                        $rowTitle = $isNew ? 'Intrebare noua' : (string) ($criterion['name'] ?: 'Intrebare');
                        $selectedTypes = (array) ($criterion['types'] ?? []);
                        ?>
                        <article class="surface-muted px-4 py-4">
                            <input type="hidden" name="criteria[<?= esc((string) $index) ?>][id]" value="<?= esc((string) ($criterion['id'] ?? '')) ?>">

                            <div class="mb-4 flex flex-wrap items-center justify-between gap-3">
                                <div>
                                    <p class="text-sm font-semibold text-slate-900"><?= esc($rowTitle) ?></p>
                                    <?php if ($isNew) : ?>
                                        <p class="mt-1 text-xs text-slate-500">Completeaza numele ca sa adaugi o intrebare noua.</p>
                                    <?php endif; ?>
                                </div>
                                <?php if (! $isNew) : ?>
                                    <label class="inline-flex items-center gap-2 rounded-xl bg-white px-3 py-2 text-sm font-semibold text-rose-600">
                                        <input type="checkbox" name="criteria[<?= esc((string) $index) ?>][delete]" value="1" class="rounded border-slate-300 text-rose-600 focus:ring-rose-500" <?= (string) ($criterion['delete'] ?? '0') === '1' ? 'checked' : '' ?>>
                                        Sterge din acest nivel
                                    </label>
                                <?php endif; ?>
                            </div>

                            <div class="grid gap-4 xl:grid-cols-[1.1fr_0.9fr_120px_120px]">
                                <div>
                                    <label class="mb-2 block text-sm font-medium text-slate-700">Intrebare / criteriu</label>
                                    <input type="text" name="criteria[<?= esc((string) $index) ?>][name]" class="form-input" value="<?= esc((string) ($criterion['name'] ?? '')) ?>" placeholder="ex. Tehnica bras">
                                    <?php if (! empty($errors['criteria_' . $index . '_name'])) : ?><p class="field-error"><?= esc($errors['criteria_' . $index . '_name']) ?></p><?php endif; ?>
                                </div>
                                <div>
                                    <label class="mb-2 block text-sm font-medium text-slate-700">Slug</label>
                                    <input type="text" name="criteria[<?= esc((string) $index) ?>][slug]" class="form-input" value="<?= esc((string) ($criterion['slug'] ?? '')) ?>" placeholder="generat automat daca ramane gol">
                                    <?php if (! empty($errors['criteria_' . $index . '_slug'])) : ?><p class="field-error"><?= esc($errors['criteria_' . $index . '_slug']) ?></p><?php endif; ?>
                                </div>
                                <div>
                                    <label class="mb-2 block text-sm font-medium text-slate-700">Pondere</label>
                                    <input type="text" name="criteria[<?= esc((string) $index) ?>][default_weight]" class="form-input" value="<?= esc((string) ($criterion['default_weight'] ?? '1.0')) ?>">
                                    <?php if (! empty($errors['criteria_' . $index . '_default_weight'])) : ?><p class="field-error"><?= esc($errors['criteria_' . $index . '_default_weight']) ?></p><?php endif; ?>
                                </div>
                                <div>
                                    <label class="mb-2 block text-sm font-medium text-slate-700">Ordine</label>
                                    <input type="number" name="criteria[<?= esc((string) $index) ?>][sort_order]" class="form-input" value="<?= esc((string) ($criterion['sort_order'] ?? '1')) ?>" min="1">
                                    <?php if (! empty($errors['criteria_' . $index . '_sort_order'])) : ?><p class="field-error"><?= esc($errors['criteria_' . $index . '_sort_order']) ?></p><?php endif; ?>
                                </div>
                            </div>

                            <div class="mt-4 grid gap-4 xl:grid-cols-[1fr_110px_110px_1fr]">
                                <div>
                                    <label class="mb-2 block text-sm font-medium text-slate-700">Descriere</label>
                                    <input type="text" name="criteria[<?= esc((string) $index) ?>][description]" class="form-input" value="<?= esc((string) ($criterion['description'] ?? '')) ?>" placeholder="Text afisat trainerului in evaluare">
                                </div>
                                <div>
                                    <label class="mb-2 block text-sm font-medium text-slate-700">Min</label>
                                    <input type="number" name="criteria[<?= esc((string) $index) ?>][min_score]" class="form-input" value="<?= esc((string) ($criterion['min_score'] ?? '1')) ?>" min="1" max="5">
                                </div>
                                <div>
                                    <label class="mb-2 block text-sm font-medium text-slate-700">Max</label>
                                    <input type="number" name="criteria[<?= esc((string) $index) ?>][max_score]" class="form-input" value="<?= esc((string) ($criterion['max_score'] ?? '5')) ?>" min="1" max="5">
                                </div>
                                <div>
                                    <label class="mb-2 block text-sm font-medium text-slate-700">Tip evaluare</label>
                                    <div class="flex flex-wrap gap-2">
                                        <?php foreach ($evaluationTypes as $type) : ?>
                                            <label class="inline-flex items-center gap-2 rounded-xl bg-white px-3 py-2 text-sm font-medium text-slate-700">
                                                <input type="checkbox" name="criteria[<?= esc((string) $index) ?>][types][]" value="<?= esc((string) $type['slug']) ?>" class="rounded border-slate-300 text-sky-600 focus:ring-sky-500" <?= in_array((string) $type['slug'], $selectedTypes, true) ? 'checked' : '' ?>>
                                                <?= esc($type['name']) ?>
                                            </label>
                                        <?php endforeach; ?>
                                    </div>
                                </div>
                            </div>

                            <div class="mt-4 flex flex-wrap gap-3">
                                <label class="inline-flex items-center gap-2 rounded-xl bg-white px-3 py-2 text-sm font-medium text-slate-700">
                                    <input type="checkbox" name="criteria[<?= esc((string) $index) ?>][is_active]" value="1" class="rounded border-slate-300 text-sky-600 focus:ring-sky-500" <?= (string) ($criterion['is_active'] ?? '1') === '1' ? 'checked' : '' ?>>
                                    Activ
                                </label>
                                <label class="inline-flex items-center gap-2 rounded-xl bg-white px-3 py-2 text-sm font-medium text-slate-700">
                                    <input type="checkbox" name="criteria[<?= esc((string) $index) ?>][is_critical]" value="1" class="rounded border-slate-300 text-rose-600 focus:ring-rose-500" <?= (string) ($criterion['is_critical'] ?? '0') === '1' ? 'checked' : '' ?>>
                                    Criteriu critic
                                </label>
                            </div>

                            <?php if (! empty($errors['criteria_' . $index . '_score_range'])) : ?><p class="field-error"><?= esc($errors['criteria_' . $index . '_score_range']) ?></p><?php endif; ?>
                            <?php if (! empty($errors['criteria_' . $index . '_types'])) : ?><p class="field-error"><?= esc($errors['criteria_' . $index . '_types']) ?></p><?php endif; ?>
                        </article>
                    <?php endforeach; ?>
                </div>
            </section>

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