<?php

include('SendMail/send-mail.php');

$conn = mysqli_connect('localhost', 'root', 'indev@123', 'bharat_impact');

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

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $first_name = mysqli_real_escape_string($conn, $_POST['first_name']);
    $last_name = mysqli_real_escape_string($conn, $_POST['last_name']);
    $email_id = mysqli_real_escape_string($conn, $_POST['email_id']);
    $phone_number = mysqli_real_escape_string($conn, $_POST['phone_number']);
    $message = mysqli_real_escape_string($conn, $_POST['message']);
    //$subject="";



    $email_idcontect = 'madhavkumarroy5@gmail.com';
    $event_all = '';
    $event_all = '
    <div style="background-color: #ffffff; border-radius: 8px; box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); margin: 20px auto; max-width: 600px; overflow: hidden; padding: 0;">
        
        <div style="padding: 20px; color: #333333;">
            <p style="margin: 15px 0; line-height: 1.6;">Dear </p>
            <p><b>Name:</b> <span style="font-weight: 600;">' . htmlspecialchars($first_name . ' ' . $last_name) . '</span></p>
            <p><b>Email:</b> <span style="font-weight: 600;">' . htmlspecialchars($email_id) . '</span></p>
            <p><b>Phone Number:</b> <span style="font-weight: 600;">' . htmlspecialchars($phone_number) . '</span></p>
            <p style="margin: 15px 0; line-height: 1.6;"><b>Message:</b><br>' . nl2br(htmlspecialchars($message)) . '</p>
            <p style="margin: 0;">Best Regards,</p>
            <p style="margin: 0;">BHARAT IMPACT</p>
        </div>
    </div>';

    $txt = $event_all;
    $mailto = $email_idcontect;
    $subject = base64_encode($subject);
    $message = base64_encode($txt);
    $fileUrl = '';
    $send_mail = send_mail_function($mailto, $message, $subject, $fileUrl, $first_name);


    if ($send_mail['status'] == 1) {
        $sqlInsert = "INSERT INTO contact_us (first_name, last_name, email_id, phone_number, message) 
        VALUES ('$first_name', '$last_name', '$email_id', '$phone_number', '$message')";
        $sqlSubmit = mysqli_query($conn, $sqlInsert);
        if ($sqlSubmit) {
            echo "<script>alert('Message sent successfully!');</script>";
        } else {
            echo "Error: " . $sql . "<br>" . mysqli_error($conn);
        }
    } else {
        echo "error";
    }
}

mysqli_close($conn);
?>




<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta property="og:image" content="">
    <title>Contact Bharat Impact</title>
    <?php include("include/links.php"); ?>
</head>

<body>
    <?php include("include/header.php"); ?>
    <div class="inner-banner p-0 position-relative">
        <img src="assets/images/contact-us-inner.jpg" class="w-100 img-fluid" alt="Contact us">
        <div class="position-absolute top-50 start-0 w-100 inner-banner-des">
            <div class="container">
                <h1 class="mb-3 fw-bold">Contact us</h1>
                <nav aria-label="breadcrumb">
                    <ol class="breadcrumb">
                        <li class="breadcrumb-item"><a href="index.php">Home</a> </li>
                        <li class="breadcrumb-item"><i class="fa-solid fa-chevron-right"></i></li>
                        <li class="breadcrumb-item active" aria-current="page">Contact us
                        </li>
                    </ol>
                </nav>
            </div>
        </div>
    </div>
    <section class="py-5 about-us ">
        <div class="container">
            <div class="text-center mb-5">
                <h2 class="special-head">Contact us </h2>
            </div>
            <div class="row">
                <div class="col-md-6">
                    <form action="" class="form bg-light rounded-3 p-4" method="post" id="contactForm">
                        <div class="row g-3">
                            <div class="form-group col-sm-6">
                                <label for="first_name" class="mb-2">First name<span class="text-danger">*</span></label>
                                <input type="text" id="first_name" name="first_name" class="form-control" placeholder="First name" aria-required="true">
                            </div>
                            <div class="form-group col-sm-6">
                                <label for="last_name" class="mb-2">Last name<span class="text-danger">*</span></label>
                                <input type="text" id="last_name" name="last_name" class="form-control" placeholder="Last name" aria-required="true">
                            </div>
                            <div class="form-group col-sm-12">
                                <label for="email_id" class="mb-2">Email<span class="text-danger">*</span></label>
                                <input type="text" id="email_id" name="email_id" class="form-control" placeholder="example@gmail.com" aria-required="true">
                            </div>
                            <div class="form-group col-sm-12">
                                <label for="phone_number" class="mb-2">Phone number<span class="text-danger">*</span></label>
                                <input type="text" id="phone_number" name="phone_number" class="form-control" placeholder="+1-234-567-8901" aria-required="true" oninput="restrictToDigits(event)">
                            </div>
                            <div class="form-group col-sm-12">
                                <label for="message" class="mb-2">Message<span class="text-danger">*</span></label>
                                <textarea rows="4" id="message" name="message" class="form-control" placeholder="Leave us a message"></textarea>
                            </div>
                            <div class="form-group col-sm-12">
                                <input type="submit" name="save" class="form-control btn btn-primary" value="Send message">
                            </div>
                        </div>
                    </form>
                </div>
                <div class="col-md-6">
                    <iframe class="w-100 h-100 rounded-3" src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d4310.008214732815!2d77.36576077613657!3d28.607670685208117!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x390ce565c0000001%3A0xfc8202ce337370fb!2sc%2C%2026%2C%20E%20Block%2C%20Sector%2059%2C%20Noida%2C%20Uttar%20Pradesh%20201301!5e1!3m2!1sen!2sin!4v1729929248914!5m2!1sen!2sin" allowfullscreen="" loading="lazy" referrerpolicy="no-referrer-when-downgrade"></iframe>
                </div>
            </div>
        </div>
    </section>
    <?php include("include/footer.php"); ?>
    <?php include("include/script.php"); ?>

    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.3/jquery.validate.min.js"></script>

    <script>
        $(document).ready(function() {
            // Initialize jQuery Validation
            $("#contactForm").validate({
                rules: {
                    first_name: {
                        required: true,
                        minlength: 2
                    },
                    last_name: {
                        required: true,
                        minlength: 2
                    },
                    email_id: {
                        required: true,
                        email: true
                    },
                    phone_number: {
                        required: true,
                        digits: true,
                        minlength: 10,
                        maxlength: 15
                    },
                    message: {
                        required: true,
                        minlength: 10
                    }
                },
                messages: {
                    first_name: {
                        required: "Please enter your first name",
                        minlength: "Your first name must be at least 2 characters long"
                    },
                    last_name: {
                        required: "Please enter your last name",
                        minlength: "Your last name must be at least 2 characters long"
                    },
                    email_id: {
                        required: "Please enter your email",
                        email: "Please enter a valid email address"
                    },
                    phone_number: {
                        required: "Please enter your phone number",
                        digits: "Please enter only numbers",
                        minlength: "Your phone number must be at least 10 digits",
                        maxlength: "Your phone number cannot exceed 15 digits"
                    },
                    message: {
                        required: "Please enter your message",
                        minlength: "Your message must be at least 10 characters long"
                    }
                },
                errorClass: "text-danger",
                errorPlacement: function(error, element) {
                    error.insertAfter(element);
                }
            });
        });
    </script>

</body>

</html>