<?php

require '../connection/conn.php';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

    // Validate inputs
    if (!isset($_POST['id']) || !isset($_POST['patient_id'])) {
        exit("Invalid request");
    }

    $id = (int) $_POST['id'];
    $patient_id = (int) $_POST['patient_id'];

    // Prepare soft delete
    $stmt = $conn->prepare("UPDATE appointment SET deleted = 1 WHERE appointment_id = ?");
    $stmt->bind_param("i", $id);

    if ($stmt->execute()) {

        file_put_contents("log.txt", "Deleted ID: $id\n", FILE_APPEND);

        header("Location: patient_detail.php?id=$patient_id");
        exit;

    } else {
        echo "<script>
                alert('Error deleting appointment.');
                window.location.href='patient_detail.php?id=$patient_id';
              </script>";
        exit;
    }

} else {
    echo "<script>
            alert('Invalid request.');
            window.location.href='appointment_list.php';
          </script>";
    exit;
}

?>