<?php
include './config.php';
include('./classes/EmailSender.php');

$errors = [];
$name = $email = $mobile = $message = "";

//checking whether the user submits the form
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['submit'])) {

    $name = filter_input(INPUT_POST, 'name', FILTER_SANITIZE_STRING);
    $email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL);
    $mobile = filter_input(INPUT_POST, 'contactNumber', FILTER_SANITIZE_STRING);
    $message = filter_input(INPUT_POST, 'message', FILTER_SANITIZE_STRING);

    // Validate name
    if (empty($name)) {
        $errors['name'] = "Name is required";
    } elseif (strlen($name) < 3) {
        $errors['name'] = "Name should be at least 3 characters long";
    } elseif (strlen($name) > 50) {
        $errors['name'] = "Name should not exceed 50 characters";
    }

    // Validate email
    if (empty($email)) {
        $errors['email'] = "Email is required";
    } elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $errors['email'] = "Enter a valid email address";
    }

    // Validate contact number
    if (empty($mobile)) {
        $errors['mobile'] = "Contact number is required";
    } elseif (!filter_var($mobile, FILTER_SANITIZE_NUMBER_INT)) {
        $errors['mobile'] = "Enter a valid contact number";
    }

    // Validate message
    if (empty($message)) {
        $errors['message'] = "Message is required";
    }

    // reCAPTCHA secret key
    $recaptcha_secret = GOOGLE_RECAPTCHA_SECRET_KEY;
    
    // Check if reCAPTCHA response is set
    if (isset($_POST['g-recaptcha-response'])) {
        $recaptcha_response = $_POST['g-recaptcha-response'];
    
        $response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=$recaptcha_secret&response=$recaptcha_response");
        $responseKeys = json_decode($response, true);
    
        if (intval($responseKeys['success']) !== 1 || $responseKeys['score'] < 0.5) {
            $errors['recaptcha'] = "Please complete the reCAPTCHA verification";
        }
    } else {
        $errors['recaptcha'] = "Please complete the reCAPTCHA verification";
    }

    if (empty($errors)) {
        $emailSender = new EmailSender();

        // Compose the email content
        $emailTo = 'sachin.pixzarloop@gmail.com';
        $emailSubject = 'Contact Form Message';
        $emailContent = "<table>
                            <tr>
                                <td><b>Name</b></td>
                                <td> : $name</td>
                            </tr>
                            <tr>
                                <td><b>Email</b></td>
                                <td> : $email</td>
                            </tr>
                            <tr>
                                <td><b>Contact Number</b></td>
                                <td> : $mobile</td>
                            </tr>
                            <tr>
                                <td><b>Message</b></td>
                                <td> : $message</td>
                            </tr>
                         </table>";

        // Send email
        $result = $emailSender->sendEmail($emailTo, $emailSubject, $emailContent);

        // Check the result of sending the email
        if ($result === true) {
            $successMessage = 'Your message has been sent successfully!';
            // Clear the form fields after successful submission
            $name = $email = $mobile = $message = "";
        } else {
            $errorMessage = 'Sorry, there was an issue sending your message. Please try again.';
            echo $errorMessage;
        }
    }
}
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <!-- Meta Tags -->
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="<?= WEBSITE_DESCRIPTION ?>">
    <meta name="keywords" content="<?= WEBSITE_KEYWORDS ?>">
    <meta name="author" content="Pixzarloop">
    <meta name="robots" content="index, follow">

    <!-- Page Title -->
    <title><?= WEBSITE_NAME ?> | Contact Us</title>

    <!-- Favicon -->
    <link rel="icon" href="assets/images/favicon.ico" type="image/x-icon">

    <!-- Canonical URL -->
    <link rel="canonical" href="#">

    <!-- Fontawesome -->
    <link rel="stylesheet" href="node_modules/@fortawesome/fontawesome-free/css/all.min.css">

    <!-- AOS Animations CSS -->
    <link href="node_modules/aos/dist/aos.css" rel="stylesheet">

    <!-- Stylesheets -->
    <link rel="stylesheet" href="node_modules/bootstrap/dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="assets/css/variables.css">
    <link rel="stylesheet" href="assets/css/overrides.css">
    <link rel="stylesheet" href="assets/css/style.css">
    <link rel="stylesheet" href="assets/css/responsive.css">
