<?php include_once('includes/config.inc.php');?>
<?php

// Load Composer's autoloader
require 'vendor/autoload.php';

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

// var_dump($_SERVER['REQUEST_METHOD']);


// Function to send an email
function sendMail($toEmail, $subject, $messageBody) {
    // Create a new PHPMailer instance
    $mail = new PHPMailer(true);


    try {
        // SMTP Configuration
        $mail->isSMTP();
        $mail->Host       = 'smtp.gmail.com'; // Replace with your SMTP host
        $mail->SMTPAuth   = true;
        $mail->Username   = 'hello.pixmango@gmail.com'; // Replace with your SMTP username
        $mail->Password   = 'qpzddfhpiqdijtud';  // PES // Replace with your SMTP password
        $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // Use TLS or PHPMailer::ENCRYPTION_SMTPS for SSL
        $mail->Port       = 587; // Replace with your SMTP port (usually 587 for TLS)

        // Sender and recipient settings
        $mail->setFrom('server@pixmango.com', 'Pixmango'); // Replace with your "From" address
        $mail->addAddress($toEmail); // Add recipient email

        // Email content
        $mail->isHTML(true);
        $mail->Subject = $subject;
        $mail->Body    = $messageBody;

        // Send the email
        $mail->send();
        // return "<script>window.location.href='success.php'</script>";
        return true;
    } catch (Exception $e) {
        return false;
    }
}


$errors = [];
$success = '';
$loader='';
$capError='';

