<?php
session_start();
require_once 'config.php';
require_once 'functions.php';

$msg = ''; $error = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['send_contact'])) {
    $name    = trim(htmlspecialchars($_POST['name']    ?? ''));
    $email   = trim(htmlspecialchars($_POST['email']   ?? ''));
    $type    = htmlspecialchars($_POST['type']    ?? 'general');
    $message = trim(htmlspecialchars($_POST['message'] ?? ''));

    if (strlen($message) < 10) { $error = "Message trop court."; }
    elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) { $error = "Courriel invalide."; }
    else {
        $body = "<strong>De :</strong> {$name} ({$email})<br><strong>Type :</strong> {$type}<br><br>{$message}";
        notifyUser('admin@jetegageque.com', "[Contact JTG] {$type} — {$name}", $body);
        $msg = "Ton message a été envoyé. On te répondra bientôt !";
    }
}

include 'header.php';
?>

<div class="container mt-4">
    <div class="row justify-content-center">
        <div class="col-md-6">
            <h5 class="oracle-text text-primary mb-4">✉️ CONTACT</h5>

            <?php if ($msg):   ?><div class="alert alert-success small py-2"><?= $msg ?></div><?php endif; ?>
            <?php if ($error): ?><div class="alert alert-danger  small py-2"><?= $error ?></div><?php endif; ?>

            <div class="card bg-dark border-secondary shadow">
                <div class="card-body p-4">
                    <p class="text-muted small mb-3">
                        Ce formulaire est géré par JeTeGageQue.com. Le site n'est pas responsable de la gestion de la boutique tierce.
                    </p>
                    <form method="POST">
                        <div class="mb-3">
                            <label class="small text-white fw-bold d-block mb-1">Nom</label>
                            <input type="text" name="name" class="form-control form-control-sm" placeholder="Ton nom" required>
                        </div>
                        <div class="mb-3">
                            <label class="small text-white fw-bold d-block mb-1">Courriel</label>
                            <input type="email" name="email" class="form-control form-control-sm" placeholder="ton@email.com" required>
                        </div>
                        <div class="mb-3">
                            <label class="small text-white fw-bold d-block mb-1">Type de message</label>
                            <select name="type" class="form-select form-select-sm">
                                <option value="plainte">Plainte</option>
                                <option value="general" selected>Commentaire général</option>
                                <option value="conseil">Conseil</option>
                                <option value="suggestion">Suggestion</option>
                            </select>
                        </div>
                        <div class="mb-3">
                            <label class="small text-white fw-bold d-block mb-1">Message</label>
                            <textarea name="message" class="form-control form-control-sm" rows="5" placeholder="Ton message…" required></textarea>
                        </div>
                        <button type="submit" name="send_contact" class="btn btn-primary w-100 fw-bold">ENVOYER</button>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

<?php include 'footer.php'; ?>
