<?php
// Check if form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Retrieve form data
    $name = $_POST["name1"];
    $email = $_POST["email"];
    $booking_date = $_POST["date"];
    $booking_time = $_POST["time"];

    // Validate form data (you can add more validation as needed)
    if (empty($name) || empty($email) || empty($booking_date) || empty($booking_time)) {
        echo "Select Date and Time required";
    } else {
        // Connect to your database (replace dbname, username, password with your actual database credentials)
        $conn = new mysqli("localhost", "design24ilogicx_autotherapy", "I[N1.?ZM-YH~", "design24ilogicx_autotherapy");

        // Check connection
        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
        }

        // Check if the slot is already booked
        $sql = "SELECT * FROM booking WHERE booking_date = '$booking_date' AND booking_time = '$booking_time'";
        $result = $conn->query($sql);

        if ($result->num_rows > 0) {
            echo "Sorry, the slot is already booked. Please choose a different time.";
        } else {
            // If the slot is available, insert the booking into the database
            $sql = "INSERT INTO booking (name, email, booking_date, booking_time) VALUES ('$name', '$email', '$booking_date', '$booking_time')";

            if ($conn->query($sql) === TRUE) {
                //echo "Booking successful! We'll see you on $booking_date at $booking_time.";
            } else {
                echo "Error: " . $sql . "<br>" . $conn->error;
            }
            $error = "";
//print_r($_POST);die;
if (!isset($_POST['name1']) || $_POST['name1'] == "") {
    $error .= "\nName is required";
} else {
    if (!preg_match('/^[a-zA-Z\\s]*$/', $_POST['name1'])) {
        $error .= "\nEnter valid Name";
    }
}

if(!isset($_POST['email']) || $_POST['email']=="")
{
    $error .=" \nEmail Id is required";
}
else
{
    if (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) 
    {   
    
        $error .=" \nEnter valid Email Id";
    }
}


if (!isset($_POST['phone1']) || $_POST['phone1'] == "" || trim($_POST['phone1']) == "") {
    $error .= " \n Phone Number is required";
}



if ($error == "") {

    require 'PHPMailer/PHPMailerAutoload.php';
    $mail = new PHPMailer;
    $message_body = "<html><body><table border=5px>";
    $message_body .= "<tr><td colspan='2' style='color : #C50B33; font-size : 20px; '><b>Request For Contact ". $_POST['url'] . "</b></td></tr>";
    $message_body .= "<tr><td>Contact's Full Name : </td><td>" . $_POST['name'] . "</td></tr>";
  
    $message_body .= "<tr><td>Contact's Email : </td><td>" . $_POST['email'] . "</td></tr>";
       $message_body .= "<tr><td>Contact's Phone : </td><td>" . $_POST['phone'] . "</td></tr>";


    $message_body .= "</table></body></html>";
   
    $mail->addAddress('getautotherapy@gmail.com');
    $mail->AddBcc('backupilogicx@gmail.com');

    $mail->Subject  = 'Contact Us Form';
    $mail->isHTML(true);
    $mail->Body = $message_body;

    if (!$mail->send()) {
        echo "Sorry, an error occurred while sending your mail. Please send again..!";
    } else {
        //echo "sent";
         echo "<p style='color: green;'>Thank you for contacting us! We will contact you soon.</p>";
        $mail->ClearAllRecipients();
        //$mail->addAddress($_POST['email']);  // Use lowercase 'email'
        $message_body = "Thank You....We Will Get Back To You Soon..";
        $mail->Subject  = 'Contact Us Form';
        $mail->Body = $message_body;
        $mail->send();
    }
} else {
    echo $error;
}
        }

        // Close database connection
        $conn->close();
    }
}
?>