if ($_SERVER["REQUEST_METHOD"] === "POST" ) {
    $name = htmlspecialchars($_POST['name']);
    $email = htmlspecialchars($_POST['email']);
    $message = htmlspecialchars($_POST['message']);

    echo "submited, ";

    // Get user IP address
    $user_ip = $_SERVER['REMOTE_ADDR'];

    // Get country using an API
    $ip_data = file_get_contents("http://www.geoplugin.net/json.gp?ip={$user_ip}");
    if ($ip_data) {
        $ip_info = json_decode($ip_data, true);
        $country = $ip_info['geoplugin_countryName'] ?? "Unknown";
    }

    // Prepare email
    $toEmail = 'contact@pixmango.com';
    // $toEmail = 'hello.pixmango@gmail.com';
    $subject = "Contact Request from $name";


    $body    = '
        <!DOCTYPE html>
        <html lang="en">
        <head>
            <meta charset="UTF-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <style>
                .button,.email-footer,.email-header{text-align:center}body{font-family:Arial,sans-serif;background-color:#f6f6f6;margin:0;padding:0}.email-container{max-width:600px;margin:20px auto;background-color:#fff;border-radius:8px;box-shadow:0 4px 8px rgba(0,0,0,.1);overflow:hidden}.button:hover,.email-header{background-color:#084258}.email-header{color:#fff;padding:20px}.email-body{padding:20px}.email-footer{background-color:#f1f1f1;color:#666;padding:10px;font-size:12px}.table{width:100%;border-collapse:collapse}.table td,.table th{text-align:left;padding:10px;border-bottom:1px solid #eee}.table th{background-color:#f7f7f7;font-weight:700}.button{display:inline-block;margin-top:20px;padding:10px 20px;color:#fff;background-color:#14a800;text-decoration:none;border-radius:4px}
            </style>
        </head>

        <body>
            <div class="email-container">
                <div class="email-header">
                    <h1>Contact Request Form Submission</h1>
                </div>
                <div class="email-body">
                <p>Hello Admin,</p>
                <p>You have received a new message from the contact form https://pixmango.com. Below are the details:</p>

                <table class="table">
                    <tr>
                        <th>Name</th>
                        <td> '. $name .'</td>
                    </tr>
                    <tr>
                        <th>Email</th>
                        <td>'. $email .'</td>
                    </tr>
                    <tr>
                        <th>Message</th>
                        <td>'. $message .'</td>
                    </tr>
                    <tr>
                        <th>IP Address</th>
                        <td>'. $user_ip .', ' . $country . '</td>
                    </tr>
                </table>

                <p>To reply to this email, you can use the sender\'s email address provided above.</p>
                    <a href="mailto:'. $email .'" class="button">Reply to Sender</a>
                </div>
                <div class="email-footer">
                <p>&copy; 2025 Pixmango. All rights reserved.</p>
                </div>
            </div>
        </body>

        </html>
    ';

    // Your secret key
    $secretKey = "6LeoW_8qAAAAAFKXXf8ael_KdeKclilFVXkAcTPN";
    $responseKey = $_POST['g-recaptcha-response'];
    $userIP = $_SERVER['REMOTE_ADDR'];

    // Verify reCAPTCHA response
    $url = "https://www.google.com/recaptcha/api/siteverify";
    $data = [
        'secret' => $secretKey,
        'response' => $responseKey,
        'remoteip' => $userIP,
    ];

    // Use cURL to send POST request
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
    $response = curl_exec($curl);
    curl_close($curl);

    $resultCap = json_decode($response, true);

    if($resultCap['success']) {
        // Send the email and display the result
        $result = sendMail($toEmail, $subject, $body);

        if ($result) {
            echo "<script>window.location.href='success.php'</script>";
        } else {
            $errors[] = 'Failed to send email.';
        }

    } else {
        $capError='true';
    }




}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1, shrink-to-fit=no">
    <link rel="shortcut icon" href="assets/images/favicon.ico" type="image/x-icon">
    <title>Contact Us</title>

    <?php include_once('includes/stylesheet.inc.php');?>
    <script src="https://www.google.com/recaptcha/api.js" async defer></script>

</head>
<body>

    <!-- Navigation Menu Start -->
    <?php include_once('includes/header.inc.php'); ?>
    <!-- Navigation Menu End -->

    <main>

        <!-- header wrapper start -->
        <section class="header__wrapper position-relative overflow-hidden pt-5 pb-4 pb-lg-0">
            <img src="assets/images/common/hero-water-bg.svg" alt="Pixamgo" class="img-fluid w-100 h-100 object-fit-cover position-absolute top-0 start-0 z-0" width="1903" height="637">
            <div class="container position-relative z-3">
                <div class="row g-4 gx-lg-5 align-items-center">
                    <div class="col-lg-7 text-center text-lg-start">
                        <h1 class="display-6 fw-bold ff-quick lh-sm mb-4">
                            <span class="text-primary display-2 fw-bold d-block" data-aos="fade-up" data-aos-delay="200">Contact Us</span>
                        </h1>
                        <div data-aos="fade-up" data-aos-delay="400">
                            <p class="fs-4 text-white">We'd love to hear from you! <span class="d-md-block">Get in touch with us for any inquiries.</span></p>
                        </div>
                    </div>

                    <div class="col-lg-5 text-center" data-aos="fade-left" data-aos-delay="600">
                        <img src="assets/images/common/contact-thumbnail.webp" alt="Pixamgo" class="img-fluid w-100" width="512" height="493">
                    </div>
                </div><!--.row-->
            </div><!-- /.container-fluid-->
        </section>
        <!-- header wrapper end -->

        <section class="py-5 contact__wrapper">
            <div class="container py-5">
                <h2 class="fs-2 text-center text-dark fw-semibold mb-1">Contact our team</h2>
                <p class="text-center mb-4">
                    Got any questions about our products or services? We're here to help.
                </p>

                <div class="py-4"></div>
                <div class="col-xxl-9 mx-auto">
                    <div class="row g-4 gx-lg-5">
                        <div class="col-lg-7">
                            <?php if(!empty($capError) && $capError === 'true'): ?>
                            <div class="alert alert-danger">
                                <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24">
                                    <path fill="#e13737" d="M12.884 2.532c-.346-.654-1.422-.654-1.768 0l-9 17A1 1 0 0 0 3 21h18a.998.998 0 0 0 .883-1.467zM13 18h-2v-2h2zm-2-4V9h2l.001 5z" stroke-width="0.5" stroke="#e13737" />
                                </svg>
                                Failed to send email. Please check reCAPTCHA.
                            </div>
                            <?php endif;?>
                            <?php if(!empty($errors)): ?>
                            <div class="alert alert-danger" role="alert">
                                <?php foreach($errors as $error): ?>
                                <span><?= $error;?></span>
                                <?php endforeach;?>
                            </div>
                            <?php endif; ?>
                            <form action="" method="POST" class="contact__form">
                                <div class="mb-3">
                                    <label for="name" class="form-label">Full Name</label>
                                    <input type="text" name="name" class="form-control" id="name" placeholder="Enter your name" required>
                                </div>
                                <div class="mb-3">
                                    <label for="email" class="form-label">Email</label>
                                    <input type="email" name="email" class="form-control" id="email" placeholder="Enter your email" required>
                                </div>
                                <div class="mb-3">
                                    <label for="message" class="form-label">Message</label>
                                    <textarea class="form-control" name="message" id="message" rows="5" placeholder="Write your message" required></textarea>
                                </div>
                                <div class="mb-3">
                                    <!-- Google reCAPTCHA -->
                                    <div class="g-recaptcha" data-sitekey="6LeoW_8qAAAAALxGaODF0Ts4O09I6_9PCCxvxDup"></div>
                                </div>
                                <button type="submit" class="btn btn-dark fs-6">Send Message</button>
                            </form>
                        </div>

                        <div class="col-lg-5">
                            <div class=" mt-4 mt-lg-0">
                                <h3 class="fs-5 fw-semibold text-dark mb-0">Chat with us</h3>
                                <p class="small text-muted mb-2">Speak to our support team via live chat or email</p>
                                <div class="d-flex align-items-center gap-2 mb-1">
                                    <svg xmlns="http://www.w3.org/2000/svg" width="1.25em" height="1.25em" viewBox="0 0 14 14"><path fill="none" stroke="#000" stroke-linecap="round" stroke-linejoin="round" d="M6 8L.5 5.5l13-5l-5 13zm0 0l3-3" stroke-width="1"/></svg>
                                    <a href="mailto:<?= $emailAddress ?>" class=" fw-semibold text-decoration-underline">Shoot us an email</a>
                                </div>
                                <div class="d-flex align-items-center gap-2">
                                    <svg xmlns="http://www.w3.org/2000/svg" width="1.125em" height="1.125em" viewBox="0 0 14 14"><g fill="none"><g clip-path="url(#primeTwitter0)"><path fill="#000" d="M11.025.656h2.147L8.482 6.03L14 13.344H9.68L6.294 8.909l-3.87 4.435H.275l5.016-5.75L0 .657h4.43L7.486 4.71zm-.755 11.4h1.19L3.78 1.877H2.504z" stroke-width="0.5" stroke="#000"/></g><defs><clipPath id="primeTwitter0"><path fill="#fff" d="M0 0h14v14H0z"/></clipPath></defs></g></svg>
                                    <a href="<?= $twUrl ?>" target="_blank" class=" fw-semibold text-decoration-underline">Message us on X</a>
                                </div>

                                <h3 class="fs-5 fw-semibold text-dark mt-4 mt-md-5 mb-0">Call us</h3>
                                <p class="small text-muted mb-2">Call our team Mon-Fri from 8am to 5pm.</p>
                                <div class="d-flex align-items-center gap-2">
                                    <svg width="18" height="14" fill="#00427A" enable-background="new 0 0 24 24" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="m12 10c-6.617 0-12 5.383-12 12 0 1.103.897 2 2 2h20c1.103 0 2-.897 2-2 0-6.617-5.383-12-12-12zm0 11c-2.206 0-4-1.794-4-4s1.794-4 4-4 4 1.794 4 4-1.794 4-4 4z"/><path d="m3.136 11.001c-.415 0-.825-.115-1.178-.347-.221-.147-.427-.3-.62-.48-1.055-.985-1.528-2.394-1.27-3.771.44-2.343 3.05-3.867 4.975-4.81 2.129-1.042 4.535-1.593 6.958-1.593 3.26 0 6.549.996 9.024 2.731 1.121.787 2.459 1.886 2.851 3.415.369 1.44-.096 2.983-1.212 4.026-.192.181-.4.334-.608.473-.759.5-1.766.467-2.519-.07l-3.496-2.495c-.779-.556-1.091-1.54-.757-2.392.019-.049.039-.098.06-.146-2.137-.723-4.55-.722-6.687 0 .021.048.041.096.06.145.334.854.021 1.837-.758 2.393l-3.496 2.495c-.398.283-.864.426-1.327.426z"/></svg>
                                    <a href="tel:0247122610" class=" fw-semibold text-decoration-underline">0247122610</a>
                                </div>

                                <h3 class="fs-5 fw-semibold text-dark mt-4 mt-md-5 mb-0">Visit us</h3>
                                <p class="small text-muted mb-2">Chat to us in person at our office.</p>
                                <div class="d-flex align-items-center gap-2 mb-2">
                                    <svg xmlns="http://www.w3.org/2000/svg" width="1.45em" height="1.45em" viewBox="0 0 1024 1024"><path fill="#000" d="M515.664-.368C305.76-.368 128 178.4 128 390.176c0 221.76 206.032 448.544 344.624 607.936c.528.64 22.929 25.52 50.528 25.52h2.449c27.6 0 49.84-24.88 50.399-25.52c130.064-149.52 320-396.048 320-607.936C896 178.4 757.344-.368 515.664-.368m12.832 955.552c-1.12 1.12-2.753 2.369-4.193 3.409c-1.472-1.008-3.072-2.288-4.255-3.408l-16.737-19.248C371.92 785.2 192 578.785 192 390.176c0-177.008 148.224-326.56 323.664-326.56c218.528 0 316.336 164 316.336 326.56c0 143.184-102.128 333.296-303.504 565.008m-15.377-761.776c-106.032 0-192 85.968-192 192s85.968 192 192 192s192-85.968 192-192s-85.968-192-192-192m0 320c-70.576 0-129.473-58.816-129.473-129.408c0-70.576 57.424-128 128-128c70.624 0 128 57.424 128 128c.032 70.592-55.903 129.408-126.527 129.408" stroke-width="4" stroke="#000"/></svg>
                                    <address class="mb-0 text-dark fw-medium text-decoration-underline lh-sm fs-6">Graphics Building, 9/G, Motijheel B/A 2nd Floor, Dhaka 1000</address>
                                </div>
                                <div class="d-flex align-items-center gap-2">
                                    <svg xmlns="http://www.w3.org/2000/svg" width="1.45em" height="1.45em" viewBox="0 0 1024 1024"><path fill="#000" d="M515.664-.368C305.76-.368 128 178.4 128 390.176c0 221.76 206.032 448.544 344.624 607.936c.528.64 22.929 25.52 50.528 25.52h2.449c27.6 0 49.84-24.88 50.399-25.52c130.064-149.52 320-396.048 320-607.936C896 178.4 757.344-.368 515.664-.368m12.832 955.552c-1.12 1.12-2.753 2.369-4.193 3.409c-1.472-1.008-3.072-2.288-4.255-3.408l-16.737-19.248C371.92 785.2 192 578.785 192 390.176c0-177.008 148.224-326.56 323.664-326.56c218.528 0 316.336 164 316.336 326.56c0 143.184-102.128 333.296-303.504 565.008m-15.377-761.776c-106.032 0-192 85.968-192 192s85.968 192 192 192s192-85.968 192-192s-85.968-192-192-192m0 320c-70.576 0-129.473-58.816-129.473-129.408c0-70.576 57.424-128 128-128c70.624 0 128 57.424 128 128c.032 70.592-55.903 129.408-126.527 129.408" stroke-width="4" stroke="#000"/></svg>
                                    <address class="mb-0 text-dark fw-medium text-decoration-underline lh-sm fs-6">35448 Mound Rd Sterling Heights, MI 48310, United States</address>
                                </div>



                            </div>
                        </div>
                    </div><!--/.row-->

                </div><!-- /.bg-light -->
            </div>

        </section>


    </main>



    <!-- footer wrapper start -->
    <?php include_once('includes/footer.inc.php'); ?>
    <!-- footer wrapper end -->


</body>
</html>
