<div class="d-flex justify-content-between align-items-center mb-4">
    <div>
        <h1 class="darlink-title mb-1">
            <i class="bi bi-envelope-gear me-2"></i>Modèles d’e-mails
        </h1>
        <p class="text-muted mb-0">
            Personnalisez les textes utilisés lors de l’envoi des documents.
        </p>
    </div>
    <a href="index.php?page=parametres" class="btn btn-outline-secondary">
        <i class="bi bi-arrow-left me-1"></i> Retour
    </a>
</div>

<div class="row g-4">
    <?php foreach ($templates as $template): ?>
        <div class="col-xl-6">
            <form method="post" action="index.php?page=parametres_mail_templates_save"
                  class="darlink-form-card h-100">
                <input type="hidden" name="type_document"
                       value="<?= htmlspecialchars((string)$template['type_document']) ?>">

                <div class="darlink-form-header">
                    <h2 class="darlink-form-title h5 mb-0">
                        <?= htmlspecialchars((string)$template['type_document']) ?>
                    </h2>
                </div>

                <div class="darlink-form-body">
                    <div class="darlink-form-group">
                        <label class="darlink-label">Nom du modèle</label>
                        <input class="darlink-input" name="nom"
                               value="<?= htmlspecialchars((string)$template['nom']) ?>" required>
                    </div>

                    <div class="darlink-form-group">
                        <label class="darlink-label">Objet</label>
                        <input class="darlink-input" name="objet"
                               value="<?= htmlspecialchars((string)$template['objet']) ?>" required>
                    </div>

                    <div class="darlink-form-group">
                        <label class="darlink-label">Message</label>
                        <textarea class="darlink-textarea" name="contenu_html"
                                  rows="10" required><?= htmlspecialchars((string)$template['contenu_html']) ?></textarea>
                    </div>

                    <div class="form-check">
                        <input class="form-check-input" type="checkbox" name="actif"
                               value="1" id="active<?= (int)$template['id'] ?>"
                               <?= (int)$template['actif'] === 1 ? 'checked' : '' ?>>
                        <label class="form-check-label" for="active<?= (int)$template['id'] ?>">
                            Modèle actif
                        </label>
                    </div>

                    <div class="alert alert-light border mt-3 mb-0 small">
                        Variables disponibles : <code>{NUMERO}</code>,
                        <code>{CLIENT}</code>, <code>{SOCIETE}</code>.
                    </div>
                </div>

                <div class="darlink-form-actions">
                    <button type="submit" class="btn darlink-btn-primary">
                        <i class="bi bi-check2 me-1"></i> Enregistrer
                    </button>
                </div>
            </form>
        </div>
    <?php endforeach; ?>
</div>
