<?php

//include_once('controllers/database.php'); 

//include('models/retrieve-data.php'); 
date_default_timezone_set('Africa/Lagos');


if ($_SERVER["REQUEST_METHOD"] === "POST") {
    $user_ip = $SERVER['HTTP_CLIENT_IP'] ?? ($_SERVER['HTTP_X_FORWARDED_FOR'] ?? $_SERVER['REMOTE_ADDR']);
    // Process comprehensive form logic
    $name = isset($_POST['name']) ? mysqli_real_escape_string($conn, $_POST['name']) : '';
    $branch = isset($_POST['branch']) ? mysqli_real_escape_string($conn, $_POST['branch']) : '';
    $department = isset($_POST['department']) ? mysqli_real_escape_string($conn, $_POST['department']) : '';
    $phone = isset($_POST['phone']) ? mysqli_real_escape_string($conn, $_POST['phone']) : '';
    $date = isset($_POST['date']) ? mysqli_real_escape_string($conn, $_POST['date']) : '';
    $time = isset($_POST['time']) ? mysqli_real_escape_string($conn, $_POST['time']) : '';
    $request = isset($_POST['request']) ? mysqli_real_escape_string($conn, $_POST['request']) : '';
    $hiddenValue = isset($_POST['hidden']) ?mysqli_real_escape_string($conn, $_POST['hidden']) : '';
    // Retrieve and sanitize form data

 $appointments = [
        [
            'key' => 'appointment_id',
            'value' => 'USER001',
            'param' => 's'
        ],
        [
            'key' => 'name',
            'value' => $name,
            'param' => 's'
        ],
        [
            'key' => 'branch',
            'value' => $branch,
            'param' => 's'
        ],
        [
            'key' => 'department',
            'value' => $department,
            'param' => 's'
        ],
        [
            'key' => 'phone',
            'value' => $phone,
            'param' => 's'
        ],
        [
            'key' => 'date',
            'value' => $date,
            'param' => 's'
        ],
        [
            'key' => 'time',
            'value' => $time,
            'param' => 's'
        ],
        [
            'key' => 'request',
            'value' => $request,
            'param' => 's'
        ],
        [
            'key' => 'user_ip',
            'value' => $user_ip,
            'param' => 's'
        ]
    ];

insertRecord($conn, 'appointments', $appointments);
//header("Location: index.html");


$prefix = '#USER'; // Customize the prefix as needed

// Retrieve existing rows
$query = "SELECT id FROM appointments";
$result = mysqli_query($conn, $query);

if ($result) {
    while ($row = mysqli_fetch_assoc($result)) {
        $appointment_id = $prefix . $row['id'];

        // Update the row with the generated booking ID
        $updateQuery = "UPDATE appointments SET appointment_id = '$appointment_id' WHERE id = " . $row['id'];
        mysqli_query($conn, $updateQuery);
    }

    // Free result set
    mysqli_free_result($result);
} else {
    echo "Error retrieving data: " . mysqli_error($conn);
}




        // Use prepared statement to prevent SQL injection
       // $stmt = $conn->prepare("INSERT INTO bookings (title, fullname, email, phone, dob, room_name, image, room_type, room_number, checkin_date, checkout_date, kids, adults, special_request, days_logged, room_price, total_price, user_ip) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");

        // Bind parameters
       // $stmt->bind_param("sssssssssssiisiiis", $title, $fullname, $email, $phone, $dateOfBirth, $room_choice, $order_image, $room_type, $room_number, $checkin, $checkout, $numKids, $numAdults, $specialRequests, $numDays, $roomprice, $grandPrice, $user_ip);

        // Execute the statement
       // if ($stmt->execute()) {
      //      echo "Submitted";
      //      log_activity("$title $fullname booked $room_choice now <a href'single-room.php?id=1'> View <a/>");
       // } else {
            // Log the error or display a user-friendly message
       //     echo "Error submitting booking. Please try again later.";
       // }

        // Close the statement
       // $stmt->close();
  

   
    // ... (rest of the code)


  
}





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

<head>
    
