
<?php
include "config.php";

if(isset($_POST['save'])){
$name=$_POST['name'];
$mobile=$_POST['mobile'];
$age=$_POST['age'];
$date=$_POST['date'];
$time=$_POST['time'];
$reason=$_POST['reason'];


$currentDate = date("Y-m-d");
$currentTime = date("H:i");

if($date == $currentDate && $time <= $currentTime){
    echo "<script>alert('Please select future time');</script>";
    exit;
}

mysqli_query($conn,"INSERT INTO appointments
(patient_name,mobile,age,visit_date,visit_time,reason)
VALUES
('$name','$mobile','$age','$date','$time','$reason')");

echo "<script>alert('Appointment Booked');</script>";
}
?>
<head>
    <link rel="stylesheet" href="assets/style.css">
    
</head>
<h2>Book Appointment</h2>

<form method="post">
<input name="name" placeholder="Patient Name" required><br><br>
<input name="mobile" placeholder="Mobile" required><br><br>
<input name="age" placeholder="Age"><br><br>
<input type="date" name="date" required><br><br>
<input type="time" name="time"><br><br>
<textarea name="reason" placeholder="Problem"></textarea><br><br>
<button name="save">Book Appointment</button>
</form>

<script>
let today = new Date().toISOString().split('T')[0];
document.querySelector("input[name='date']").setAttribute("min", today);

document.querySelector("input[name='date']").addEventListener("change", function(){
    let selectedDate = this.value;
    let todayDate = new Date().toISOString().split('T')[0];

    if(selectedDate === todayDate){
        let now = new Date();
        let h = String(now.getHours()).padStart(2,'0');
        let m = String(now.getMinutes()).padStart(2,'0');
        document.getElementById("time").setAttribute("min", h+":"+m);
    }else{
        document.getElementById("time").removeAttribute("min");
    }
});
</script>