<main id="main-content">
    <div class="thesis-add-header">
        <h1>Ajouter un TFE</h1>
    </div>

    <p class="required-note"><span class="asterisk">*</span> Champs obligatoires</p>
    <form action="actions/formulaire.php" method="post" enctype="multipart/form-data" class="admin-form">
        <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($_SESSION["csrf_token"]) ?>">

        <!-- ═══════════════════ Informations du TFE ═══════════════════ -->
        <fieldset>
            <legend>Informations du TFE</legend>

            <?php $name = 'titre';     $label = 'Titre :';                          $value = old('titre');     $required = true;  $attrs = withAutofocus('titre');     include APP_ROOT . '/templates/partials/form/text-field.php'; ?>
            <?php $name = 'subtitle';  $label = 'Sous-titre (si applicable) :';     $value = old('subtitle');  $required = false; include APP_ROOT . '/templates/partials/form/text-field.php'; ?>
            <?php $name = 'auteurice'; $label = 'Auteur·ice(s) :';                  $value = old('auteurice'); $required = true;  $attrs = withAutofocus('auteurice', ['autocomplete' => 'name']); include APP_ROOT . '/templates/partials/form/text-field.php'; ?>
            <?php $name = 'mail';      $label = 'Contact(s) (optionnel) [mail/site/insta/etc.] :'; $value = old('mail'); $attrs = ['autocomplete' => 'email']; include APP_ROOT . '/templates/partials/form/text-field.php'; ?>

            <div class="admin-form-group">
                <label class="admin-checkbox-label">
                    <input type="checkbox" name="contact_public" value="1"
                           <?= isset($formData['contact_public']) ? 'checked' : '' ?>>
                    Je veux que mon contact soit accessible à toustes depuis la plateforme xamxam
                </label>
                <small>Si cette case est cochée, votre contact apparaîtra sur la page publique de votre TFE.</small>
            </div>

            <div>
                <label for="synopsis">Synopsis :</label>
                <textarea id="synopsis" name="synopsis"
                          rows="7" required
                          <?= $autofocusField === 'synopsis' ? 'autofocus' : '' ?>><?= old('synopsis') ?></textarea>
            </div>
        </fieldset>

        <!-- ═══════════════════ Composition du jury ═══════════════════ -->
        <?php require APP_ROOT . '/templates/partials/form/jury-fieldset.php'; ?>

        <!-- ═══════════════════ Cadre académique ═══════════════════ -->
        <fieldset>
            <legend>Cadre académique</legend>

            <?php
                $name  = 'année'; $label = 'Année :'; $value = old('année'); $required = true;
                $type  = 'number';
                $placeholder = date('Y');
                $attrs = withAutofocus('année', ['min' => 2000, 'max' => date('Y') + 1]);
                include APP_ROOT . '/templates/partials/form/text-field.php';
            ?>

            <?php $name = 'orientation'; $label = 'Orientation :';             $options = $orientations;    $selected = $formData['orientation'] ?? ''; $required = true; $placeholder = ''; $attrs = withAutofocus('orientation'); include APP_ROOT . '/templates/partials/form/select-field.php'; ?>
            <?php $name = 'ap';          $label = 'Atelier pluridisciplinaire :'; $options = $apPrograms;    $selected = $formData['ap'] ?? '';            $required = true; $placeholder = ''; $attrs = withAutofocus('ap');          include APP_ROOT . '/templates/partials/form/select-field.php'; ?>
            <?php $name = 'finality';   $label = 'Finalité du master :';       $options = $finalityTypes;   $selected = $formData['finality'] ?? '';    $required = true; $placeholder = ''; $attrs = withAutofocus('finality');    include APP_ROOT . '/templates/partials/form/select-field.php'; ?>

            <?php $name = 'languages'; $label = 'Langue(s) :'; $options = $languages; $checked = $formData['languages'] ?? []; $required = true; include APP_ROOT . '/templates/partials/form/checkbox-list.php'; ?>
            <?php $name = 'formats';   $label = 'Format(s) :'; $options = $formatTypes; $checked = $formData['formats'] ?? [];   include APP_ROOT . '/templates/partials/form/checkbox-list.php'; ?>

            <?php $name = 'tag'; $label = 'Mots-clés :'; $value = old('tag'); $placeholder = 'sociologie, anthropologie, ...'; $hint = 'Séparez par des virgules. Max 10 mots-clés.'; $attrs = withAutofocus('tag'); include APP_ROOT . '/templates/partials/form/text-field.php'; ?>
        </fieldset>

        <!-- ═══════════════════ Fichiers ═══════════════════ -->
        <fieldset>
            <legend>Fichiers</legend>

            <?php $name = 'couverture'; $label = 'Image de couverture :';       $accept = 'image/jpeg,image/png';          $hint = 'JPG, PNG. Taille max : 10 MB.';  include APP_ROOT . '/templates/partials/form/file-field.php'; ?>
            <?php $name = 'banner';     $label = 'Image bannière (accueil) :';  $accept = 'image/jpeg,image/png,image/webp'; $hint = 'JPG, PNG ou WEBP. Format paysage recommandé (4:1). Max 5 MB.'; include APP_ROOT . '/templates/partials/form/file-field.php'; ?>
            <?php $name = 'files';      $label = 'Fichiers du TFE :';           $accept = '.pdf,.jpg,.jpeg,.png,.mp4,.zip,.vtt'; $hint = 'PDF, JPG, PNG, MP4, ZIP. Max 50 MB par fichier. Pour les vidéos, un fichier .vtt de sous-titres peut être joint (il sera associé automatiquement à la vidéo correspondante).'; $multiple = true; include APP_ROOT . '/templates/partials/form/file-field.php'; ?>
        </fieldset>

        <!-- ═══════════════════ Métadonnées complémentaires ═══════════════════ -->
        <fieldset>
            <legend>Métadonnées complémentaires</legend>

            <?php $name = 'license_id';    $label = 'Licence :';    $options = $licenseTypes; $selected = $formData['license_id'] ?? '';    $placeholder = '— Inconnue —'; include APP_ROOT . '/templates/partials/form/select-field.php'; ?>

            <?php $name = 'duration_info'; $label = 'Durée / Taille :'; $value = old('duration_info'); $placeholder = 'Ex : 84 pages'; $hint = 'Durée (minutes) ou nombre de pages.'; include APP_ROOT . '/templates/partials/form/text-field.php'; ?>

            <?php $name = 'lien'; $label = 'Lien (site / ressource) :'; $value = old('lien'); $type = 'url'; $placeholder = 'https://...'; $attrs = withAutofocus('lien'); include APP_ROOT . '/templates/partials/form/text-field.php'; ?>

            <?php
                $accessOptions = array_map(function($at) {
                    return ['id' => $at['id'], 'name' => $at['name']];
                }, $enabledAccessTypes);
                $defaultAccessType    = 2;
                $selectedAccessType   = isset($formData['access_type_id'])
                    ? (int)$formData['access_type_id']
                    : $defaultAccessType;
                $name      = 'access_type_id';
                $label     = 'Visibilité / Accès :';
                $options   = $accessOptions;
                $selected  = $selectedAccessType;
                $placeholder = null;
                $required  = true;
                $attrs     = [];
                include APP_ROOT . '/templates/partials/form/select-field.php';
            ?>
        </fieldset>

        <!-- ═══════════════════ E-mail de confirmation ═══════════ -->
        <fieldset>
            <legend>E-mail de confirmation</legend>
            <?php $name = 'confirmation_email'; $label = 'Adresse e-mail * :'; $value = old('confirmation_email'); $type = 'email'; $required = true; $placeholder = 'ton.email@exemple.be'; $hint = 'Nécessaire pour recevoir le récapitulatif de ta soumission.'; $attrs = withAutofocus('confirmation_email'); include APP_ROOT . '/templates/partials/form/text-field.php'; ?>
        </fieldset>

        <div class="form-footer">
            <button type="submit" name="go">Soumettre</button>
        </div>
    </form>
</main>
