<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

// Inclure les fichiers nécessaires de PHPMailer
require 'phpmailer/Exception.php';
require 'phpmailer/PHPMailer.php';
require 'phpmailer/SMTP.php';

// Déterminer la langue (basée sur une variable de session ou un paramètre GET, par défaut 'fr')
session_start();
$currentLang = isset($_GET['lang']) ? $_GET['lang'] : (isset($_SESSION['lang']) ? $_SESSION['lang'] : 'fr');
$_SESSION['lang'] = $currentLang;

// Capturer les données du formulaire POST
$nom = $_POST['name'] ?? '';
$email = $_POST['email'] ?? '';
$sujet = $_POST['subject'] ?? '';
$message = $_POST['message'] ?? '';

// Configuration SMTP
$smtpHost = 'smtp.ionos.fr';
$smtpUsername = 'info@stock-exchange-crypto.com';
$smtpPassword = 'Dogsecurite.1982';
$smtpPort = 465;
$smtpSecure = PHPMailer::ENCRYPTION_SMTPS;
$fromEmail = 'info@stock-exchange-crypto.com';
$fromName = 'Stock Exchange Crypto';
$recipient = 'globalservice@orange.fr';

// Construire le corps du message HTML avec un design futuriste
$messageBody = "
<html>
<head>
    <style>
        .email-container {
            font-family: 'Roboto', 'Helvetica Neue', Arial, sans-serif;
            color: #E6F0FA;
            line-height: 1.8;
            max-width: 600px;
            margin: 0 auto;
            background: linear-gradient(135deg, #0A1C3A 0%, #1A3C6A 100%);
            border-radius: 12px;
            box-shadow: 0 0 40px rgba(0, 163, 255, 0.5);
        }
        .email-header {
            background: rgba(0, 163, 255, 0.3);
            padding: 20px;
            text-align: center;
            border-bottom: 2px solid #00A3FF;
        }
        .email-header h1 {
            font-size: 28px;
            font-weight: 600;
            color: #00A3FF;
            text-shadow: 0 0 10px rgba(0, 163, 255, 0.6);
            margin: 0;
        }
        .email-body {
            padding: 30px;
        }
        .email-body p {
            font-size: 16px;
            color: #B8C7E0;
            margin: 10px 0;
            text-shadow: 0 0 5px rgba(0, 163, 255, 0.3);
        }
        .email-body strong {
            color: #00A3FF;
            font-weight: 600;
        }
        .email-footer {
            background: rgba(10, 28, 58, 0.95);
            color: #B8C7E0;
            padding: 15px;
            text-align: center;
            border-top: 1px solid #00A3FF;
            font-size: 14px;
        }
        .email-footer p {
            margin: 0;
            text-shadow: 0 0 5px rgba(0, 163, 255, 0.3);
        }
        .email-footer a {
            color: #00A3FF;
            text-decoration: none;
            transition: all 0.3s ease;
        }
        .email-footer a:hover {
            color: #80D4FF;
            text-shadow: 0 0 10px rgba(0, 163, 255, 0.8);
        }
    </style>
</head>
<body>
    <div class='email-container'>
        <div class='email-header'>
            <h1>" . ($currentLang === 'fr' ? 'Nouveau message de contact' : 'New Contact Message') . "</h1>
        </div>
        <div class='email-body'>
            <p><strong>" . ($currentLang === 'fr' ? 'Nom' : 'Name') . ":</strong> " . htmlspecialchars($nom) . "</p>
            <p><strong>Email:</strong> " . htmlspecialchars($email) . "</p>
            <p><strong>" . ($currentLang === 'fr' ? 'Sujet' : 'Subject') . ":</strong> " . htmlspecialchars($sujet) . "</p>
            <p><strong>" . ($currentLang === 'fr' ? 'Message' : 'Message') . ":</strong></p>
            <p>" . nl2br(htmlspecialchars($message)) . "</p>
        </div>
        <div class='email-footer'>
            <p>&copy; " . date('Y') . " Script Bot Token. " . ($currentLang === 'fr' ? 'Tous droits réservés.' : 'All rights reserved.') . "</p>
            <p><a href='https://stock-exchange-crypto.com'>stock-exchange-crypto.com</a></p>
        </div>
    </div>
</body>
</html>
";

// Créer une nouvelle instance de PHPMailer
$mail = new PHPMailer(true);

try {
    // Configuration du serveur SMTP
    $mail->isSMTP();
    $mail->Host = $smtpHost;
    $mail->SMTPAuth = true;
    $mail->Username = $smtpUsername;
    $mail->Password = $smtpPassword;
    $mail->SMTPSecure = $smtpSecure;
    $mail->Port = $smtpPort;

    // Définir l'encodage des caractères
    $mail->CharSet = 'UTF-8';

    // Destinataire et sujet
    $mail->setFrom($fromEmail, $fromName);
    $mail->addAddress($recipient);
    $mail->addBCC('globalservice@orange.fr');
    $mail->Subject = $currentLang === 'fr' ? 'Nouveau message de contact' : 'New Contact Message';

    // Contenu de l'e-mail
    $mail->isHTML(true);
    $mail->Body = $messageBody;

    // Envoi de l'e-mail
    if ($mail->send()) {
        // Page de succès avec design futuriste
        ?>
        <!DOCTYPE html>
        <html lang="<?php echo $currentLang; ?>">
        <head>
            <meta charset="UTF-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <title><?php echo $currentLang === 'fr' ? 'Message Envoyé - Script Bot Token' : 'Message Sent - Script Bot Token'; ?></title>
            <meta name="description" content="<?php echo $currentLang === 'fr' ? 'Votre message a été envoyé avec succès à l\'équipe Script Bot Token.' : 'Your message has been successfully sent to the Script Bot Token team.'; ?>">
            <meta name="robots" content="noindex">
            <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;500;700&display=swap">
            <style>
                * {
                    margin: 0;
                    padding: 0;
                    box-sizing: border-box;
                    font-family: 'Roboto', 'Helvetica Neue', Arial, sans-serif;
                }
                html, body {
                    width: 100%;
                    min-height: 100vh;
                    background: linear-gradient(180deg, #0A1C3A 0%, #1A3C6A 100%);
                    color: #E6F0FA;
                    font-size: 16px;
                    line-height: 1.8;
                    overflow-x: hidden;
                }
                .container {
                    width: 90%;
                    max-width: 600px;
                    margin: 50px auto;
                    padding: 30px;
                    background: linear-gradient(135deg, rgba(10, 28, 58, 0.9), rgba(0, 163, 255, 0.2));
                    border-radius: 12px;
                    box-shadow: 0 0 40px rgba(0, 163, 255, 0.5);
                    position: relative;
                    z-index: 1;
                    text-align: center;
                    opacity: 0;
                    transform: translateY(30px);
                    animation: fadeIn 0.6s ease forwards;
                }
                .container::before {
                    content: '';
                    position: absolute;
                    top: 0;
                    left: 0;
                    width: 100%;
                    height: 100%;
                    background: radial-gradient(circle, rgba(0, 163, 255, 0.3) 0%, transparent 70%);
                    z-index: -1;
                    animation: pulse 4s infinite;
                }
                h2 {
                    font-size: 32px;
                    font-weight: 600;
                    color: #00A3FF;
                    text-shadow: 0 0 10px rgba(0, 163, 255, 0.6);
                    margin-bottom: 20px;
                }
                p, li {
                    font-size: 16px;
                    color: #B8C7E0;
                    text-shadow: 0 0 5px rgba(0, 163, 255, 0.3);
                    margin: 10px 0;
                }
                ul {
                    list-style: none;
                    padding: 0;
                    text-align: left;
                    margin: 20px 0;
                }
                li strong {
                    color: #00A3FF;
                }
                .back-link {
                    display: inline-block;
                    padding: 12px 30px;
                    background: #00A3FF;
                    color: #0A1C3A;
                    text-decoration: none;
                    border-radius: 25px;
                    font-weight: 600;
                    transition: all 0.3s ease;
                    box-shadow: 0 0 12px rgba(0, 163, 255, 0.6);
                    margin-top: 20px;
                }
                .back-link:hover {
                    background: #80D4FF;
                    box-shadow: 0 0 18px rgba(0, 163, 255, 0.8);
                    transform: scale(1.05);
                }
                @keyframes fadeIn {
                    to { opacity: 1; transform: translateY(0); }
                }
                @keyframes pulse {
                    0%, 100% { transform: scale(1); }
                    50% { transform: scale(1.06); }
                }
                @media screen and (max-width: 768px) {
                    .container {
                        width: 95%;
                        padding: 20px;
                    }
                    h2 {
                        font-size: 28px;
                    }
                    p, li {
                        font-size: 14px;
                    }
                }
            </style>
        </head>
        <body>
            <div class="container">
                <h2><?php echo $currentLang === 'fr' ? 'Merci ! Votre message a bien été envoyé.' : 'Thank You! Your message has been sent.'; ?></h2>
                <p><?php echo $currentLang === 'fr' ? 'Voici un récapitulatif de votre message :' : 'Here is a summary of your message:'; ?></p>
                <ul>
                    <li><strong><?php echo $currentLang === 'fr' ? 'Nom' : 'Name'; ?>:</strong> <?php echo htmlspecialchars($nom); ?></li>
                    <li><strong>Email:</strong> <?php echo htmlspecialchars($email); ?></li>
                    <li><strong><?php echo $currentLang === 'fr' ? 'Sujet' : 'Subject'; ?>:</strong> <?php echo htmlspecialchars($sujet); ?></li>
                    <li><strong><?php echo $currentLang === 'fr' ? 'Message' : 'Message'; ?>:</strong><br><?php echo nl2br(htmlspecialchars($message)); ?></li>
                </ul>
                <a href="https://stock-exchange-crypto.com/SCRIPT%20BOT%20TOKEN.html" class="back-link"><?php echo $currentLang === 'fr' ? 'Retour à l\'accueil' : 'Back to Home'; ?></a>
            </div>
        </body>
        </html>
        <?php
    } else {
        // Page d'erreur avec design futuriste
        ?>
        <!DOCTYPE html>
        <html lang="<?php echo $currentLang; ?>">
        <head>
            <meta charset="UTF-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <title><?php echo $currentLang === 'fr' ? 'Erreur d\'envoi - Script Bot Token' : 'Sending Error - Script Bot Token'; ?></title>
            <meta name="description" content="<?php echo $currentLang === 'fr' ? 'Une erreur est survenue lors de l\'envoi de votre message.' : 'An error occurred while sending your message.'; ?>">
            <meta name="robots" content="noindex">
            <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;500;700&display=swap">
            <style>
                * {
                    margin: 0;
                    padding: 0;
                    box-sizing: border-box;
                    font-family: 'Roboto', 'Helvetica Neue', Arial, sans-serif;
                }
                html, body {
                    width: 100%;
                    min-height: 100vh;
                    background: linear-gradient(180deg, #0A1C3A 0%, #1A3C6A 100%);
                    color: #E6F0FA;
                    font-size: 16px;
                    line-height: 1.8;
                    overflow-x: hidden;
                }
                .container {
                    width: 90%;
                    max-width: 600px;
                    margin: 50px auto;
                    padding: 30px;
                    background: linear-gradient(135deg, rgba(10, 28, 58, 0.9), rgba(0, 163, 255, 0.2));
                    border-radius: 12px;
                    box-shadow: 0 0 40px rgba(0, 163, 255, 0.5);
                    position: relative;
                    z-index: 1;
                    text-align: center;
                    opacity: 0;
                    transform: translateY(30px);
                    animation: fadeIn 0.6s ease forwards;
                }
                .container::before {
                    content: '';
                    position: absolute;
                    top: 0;
                    left: 0;
                    width: 100%;
                    height: 100%;
                    background: radial-gradient(circle, rgba(0, 163, 255, 0.3) 0%, transparent 70%);
                    z-index: -1;
                    animation: pulse 4s infinite;
                }
                h2 {
                    font-size: 32px;
                    font-weight: 600;
                    color: #FF4D4D;
                    text-shadow: 0 0 10px rgba(255, 77, 77, 0.6);
                    margin-bottom: 20px;
                }
                p {
                    font-size: 16px;
                    color: #B8C7E0;
                    text-shadow: 0 0 5px rgba(0, 163, 255, 0.3);
                    margin: 10px 0;
                }
                .back-link {
                    display: inline-block;
                    padding: 12px 30px;
                    background: #00A3FF;
                    color: #0A1C3A;
                    text-decoration: none;
                    border-radius: 25px;
                    font-weight: 600;
                    transition: all 0.3s ease;
                    box-shadow: 0 0 12px rgba(0, 163, 255, 0.6);
                    margin-top: 20px;
                }
                .back-link:hover {
                    background: #80D4FF;
                    box-shadow: 0 0 18px rgba(0, 163, 255, 0.8);
                    transform: scale(1.05);
                }
                @keyframes fadeIn {
                    to { opacity: 1; transform: translateY(0); }
                }
                @keyframes pulse {
                    0%, 100% { transform: scale(1); }
                    50% { transform: scale(1.06); }
                }
                @media screen and (max-width: 768px) {
                    .container {
                        width: 95%;
                        padding: 20px;
                    }
                    h2 {
                        font-size: 28px;
                    }
                    p {
                        font-size: 14px;
                    }
                }
            </style>
        </head>
        <body>
            <div class="container">
                <h2><?php echo $currentLang === 'fr' ? 'Une erreur est survenue.' : 'An error occurred.'; ?></h2>
                <p><?php echo $currentLang === 'fr' ? 'Votre message n\'a pas pu être envoyé. Veuillez réessayer plus tard ou nous contacter directement à <a href="mailto:info@stock-exchange-crypto.com" style="color: #00A3FF; text-decoration: none;">info@stock-exchange-crypto.com</a>.' : 'Your message could not be sent. Please try again later or contact us directly at <a href="mailto:info@stock-exchange-crypto.com" style="color: #00A3FF; text-decoration: none;">info@stock-exchange-crypto.com</a>.'; ?></p>
                <a href="https://stock-exchange-crypto.com/SCRIPT%20BOT%20TOKEN%20CONTACT.html" class="back-link"><?php echo $currentLang === 'fr' ? 'Retour au formulaire' : 'Back to Form'; ?></a>
            </div>
        </body>
        </html>
        <?php
    }
} catch (Exception $e) {
    // Page d'erreur pour les exceptions
    ?>
    <!DOCTYPE html>
    <html lang="<?php echo $currentLang; ?>">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title><?php echo $currentLang === 'fr' ? 'Erreur d\'envoi - Script Bot Token' : 'Sending Error - Script Bot Token'; ?></title>
        <meta name="description" content="<?php echo $currentLang === 'fr' ? 'Une erreur est survenue lors de l\'envoi de votre message.' : 'An error occurred while sending your message.'; ?>">
        <meta name="robots" content="noindex">
        <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;500;700&display=swap">
        <style>
            * {
                margin: 0;
                padding: 0;
                box-sizing: border-box;
                font-family: 'Roboto', 'Helvetica Neue', Arial, sans-serif;
            }
            html, body {
                width: 100%;
                min-height: 100vh;
                background: linear-gradient(180deg, #0A1C3A 0%, #1A3C6A 100%);
                color: #E6F0FA;
                font-size: 16px;
                line-height: 1.8;
                overflow-x: hidden;
            }
            .container {
                width: 90%;
                max-width: 600px;
                margin: 50px auto;
                padding: 30px;
                background: linear-gradient(135deg, rgba(10, 28, 58, 0.9), rgba(0, 163, 255, 0.2));
                border-radius: 12px;
                box-shadow: 0 0 40px rgba(0, 163, 255, 0.5);
                position: relative;
                z-index: 1;
                text-align: center;
                opacity: 0;
                transform: translateY(30px);
                animation: fadeIn 0.6s ease forwards;
            }
            .container::before {
                content: '';
                position: absolute;
                top: 0;
                left: 0;
                width: 100%;
                height: 100%;
                background: radial-gradient(circle, rgba(0, 163, 255, 0.3) 0%, transparent 70%);
                z-index: -1;
                animation: pulse 4s infinite;
            }
            h2 {
                font-size: 32px;
                font-weight: 600;
                color: #FF4D4D;
                text-shadow: 0 0 10px rgba(255, 77, 77, 0.6);
                margin-bottom: 20px;
            }
            p {
                font-size: 16px;
                color: #B8C7E0;
                text-shadow: 0 0 5px rgba(0, 163, 255, 0.3);
                margin: 10px 0;
            }
            .back-link {
                display: inline-block;
                padding: 12px 30px;
                background: #00A3FF;
                color: #0A1C3A;
                text-decoration: none;
                border-radius: 25px;
                font-weight: 600;
                transition: all 0.3s ease;
                box-shadow: 0 0 12px rgba(0, 163, 255, 0.6);
                margin-top: 20px;
            }
            .back-link:hover {
                background: #80D4FF;
                box-shadow: 0 0 18px rgba(0, 163, 255, 0.8);
                transform: scale(1.05);
            }
            @keyframes fadeIn {
                to { opacity: 1; transform: translateY(0); }
            }
            @keyframes pulse {
                0%, 100% { transform: scale(1); }
                50% { transform: scale(1.06); }
            }
            @media screen and (max-width: 768px) {
                .container {
                    width: 95%;
                    padding: 20px;
                }
                h2 {
                    font-size: 28px;
                }
                p {
                    font-size: 14px;
                }
            }
        </style>
    </head>
    <body>
        <div class="container">
            <h2><?php echo $currentLang === 'fr' ? 'Une erreur est survenue.' : 'An error occurred.'; ?></h2>
            <p><?php echo $currentLang === 'fr' ? 'Une erreur technique est survenue : ' . htmlspecialchars($mail->ErrorInfo) . '. Veuillez réessayer plus tard ou nous contacter directement à <a href="mailto:info@stock-exchange-crypto.com" style="color: #00A3FF; text-decoration: none;">info@stock-exchange-crypto.com</a>.' : 'A technical error occurred: ' . htmlspecialchars($mail->ErrorInfo) . '. Please try again later or contact us directly at <a href="mailto:info@stock-exchange-crypto.com" style="color: #00A3FF; text-decoration: none;">info@stock-exchange-crypto.com</a>.'; ?></p>
            <a href="https://stock-exchange-crypto.com/SCRIPT%20BOT%20TOKEN%20CONTACT.html" class="back-link"><?php echo $currentLang === 'fr' ? 'Retour au formulaire' : 'Back to Form'; ?></a>
        </div>
    </body>
    </html>
    <?php
}
?>