<?php
// Database connection
$con = mysqli_connect("localhost", "collinsg_camgtpu", "dh+z9*R*V0mR", "collinsg_camgtpu");

if (!$con) {
    die("Connection failed: " . mysqli_connect_error());
}

// Check if form is submitted
if (isset($_POST['submit'])) {
    // Get form data
    $name = mysqli_real_escape_string($con, $_POST['name']);
    $email = mysqli_real_escape_string($con, $_POST['email']);
    $phone = mysqli_real_escape_string($con, $_POST['phone']);
    $product = mysqli_real_escape_string($con, $_POST['product']);
    $messageContent = mysqli_real_escape_string($con, $_POST['message']);
    
    // Define status and created_at
    $status = 'Pending';
    $created_at = date('Y-m-d H:i:s');

    // Insert data into database
    $sql = "INSERT INTO `contact` (`name`, `email`, `phone`, `product`, `message`, `status`, `created_at`) 
            VALUES ('$name', '$email', '$phone', '$product', '$messageContent', '$status', '$created_at')";

    if (mysqli_query($con, $sql)) {
        // Send confirmation email
        $to = $email;
        $subject = "Thank you for contacting us";
        
        $body = "
        <html>
        <head>
            <title>Thank You</title>
            <style>
                body {
                    font-family: Arial, sans-serif;
                    color: #333;
                    margin: 0;
                    padding: 0;
                    background-color: #f4f4f4;
                }
                .container {
                    width: 100%;
                    max-width: 600px;
                    margin: 20px auto;
                    padding: 20px;
                    background-color: #fff;
                    border-radius: 8px;
                    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
                }
                h1 {
                    color: #333;
                }
                p {
                    line-height: 1.6;
                }
                .footer {
                    margin-top: 20px;
                    padding-top: 10px;
                    border-top: 1px solid #ddd;
                    text-align: center;
                    color: #888;
                }
                .button {
                    display: inline-block;
                    padding: 10px 20px;
                    font-size: 16px;
                    color: #fff;
                    background-color: #007bff;
                    text-decoration: none;
                    border-radius: 5px;
                }
            </style>
        </head>
        <body>
            <div class='container'>
                <h1>Thank You for Your Message, $name!</h1>
                <p>Hello $name,</p>
                <p>Thank you for reaching out to us. We have received your message and will get back to you soon.</p>
                <p>If you need immediate assistance, feel free to contact us directly.</p>
                <p>Best regards,<br>Global Trade Plaza</p>
                <div class='footer'>
                    <p>&copy; 2024 Global Trade Plaza. All rights reserved.</p>
                </div>
            </div>
        </body>
        </html>";

        $headers = "From: no-reply@yourdomain.com\r\n";
        $headers .= "Reply-To: no-reply@yourdomain.com\r\n";
        $headers .= "Content-type: text/html\r\n";

        if (mail($to, $subject, $body, $headers)) {
            // Display alert and redirect
            echo "<!DOCTYPE html>
            <html>
            <head>
                <title>Success</title>
                <style>
                    body {
                        font-family: Arial, sans-serif;
                        background-color: #f4f4f4;
                        margin: 0;
                        padding: 0;
                        text-align: center;
                        padding-top: 50px;
                    }
                    .alert {
                        display: inline-block;
                        padding: 20px;
                        font-size: 18px;
                        color: #fff;
                        background-color: #4caf50;
                        border-radius: 5px;
                        margin-bottom: 20px;
                    }
                    .alert a {
                        color: #fff;
                        text-decoration: underline;
                    }
                </style>
            </head>
            <body>
                <div class='alert'>
                    Your message has been sent successfully! You will be redirected shortly.
                </div>
                <script>
                    setTimeout(function() {
                        window.location.href = 'https://campaigns.globaltradeplaza.com/';
                    }, 2000); // Redirect after 3 seconds
                </script>
            </body>
            </html>";
        } else {
            echo "Data inserted but failed to send confirmation email. Please check your email configuration.";
        }
    } else {
        echo "Error: " . $sql . "<br>" . mysqli_error($con);
    }

    // Close database connection
    mysqli_close($con);
} else {
    echo "No data submitted.";
}
?>