<?php
/**
 * format-website-fragment.php (partage)
 *
 * HTMX fragment for the student share form: returns the website URL input fields
 * if "Site web" is among the currently selected format checkboxes.
 *
 * Regular PHP include inside partage/index.php routing — no separate bootstrap.
 * The parent partage/index.php already handles boot + session.
 *
 * Expected POST:
 *   - formats[]: array of selected format_type IDs
 *   - website_url:  current website_url value (for repopulation)
 *   - website_label: current website_label value (for repopulation)
 */

// Find the "Site web" format ID
$stmt = Database::getInstance()->getConnection()->prepare(
    'SELECT id FROM format_types WHERE name = ? LIMIT 1'
);
$stmt->execute(['Site web']);
$websiteFormatId = $stmt->fetchColumn();

if (!$websiteFormatId) {
    echo '';
    exit;
}

$selectedFormats = isset($_POST['formats']) && is_array($_POST['formats'])
    ? array_map('intval', $_POST['formats'])
    : [];

if (!in_array((int)$websiteFormatId, $selectedFormats, true)) {
    echo '';
    exit;
}

$websiteUrl   = htmlspecialchars($_POST['website_url']   ?? '');
$websiteLabel = htmlspecialchars($_POST['website_label'] ?? '');
?>
<fieldset id="website-url-fieldset">
    <legend>Site web</legend>

    <div class="admin-form-group">
        <label for="website_url">URL du site :</label>
        <div class="admin-file-input">
            <input type="url"
                   id="website_url"
                   name="website_url"
                   value="<?= $websiteUrl ?>"
                   placeholder="https://mon-tfe.erg.be">
            <small>Si le TFE est un site web, entrez son URL ici. Il sera affiché comme un site embarqué sur la page du TFE.</small>
        </div>
    </div>

    <div class="admin-form-group">
        <label for="website_label">Légende :</label>
        <input type="text"
               id="website_label"
               name="website_label"
               value="<?= $websiteLabel ?>"
               placeholder="Description du site (optionnel)"
               class="admin-file-label-input"
               style="max-width:400px;">
    </div>
</fieldset>
