<?php
/**
 * HTMX fragment: renders the licence section with conditional required states.
 *
 * POST: access_type_id, license_id, license_custom, cc2r, admin_mode
 *
 * When access_type_id=1 (Libre): licence fields are required.
 * When access_type_id=2|3 (Interne/Interdit): licence fields are optional.
 */
require_once __DIR__ . '/../../bootstrap.php';

$accessTypeId = $_POST['access_type_id'] ?? '2';
$licenseId    = $_POST['license_id']     ?? '';
$licenseCustom = $_POST['license_custom'] ?? '';
$cc2r         = !empty($_POST['cc2r']);
$adminMode    = ($_POST['admin_mode'] ?? '0') === '1';

$required = !$adminMode && $accessTypeId === '1';

require_once APP_ROOT . '/src/Database.php';
$db = Database::getInstance();
$licenseTypes = $db->getAllLicenseTypes();
?>

<div class="licence-license-choice">
    <?php
    $name = 'license_id'; $label = 'Licence :'; $options = $licenseTypes;
    $selected = $licenseId; $placeholder = '— Sélectionner —';
    include APP_ROOT . '/templates/partials/form/select-field.php';
    ?>

    <?php
    $name = 'license_custom'; $label = 'Ou précisez une autre licence :';
    $value = htmlspecialchars($licenseCustom);
    $hint = 'Ex: CC BY-NC 4.0, Tous droits réservés...';
    include APP_ROOT . '/templates/partials/form/text-field.php';
    ?>

    <div class="admin-form-group">
        <label class="admin-checkbox-label">
            <input type="checkbox" name="cc2r" value="1"
                   <?= $cc2r ? 'checked' : '' ?>>
            J'accepte les Conditions Collectives de Réutilisation (CC2r)
        </label>
        <small><a href="https://cc2r.net/" target="_blank" rel="noopener">En savoir plus sur la CC2r ↗</a></small>
    </div>
</div>
