<?php
use App\Core\Csrf;

$errors = $_SESSION['form_errors'] ?? [];
$old = $_SESSION['old'] ?? [];
unset($_SESSION['form_errors'], $_SESSION['old']);

function group_old(string $key, string $default = ''): string
{
    return htmlspecialchars((string)($GLOBALS['old'][$key] ?? $default));
}
?>
<link rel="stylesheet" href="/public/assets/css/cda-v4-8-workshops.css">

<section class="page-intro">
    <div>
        <span class="page-kicker">Organisation pédagogique</span>
        <h2>Nouveau groupe ou créneau</h2>
        <p>
            Affectez un atelier à un professeur, une salle et un planning.
        </p>
    </div>

    <a class="button button-secondary" href="/public/groupes">
        Retour aux groupes
    </a>
</section>

<?php if ($errors): ?>
<div class="alert alert-error">
    <ul>
        <?php foreach ($errors as $error): ?>
        <li><?= htmlspecialchars((string)$error) ?></li>
        <?php endforeach; ?>
    </ul>
</div>
<?php endif; ?>

<form method="post" action="/public/groupes" class="v4-form">
    <?= Csrf::field() ?>

    <section class="form-card">
        <header class="form-card-header">
            <div class="form-step">1</div>
            <div>
                <h3>Atelier et type d’organisation</h3>
                <p>
                    Choisissez le cours déjà créé.
                </p>
            </div>
            <div class="form-icon">🎨</div>
        </header>

        <div class="form-grid grid-4">
            <label class="field" style="grid-column: span 2">
                <span>Atelier *</span>
                <select required name="atelier_id">
                    <option value="">Choisir un atelier</option>

                    <?php foreach ($workshops as $workshop): ?>
                    <option value="<?= (int)$workshop['id'] ?>">
                        <?= htmlspecialchars(
                            trim(
                                (string)($workshop['discipline_libelle'] ?? '') .
                                ' — ' .
                                (string)$workshop['libelle'] .
                                (
                                    !empty($workshop['public_cible'])
                                    ? ' — ' . $workshop['public_cible']
                                    : ''
                                )
                            )
                        ) ?>
                    </option>
                    <?php endforeach; ?>
                </select>
            </label>

            <label class="field">
                <span>Type de groupe</span>
                <select name="type_groupe">
                    <option value="collectif">Collectif</option>
                    <option value="individuel">Créneaux individuels</option>
                    <option value="ponctuel">Ponctuel</option>
                </select>
            </label>

            <label class="field">
                <span>Capacité</span>
                <input
                    type="number"
                    min="1"
                    name="capacite_max"
                    value="<?= group_old('capacite_max') ?>"
                >
            </label>

            <label class="field">
                <span>Nom du groupe *</span>
                <input
                    required
                    name="libelle"
                    value="<?= group_old('libelle') ?>"
                    placeholder="Théâtre adultes du lundi"
                >
            </label>

            <label class="field">
                <span>Code interne</span>
                <input
                    required
                    name="code"
                    value="<?= group_old('code') ?>"
                    placeholder="GROUPE-01"
                >
            </label>

            <label class="field">
                <span>Durée d’un créneau individuel</span>
                <div class="input-with-unit">
                    <input
                        type="number"
                        min="5"
                        step="5"
                        name="duree_creneau_minutes"
                        value="<?= group_old('duree_creneau_minutes') ?>"
                        placeholder="30"
                    >
                    <span>minutes</span>
                </div>
            </label>
        </div>
    </section>

    <section class="form-card">
        <header class="form-card-header">
            <div class="form-step pink">2</div>
            <div>
                <h3>Professeur et lieu</h3>
                <p>
                    La première version du planning sera créée automatiquement.
                </p>
            </div>
            <div class="form-icon pink">👨‍🏫</div>
        </header>

        <div class="form-grid grid-2">
            <label class="field">
                <span>Professeur</span>
                <select name="professeur_id">
                    <option value="">Non affecté</option>

                    <?php foreach ($professors as $professor): ?>
                    <option value="<?= (int)$professor['id'] ?>">
                        <?= htmlspecialchars(
                            (string)$professor['nom'] . ' ' .
                            (string)$professor['prenom']
                        ) ?>
                    </option>
                    <?php endforeach; ?>
                </select>
            </label>

            <label class="field">
                <span>Salle ou studio</span>
                <select name="salle_id">
                    <option value="">Non affectée</option>

                    <?php foreach ($rooms as $room): ?>
                    <option value="<?= (int)$room['id'] ?>">
                        <?= htmlspecialchars((string)$room['libelle']) ?>
                    </option>
                    <?php endforeach; ?>
                </select>
            </label>
        </div>
    </section>

    <section class="form-card">
        <header class="form-card-header">
            <div class="form-step green">3</div>
            <div>
                <h3>Planning récurrent</h3>
                <p>
                    Les séances seront ensuite générées hors vacances scolaires.
                </p>
            </div>
            <div class="form-icon green">📅</div>
        </header>

        <div class="form-grid grid-4">
            <label class="field">
                <span>Jour</span>
                <select name="jour_semaine">
                    <?php foreach (
                        [
                            'lundi',
                            'mardi',
                            'mercredi',
                            'jeudi',
                            'vendredi',
                            'samedi',
                            'dimanche'
                        ] as $day
                    ): ?>
                    <option value="<?= $day ?>">
                        <?= ucfirst($day) ?>
                    </option>
                    <?php endforeach; ?>
                </select>
            </label>

            <label class="field">
                <span>Heure de début *</span>
                <input required type="time" name="heure_debut">
            </label>

            <label class="field">
                <span>Heure de fin *</span>
                <input required type="time" name="heure_fin">
            </label>

            <div class="schedule-summary">
                <span>Aperçu</span>
                <strong id="schedulePreview">Lundi · --:-- à --:--</strong>
            </div>

            <label class="field">
                <span>Début de la période *</span>
                <input required type="date" name="date_debut">
            </label>

            <label class="field">
                <span>Fin de la période *</span>
                <input required type="date" name="date_fin">
            </label>
        </div>
    </section>

    <div class="sticky-actions">
        <a class="button button-secondary" href="/public/groupes">
            Annuler
        </a>

        <button class="button button-primary" type="submit">
            Créer le groupe et son planning
        </button>
    </div>
</form>

<script src="/public/assets/js/group-form.js"></script>
