<?php
/**
 * Reusable partial for apropos contacts groups form.
 * Expected variables:
 *   $aproposKey  string  'contacts'
 *   $groups      array   Existing groups data
 */
?>
<form action="/admin/actions/apropos.php" method="post" class="admin-form" id="apropos-form-<?= $aproposKey ?>">
    <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($_SESSION['csrf_token']) ?>">
    <input type="hidden" name="apropos_key" value="<?= htmlspecialchars($aproposKey) ?>">

    <?php foreach ($groups as $gi => $group): ?>
    <fieldset class="apropos-group">
        <legend>Contact <?= $gi + 1 ?></legend>
        <label for="group_f_<?= $aproposKey ?>_<?= $gi ?>_role">Rôle :</label>
        <input type="text" id="group_f_<?= $aproposKey ?>_<?= $gi ?>_role"
               name="groups[<?= $gi ?>][role]"
               value="<?= htmlspecialchars($group['role'] ?? '') ?>">

        <?php $entries = is_array($group['entries'] ?? null) ? $group['entries'] : []; ?>
        <?php foreach ($entries as $ei => $entry): ?>
        <div class="apropos-entry">
            <label for="entry_f_<?= $aproposKey ?>_<?= $gi ?>_<?= $ei ?>_text">Nom :</label>
            <input type="text" id="entry_f_<?= $aproposKey ?>_<?= $gi ?>_<?= $ei ?>_text"
                   name="groups[<?= $gi ?>][entries][<?= $ei ?>][text]"
                   value="<?= htmlspecialchars($entry['text'] ?? '') ?>">
            <label for="entry_f_<?= $aproposKey ?>_<?= $gi ?>_<?= $ei ?>_email">Email :</label>
            <input type="email" id="entry_f_<?= $aproposKey ?>_<?= $gi ?>_<?= $ei ?>_email"
                   name="groups[<?= $gi ?>][entries][<?= $ei ?>][email]"
                   value="<?= htmlspecialchars($entry['email'] ?? '') ?>">
            <label for="entry_f_<?= $aproposKey ?>_<?= $gi ?>_<?= $ei ?>_url">Lien (optionnel) :</label>
            <input type="url" id="entry_f_<?= $aproposKey ?>_<?= $gi ?>_<?= $ei ?>_url"
                   name="groups[<?= $gi ?>][entries][<?= $ei ?>][url]"
                   value="<?= htmlspecialchars($entry['url'] ?? '') ?>">
        </div>
        <?php endforeach; ?>

        <button type="button" class="btn btn--primary btn--sm add-entry-btn-f"
                data-group="<?= $gi ?>"
                data-key="<?= $aproposKey ?>">+ Ajouter une entrée</button>
    </fieldset>
    <?php endforeach; ?>

    <button type="button" class="btn btn--primary add-group-btn-f"
            data-key="<?= $aproposKey ?>">+ Ajouter un contact</button>

    <div class="admin-form-footer">
        <button type="submit" class="btn btn--primary">Enregistrer</button>
        <a href="/admin/contenus.php" class="btn btn--secondary admin-cancel-link">Annuler</a>
    </div>

    <template id="entry-template-f-<?= $aproposKey ?>">
        <div class="apropos-entry">
            <label>Entrée :</label>
            <input type="text" name="groups[{{gi}}][entries][{{ei}}][text]">
            <label>Email :</label>
            <input type="email" name="groups[{{gi}}][entries][{{ei}}][email]">
            <label>Lien (optionnel) :</label>
            <input type="url" name="groups[{{gi}}][entries][{{ei}}][url]">
        </div>
    </template>

    <template id="group-template-f-<?= $aproposKey ?>">
        <fieldset class="apropos-group">
            <legend>Contact {{gi}}</legend>
            <label>Rôle :</label>
            <input type="text" name="groups[{{gi}}][role]">
            <button type="button" class="btn btn--primary btn--sm add-entry-btn-f"
                    data-group="{{gi}}"
                    data-key="<?= $aproposKey ?>">+ Ajouter une entrée</button>
        </fieldset>
    </template>
</form>

<script>
(function() {
    var key = '<?= $aproposKey ?>';
    var form = document.getElementById('apropos-form-' + key);
    var groupCount = <?= count($groups) ?>;
    var entryTpl = document.getElementById('entry-template-f-' + key).innerHTML;
    var groupTpl = document.getElementById('group-template-f-' + key).innerHTML;

    form.querySelectorAll('.add-entry-btn-f').forEach(function(btn) {
        btn.addEventListener('click', function() {
            var gi = parseInt(this.dataset.group);
            var fieldset = this.closest('fieldset');
            var entryCount = fieldset.querySelectorAll('.apropos-entry').length;
            var html = entryTpl.replaceAll('{{gi}}', gi).replaceAll('{{ei}}', entryCount);
            this.insertAdjacentHTML('beforebegin', html);
        });
    });

    form.querySelector('.add-group-btn-f').addEventListener('click', function() {
        groupCount++;
        var html = groupTpl.replaceAll('{{gi}}', groupCount);
        this.insertAdjacentHTML('beforebegin', html);

        var newGroup = this.previousElementSibling;
        if (newGroup && newGroup.classList.contains('apropos-group')) {
            var btn = newGroup.querySelector('.add-entry-btn-f');
            if (btn) {
                btn.dataset.group = groupCount;
                btn.addEventListener('click', function() {
                    var gi = parseInt(this.dataset.group);
                    var fieldset = this.closest('fieldset');
                    var entryCount = fieldset.querySelectorAll('.apropos-entry').length;
                    var html = entryTpl.replaceAll('{{gi}}', gi).replaceAll('{{ei}}', entryCount);
                    this.insertAdjacentHTML('beforebegin', html);
                });
            }
        }
    });
})();
</script>
