<?php
require_once 'config.php';

$message = '';
$success = false;

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $nom = $_POST['nom'] ?? '';
    $email = $_POST['email'] ?? '';
    $telephone = $_POST['telephone'] ?? '';
    $sujet = $_POST['sujet'] ?? '';
    $message_contact = $_POST['message'] ?? '';
    
    if (empty($nom) || empty($email) || empty($sujet) || empty($message_contact)) {
        $message = '<div class="alert-error">Veuillez remplir tous les champs obligatoires.</div>';
    } elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $message = '<div class="alert-error">Email invalide.</div>';
    } else {
        try {
            $sql = "INSERT INTO contact (nom, email, telephone, sujet, message, statut) 
                    VALUES (:nom, :email, :telephone, :sujet, :message, 'non_lu')";
            $stmt = $pdo->prepare($sql);
            $stmt->execute([
                ':nom' => $nom,
                ':email' => $email,
                ':telephone' => $telephone,
                ':sujet' => $sujet,
                ':message' => $message_contact
            ]);
            $success = true;
            $message = '<div class="alert-success">✅ Votre message a été envoyé avec succès ! Nous vous répondrons dans les plus brefs délais.</div>';
        } catch(PDOException $e) {
            $message = '<div class="alert-error">❌ Erreur lors de l\'envoi du message.</div>';
        }
    }
}
?>
<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <title>Contact | CPN Formation</title>
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; font-family: 'Poppins', sans-serif; }
        body { background: #f5f7fa; padding: 50px 20px; }
        .contact-container { max-width: 600px; margin: 0 auto; background: white; border-radius: 20px; padding: 40px; box-shadow: 0 20px 40px rgba(0,0,0,0.1); }
        h1 { color: #1e293b; margin-bottom: 10px; }
        p { color: #64748b; margin-bottom: 30px; }
        .form-group { margin-bottom: 20px; }
        label { display: block; font-weight: 500; margin-bottom: 8px; color: #1e293b; }
        input, textarea, select { width: 100%; padding: 12px 15px; border: 2px solid #e2e8f0; border-radius: 12px; font-size: 14px; transition: all 0.3s; }
        input:focus, textarea:focus, select:focus { outline: none; border-color: #2f6d8c; }
        button { background: linear-gradient(135deg, #2f6d8c, #095175); color: white; border: none; padding: 14px 28px; border-radius: 12px; font-size: 16px; font-weight: 600; cursor: pointer; width: 100%; transition: all 0.3s; }
        button:hover { transform: translateY(-2px); box-shadow: 0 5px 20px rgba(47,109,140,0.3); }
        .alert-success { background: #d4edda; color: #155724; padding: 15px; border-radius: 12px; margin-bottom: 20px; border-left: 4px solid #28a745; }
        .alert-error { background: #f8d7da; color: #721c24; padding: 15px; border-radius: 12px; margin-bottom: 20px; border-left: 4px solid #dc3545; }
        .required { color: #dc3545; }
    </style>
</head>
<body>
    <div class="contact-container">
        <h1>📧 Nous contacter</h1>
        <p>Une question ? Un projet ? N'hésitez pas à nous écrire !</p>
        
        <?php echo $message; ?>
        
        <?php if(!$success): ?>
        <form method="POST">
            <div class="form-group">
                <label>Nom complet <span class="required">*</span></label>
                <input type="text" name="nom" required>
            </div>
            <div class="form-group">
                <label>Email <span class="required">*</span></label>
                <input type="email" name="email" required>
            </div>
            <div class="form-group">
                <label>Téléphone</label>
                <input type="tel" name="telephone">
            </div>
            <div class="form-group">
                <label>Sujet <span class="required">*</span></label>
                <select name="sujet" required>
                    <option value="">Sélectionner un sujet</option>
                    <option value="Information formation">Information formation</option>
                    <option value="Inscription">Inscription</option>
                    <option value="Stage">Stage</option>
                    <option value="Partenariat">Partenariat</option>
                    <option value="Autre">Autre</option>
                </select>
            </div>
            <div class="form-group">
                <label>Message <span class="required">*</span></label>
                <textarea name="message" rows="5" required></textarea>
            </div>
            <button type="submit">📤 Envoyer le message</button>
        </form>
        <?php endif; ?>
    </div>
</body>
</html>