</head>

<body>
    <!-- Header starts -->
    <?php include 'parts/header.php'; ?>
    <!-- Header ends -->

    <!-- Hero starts -->
    <section id="hero" class="text-center py-5"
        style="background: url('assets/images/contact-main.webp') no-repeat center center; background-size: cover;"
        data-aos="fade-up" data-aos-duration="1000" data-aos-delay="100">
        <div class="hero-content">
            <h1 class="text-white" data-aos="fade-up" data-aos-duration="1000" data-aos-delay="200">Contact Us</h1>
            <p class="text-white mt-4" data-aos="fade-up" data-aos-duration="1000" data-aos-delay="300">Get in Touch – We're Here to Help You with All Your Seafood Need</p>
        </div>
    </section>
    <!-- Hero ends -->

    <!-- Contact Us section starts -->
    <section id="contactUs" class="py-5">
        <div class="container">
            <div class="row px-3">
                <div class="col-12 col-md-6 mb-3">
                    <div class="p-4 form-area shadow-sm">
                        <p class="mb-4 text-justify" data-aos="fade-up" data-aos-duration="1000" data-aos-delay="300">
                            Whether you’re interested in our live seafood auctions, have questions about our products,
                            or need more information on how we can serve your business, our team is here to assist.
                            Simply reach out using the form below, and we’ll get back to you as soon as possible.</p>

                        <form action="./contact" method="POST" data-aos="zoom-in" data-aos-duration="1000"
                            data-aos-delay="450">

                            <div class="col-12 col-md-12">
                                <?php if (isset($successMessage)): ?>
                                    <div class="alert alert-success alert-dismissible fade show text-center" role="alert">
                                        <?= $successMessage ?>
                                        <button type="button" class="btn-close" data-bs-dismiss="alert"
                                            aria-label="Close"></button>
                                    </div>
                                <?php elseif (isset($errorMessage)): ?>
                                    <div class="alert alert-danger alert-dismissible fade show text-center" role="alert">
                                        <?= $errorMessage ?>
                                        <button type="button" class="btn-close" data-bs-dismiss="alert"
                                            aria-label="Close"></button>
                                    </div>
                                <?php endif; ?>
                            </div>

                            <div class="row mb-3">
                                <div class="col-md-6 mb-md-0 mb-3">
                                    <label for="name" class="form-label"><strong>Name</strong></label>
                                    <input type="text" class="form-control" id="name" name="name"
                                        placeholder="Enter Name" value="<?= htmlspecialchars($name) ?>">
                                    <?php if (!empty($errors['name'])): ?>
                                        <span class="text-danger"><?= $errors['name']; ?></span>
                                    <?php endif; ?>
                                </div>
                                <div class="col-md-6">
                                    <label for="email" class="form-label"><strong>Email</strong></label>
                                    <input type="email" class="form-control" id="email" name="email"
                                        placeholder="Enter Email" value="<?= htmlspecialchars($email) ?>">
                                    <?php if (!empty($errors['email'])): ?>
                                        <span class="text-danger"><?= $errors['email']; ?></span>
                                    <?php endif; ?>
                                </div>
                            </div>

                            <div class="row mb-3">
                                <div class="col-md-6">
                                    <label for="contactNumber" class="form-label"><strong>Contact
                                            Number</strong></label>
                                    <input type="text" class="form-control" id="contactNumber" name="contactNumber"
                                        placeholder="Enter Contact" value="<?= htmlspecialchars($mobile) ?>">
                                    <?php if (!empty($errors['mobile'])): ?>
                                        <span class="text-danger"><?= $errors['mobile']; ?></span>
                                    <?php endif; ?>
                                </div>
                            </div>

                            <div class="mb-3">
                                <label for="message" class="form-label"><strong>Message</strong></label>
                                <textarea class="form-control" id="message" name="message" rows="4"
                                    placeholder="Enter Message" value="<?= htmlspecialchars($message) ?>"></textarea>
                                <?php if (!empty($errors['message'])): ?>
                                    <span class="text-danger"><?= $errors['message']; ?></span>
                                <?php endif; ?>
                            </div>

                            <input type="hidden" name="g-recaptcha-response" id="g-recaptcha-response">
                            <?php if (!empty($errors['recaptcha'])): ?>
                                <span class="text-danger text-italic small"><?= $errors['recaptcha']; ?></span>
                            <?php endif; ?>

                            <div class="text-start">
                                <button type="submit" name="submit" class="btn send-button">
                                    Send
                                    <img src="assets/images/paper-plane.svg" alt="Image 1" class="image-margin">
                                </button>
                            </div>
                        </form>
                    </div>
                </div>

                <div class="col-12 col-md-6">
                    <div class="row">
                        <div class="col-md-3 d-none d-md-block"></div>
                        <div class="col-md-6 mt-3 mt-md-0">
                            <iframe
                                src="https://www.google.com/maps/embed?pb=!1m16!1m12!1m3!1d7916.336953341724!2d79.89889974458094!3d7.2216155648942415!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!2m1!1s243%2FA%2F5%2C%20Kondagammulla%2C!5e0!3m2!1sen!2slk!4v1730427068272!5m2!1sen!2slk"
                                width="100%" height="auto" style="border:0;" allowfullscreen="" loading="lazy"
                                referrerpolicy="no-referrer-when-downgrade" data-aos="fade-up" data-aos-duration="1000"
                                data-aos-delay="400"></iframe>

                            <div class="mt-4">
                                <ul class="list-unstyled text-md-left">
                                    <li class="mb-2">
                                        <div class="contact-address d-flex">
                                            <img src="assets/images/location.svg" alt="Image 1" class="contact-icon"
                                                width="15" data-aos="flip-right" data-aos-duration="1000"
                                                data-aos-delay="400">
                                            <div class="contact-details-margin" data-aos="fade-up" data-aos-duration="1000"
                                            data-aos-delay="400">
                                                <a target="_blank" href="https://maps.app.goo.gl/uc1sSVi7kaEaVgb58">836, South Pitipana, Negombo</a>
                                            </div>
                                        </div>
                                    </li>
                                    <li class="mb-2">
                                        <div class="contact-address d-flex">
                                            <img src="assets/images/mail.svg" alt="Image 1" class="contact-icon"
                                                width="15" data-aos="flip-right" data-aos-duration="1000"
                                                data-aos-delay="400">
                                            <div class="contact-details-margin" data-aos="fade-up" data-aos-duration="1000"
                                            data-aos-delay="400">
                                                <a href="mailto:juliyaseafood@gmail.com">juliyaseafood@gmail.com</a>
                                            </div>
                                        </div>
                                    </li>
                                    <li class="mb-2">
                                        <div class="contact-address d-flex">
                                            <img src="assets/images/phone.svg" alt="Image 1" class="contact-icon"
                                                width="15" data-aos="flip-right" data-aos-duration="1000"
                                                data-aos-delay="400">
                                            <div class="contact-details-margin" data-aos="fade-up" data-aos-duration="1000"
                                            data-aos-delay="400">
                                                <a href="tel:+94 71 7779002">071 7779002</a> , <a
                                                href="tel:+94 71 6875063">071 6875063</a>
                                            </div>
                                        </div>
                                    </li>
                                </ul>
                            </div>
                        </div>
                        <div class="col-md-3 d-none d-md-block"></div>
                    </div>
                </div>
            </div>
        </div>
    </section>
    <!-- Contact Us section ends -->

    <!-- Footer starts -->
    <?php include 'parts/footer.php'; ?>
    <!-- Footer ends -->

    <!-- Bootstrap -->
    <script src="node_modules/bootstrap/dist/js/bootstrap.min.js"></script>
    <!-- AOS Animations JS -->
    <script src="node_modules/aos/dist/aos.js"></script>
    <!-- Google reCAPTCHA for form verification -->
    <script src="https://www.google.com/recaptcha/api.js?render=<?php echo GOOGLE_RECAPTCHA_SITE_KEY; ?>"></script>
    <!-- Custom JS -->
    <script src="assets/js/script.js"></script>

    <!-- reCAPTCHA v3 implementation -->
    <script>
        grecaptcha.ready(function() {
            grecaptcha.execute('<?php echo GOOGLE_RECAPTCHA_SITE_KEY; ?>', {action: 'submit'}).then(function(token) {
                document.getElementById('g-recaptcha-response').value = token;
            });
        });
    </script>

</body>

</html>