<?php
error_reporting(E_ALL); // Enable error reporting for debugging
ini_set('display_errors', 1);

// Include the database connection file
include 'dbconnect.php';  // Make sure this is the correct path to the dbconnect.php file

// Check if the ID is provided in the URL
if (isset($_GET['id']) && is_numeric($_GET['id'])) {
    $id = $_GET['id'];  // Get the ID from the URL

    // SQL query to delete the appointment based on the ID
    $sql = "DELETE FROM appointments WHERE id = ?";

    // Prepare the statement
    if ($stmt = $conn->prepare($sql)) {
        // Bind the ID parameter to the query
        $stmt->bind_param("i", $id);

        // Execute the query
        if ($stmt->execute()) {
            // If deletion is successful, redirect to the appointment list page
            header("Location: appointment_list.php");  // Change this to your actual appointment listing page
            exit();  // Ensure no further code is executed after redirection
        } else {
            // Show error if deletion fails
            echo "Error deleting appointment: " . $stmt->error;
        }

        // Close the prepared statement
        $stmt->close();
    } else {
        // Error preparing the SQL query
        echo "Error preparing query: " . $conn->error;
    }
} else {
    // Error if no valid ID is provided
    echo "Invalid or missing appointment ID.";
}

// Close the database connection
$conn->close();
?>