</head>
<body class="">
    <!-- Start Main Body -->
    <div class="main-body">
        <!-- Start Appointment/faqs -->
        <section class="book-appointment parallax section-padding" id="book-appointment"  style="background-image: url(assets/images/7.jpg);">
            <div class="overlay overlay-bg-black"></div>
            <div class="pattern"></div>
            <div class="container">
                <div class="row">
                    <div class="col-lg-12">
                        <div class="booking-form p-relative">
                            <div class="book-form-wrapper">
                                <div class="section-header">
                                    <div class="section-heading mx-auto text-center">
                                        <h3 class="text-custom-black fw-700">Book an Appointment</h3>
                                        <img src="assets/images/title-icon.png" class="mb-xl-20" alt="img">
                                        <div class="section-description">
                                            <p class="text-light-white">
                                            Don't wait to take control of your health. Book an appointment with one of our expert medical professionals today. Experience personalized and compassionate care. Schedule now.
                                            </p>
                                        </div>
                                    </div>
                                </div>
                                <form class="form-layout-1" method="POST" action="<?= $_SERVER['PHP_SELF']?>">
                                    <div class="row">
                                        <div class="col-lg-4 col-md-6">
                                            <div class="form-group">
                                                <div class="icon">
                                                    <i class="flaticon-user"></i>
                                                </div>
                                                <label>Your Name</label>
                                                <input type="text" name="name" class="form-control" placeholder="Your Name">
                                            </div>
                                        </div>
                                        <div class="col-lg-4 col-md-6">
                                            <div class="form-group">
                                                <div class="icon">
                                                    <i class="flaticon-stethoscope"></i>
                                                </div>
                                                <label>Select Preferred Branch</label>
                                                <select class="form-control custom-select" name="branch">
                                                    <option value="">Select a Branch</option>
                                                    <option value="Okebola">Okebola Ibadan Branch</option>
                                                    <option value="Oremeji">Oremeji Ibadan Branch</option>
                                                    
                                                </select>
                                            </div>
                                        </div>
                                        <div class="col-lg-4 col-md-6">
                                            <div class="form-group">
                                                <div class="icon">
                                                    <i class="flaticon-information"></i>
                                                </div>
                                                <label>Select Your Department</label>
                                                <select class="form-control custom-select" name="department">
                                                    <option value="Cardiology">Cardiology</option>
                                                    <option value="null">Urologic </option>
                                                    <option value="null">Neurology</option>
                                                    <option value="null">Medicine</option>
                                                    <option value="null">Surgery</option>
                                                    <option value="null">Urology</option>
                                                    <option value="null">Radiology</option>
                                                    <option value="null">Neurosurgery</option>
                                                    <option value="null">Bariatric</option>
                                                </select>
                                            </div>
                                        </div>
                                        <div class="col-lg-4 col-md-6">
                                            <div class="form-group">
                                                <div class="icon">
                                                    <i class="flaticon-telephone"></i>
                                                </div>
                                                <label>Your Phone</label>
                                                <input type="text" name="phone" class="form-control" placeholder="Enter your Phone">
                                            </div>
                                        </div>
                                        <div class="col-lg-4 col-md-6">
                                            <div class="form-group">
                                                <div class="icon">
                                                    <i class="flaticon-calendar"></i>
                                                </div>
                                                <label>Select Date</label>
                                                <input type="text" name="date" class="form-control datepickr" placeholder="dd/mm/yy" readonly>
                                            </div>
                                        </div>
                                        <div class="col-lg-4 col-md-6">
                                            <div class="form-group">
                                                <div class="icon">
                                                    <i class="flaticon-wall-clock"></i>
                                                </div>
                                                <label>Add Time</label>
                                                <input type="text" name="time" class="form-control timepickr" placeholder="12:00 PM" readonly>
                                            </div>
                                        </div>
                                        <div class="col-md-12 mb-xl-20">
                                            <div class="form-group">
                                                <div class="icon">
                                                    <i class="flaticon-conversation"></i>
                                                </div>
                                                <label>Special Request</label>
                                                <textarea class="form-control" rows="2" name="request" placeholder="Type Message"></textarea>
                                            </div>
                                        </div>
                                        <div class="col-12 text-center">
                                            <button type="submit" class="btn-first btn-submit text-light-blue fw-600">Make Appointment</button>
                                        </div>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </section>
        <!-- End Appointment/faqs -->
 
    
</body>
</html>
