<?php
include 'db_connect.php';

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = $_POST['name'];
    $phone = $_POST['phone'];
    $doctor = $_POST['doctor'];
    $date = $_POST['date'];

    $sql = "INSERT INTO appointments (patient_name, phone, doctor, preferred_date) 
            VALUES ('$name', '$phone', '$doctor', '$date')";

    if ($conn->query($sql) === TRUE) {
        // Redirect to WhatsApp with pre-filled message
        $wa_message = "Hello, I want to book an appointment for *$name* with *$doctor* on *$date*.";
        $wa_url = "https://wa.me/919922346009?text=" . urlencode($wa_message);
        
        echo "<script>window.location.href='$wa_url';</script>";
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }
}
?>