<?php
$value = static fn (string $key, string $default = ''): string => htmlspecialchars(
    (string)($settings[$key] ?? $default),
    ENT_QUOTES,
    'UTF-8'
);
$isActive = (int)($settings['actif'] ?? 0) === 1;
$passwordConfigured = !empty($settings['password_configured']);
?>

<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>Configuration e-mail / SMTP
        </h1>
        <p class="text-muted mb-0">
            Paramétrer le serveur utilisé pour envoyer les devis et les factures.
        </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>

<?php if (!empty($errors['general'])): ?>
    <div class="alert alert-danger">
        <?= htmlspecialchars((string)$errors['general']) ?>
    </div>
<?php endif; ?>

<?php if (!$phpMailerAvailable): ?>
    <div class="alert alert-warning">
        <i class="bi bi-exclamation-triangle me-1"></i>
        PHPMailer n’est pas encore installé. Exécutez <code>composer install</code>
        après avoir installé ce package.
    </div>
<?php endif; ?>

<form method="post" action="index.php?page=parametres_mail_save" class="darlink-form-card mb-4">
    <div class="darlink-form-header">
        <h2 class="darlink-form-title">Serveur SMTP</h2>
    </div>

    <div class="darlink-form-body">
        <div class="row g-4">
            <div class="col-md-8 darlink-form-group">
                <label class="darlink-label" for="smtp_host">Serveur SMTP *</label>
                <input class="darlink-input" id="smtp_host" name="smtp_host" required
                       value="<?= $value('smtp_host') ?>" placeholder="smtp.exemple.com">
            </div>

            <div class="col-md-4 darlink-form-group">
                <label class="darlink-label" for="smtp_port">Port *</label>
                <input class="darlink-input" type="number" min="1" max="65535"
                       id="smtp_port" name="smtp_port" required value="<?= $value('smtp_port', '587') ?>">
            </div>

            <div class="col-md-4 darlink-form-group">
                <label class="darlink-label" for="smtp_secure">Chiffrement *</label>
                <select class="darlink-select" id="smtp_secure" name="smtp_secure" required>
                    <?php foreach (['tls' => 'TLS / STARTTLS', 'ssl' => 'SSL / SMTPS', 'none' => 'Aucun'] as $key => $label): ?>
                        <option value="<?= $key ?>" <?= $value('smtp_secure', 'tls') === $key ? 'selected' : '' ?>>
                            <?= htmlspecialchars($label) ?>
                        </option>
                    <?php endforeach; ?>
                </select>
            </div>

            <div class="col-md-8 darlink-form-group">
                <label class="darlink-label" for="smtp_username">Identifiant SMTP *</label>
                <input class="darlink-input" id="smtp_username" name="smtp_username" required
                       autocomplete="username" value="<?= $value('smtp_username') ?>">
            </div>

            <div class="col-md-6 darlink-form-group">
                <label class="darlink-label" for="smtp_password">Mot de passe SMTP</label>
                <input class="darlink-input" type="password" id="smtp_password" name="smtp_password"
                       autocomplete="new-password" placeholder="<?= $passwordConfigured ? 'Laisser vide pour conserver le mot de passe' : 'Mot de passe obligatoire' ?>">
                <div class="form-text">
                    <?= $passwordConfigured ? 'Un mot de passe est déjà enregistré et chiffré.' : 'Aucun mot de passe enregistré.' ?>
                </div>
            </div>

            <div class="col-md-6 darlink-form-group">
                <label class="darlink-label" for="smtp_from_name">Nom de l’expéditeur *</label>
                <input class="darlink-input" id="smtp_from_name" name="smtp_from_name" required
                       value="<?= $value('smtp_from_name') ?>" placeholder="Darlink ERP">
            </div>

            <div class="col-md-6 darlink-form-group">
                <label class="darlink-label" for="smtp_from_email">Adresse d’expédition *</label>
                <input class="darlink-input" type="email" id="smtp_from_email" name="smtp_from_email" required
                       value="<?= $value('smtp_from_email') ?>">
            </div>

            <div class="col-md-6 darlink-form-group">
                <label class="darlink-label" for="smtp_reply_to">Adresse de réponse</label>
                <input class="darlink-input" type="email" id="smtp_reply_to" name="smtp_reply_to"
                       value="<?= $value('smtp_reply_to') ?>">
            </div>

            <div class="col-md-6 darlink-form-group">
                <label class="darlink-label" for="smtp_test_email">Adresse de test</label>
                <input class="darlink-input" type="email" id="smtp_test_email" name="smtp_test_email"
                       value="<?= $value('smtp_test_email') ?>">
            </div>

            <div class="col-md-6 darlink-form-group">
                <label class="darlink-label">État</label>
                <div class="darlink-switch-card">
                    <div>
                        <div class="fw-semibold">Messagerie active</div>
                        <div class="small text-muted">Autoriser les envois depuis Darlink ERP.</div>
                    </div>
                    <div class="form-check form-switch m-0">
                        <input class="form-check-input" type="checkbox" name="actif" value="1"
                               id="mailActive" <?= $isActive ? 'checked' : '' ?>>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="darlink-form-actions">
        <a href="index.php?page=parametres" class="btn btn-outline-secondary">Annuler</a>
        <button type="submit" class="btn darlink-btn-primary">
            <i class="bi bi-check-lg me-1"></i>Enregistrer
        </button>
    </div>
</form>

<div class="darlink-card overflow-hidden">
    <div class="darlink-card-header">Tester la messagerie</div>
    <div class="darlink-card-body">
        <form method="post" action="index.php?page=parametres_mail_test" class="row g-3 align-items-end">
            <div class="col-md-8">
                <label class="darlink-label" for="test_email">Destinataire du test</label>
                <input class="darlink-input" type="email" id="test_email" name="test_email"
                       value="<?= $value('smtp_test_email') ?>" required>
            </div>
            <div class="col-md-4">
                <button class="btn btn-outline-primary w-100" type="submit" <?= !$phpMailerAvailable ? 'disabled' : '' ?>>
                    <i class="bi bi-send me-1"></i>Envoyer un e-mail de test
                </button>
            </div>
        </form>
    </div>
</div>
