<?php
/**
 * cancel_appointment.php
 * Secure appointment cancellation handler
 */

session_start();
require_once __DIR__ . '/../../dripend/includes/db.php';


/* ------------------------------------
   Fetch Appointment
------------------------------------- */
$stmt = $conn->prepare("
    SELECT a.appointment_id, a.status, s.status_code
    FROM appointment a
    INNER JOIN appointment_status s ON a.status = s.status_id
    WHERE a.appointment_id = ?
    LIMIT 1
");
$stmt->bind_param("s", $appointment_id);
$stmt->execute();
$appointment = $stmt->get_result()->fetch_assoc();
$stmt->close();

if (!$appointment || $appointment['status_code'] === 'CONSULTED') {
    header("Location: ../patient/patient_management_dashboard.php");
    exit;
}

/* ------------------------------------
   Handle Cancellation Action
------------------------------------- */
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['confirm_cancel'])) {

    $stmt = $conn->prepare("
        SELECT status_id 
        FROM appointment_status 
        WHERE status_code = 'CANCELLED'
        LIMIT 1
    ");
    $stmt->execute();
    $statusRow = $stmt->get_result()->fetch_assoc();
    $stmt->close();

    if ($statusRow) {
        $cancelled_status_id = (int)$statusRow['status_id'];

        $stmt = $conn->prepare("
            UPDATE appointment
            SET status = ?
            WHERE appointment_id = ?
        ");
        $stmt->bind_param("is", $cancelled_status_id, $appointment_id);
        $stmt->execute();
        $stmt->close();
    }

    header("Location: ../patient/patient_management_dashboard.php");
    exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Cancel Appointment</title>
<meta name="viewport" content="width=device-width, initial-scale=1">

<style>
body {
    margin: 0;
    background: rgba(0,0,0,0.65);
    font-family: "Segoe UI", Tahoma, sans-serif;
}

.modal-overlay {
    position: fixed;
    inset: 0;
    display: flex;
    justify-content: center;
    align-items: center;
}

.modal {
    background: #ffffff;
    width: 440px;
    max-width: 92%;
    border-radius: 16px;
    padding: 26px;
    text-align: center;
    box-shadow: 0 14px 50px rgba(0,0,0,0.35);
    animation: popIn 0.35s ease;
}

@keyframes popIn {
    from { transform: scale(0.88); opacity: 0; }
    to { transform: scale(1); opacity: 1; }
}

.icon {
    width: 80px;
    height: 80px;
    border-radius: 50%;
    background: #a8020e;
    color: #fff;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 38px;
    margin: 0 auto 14px;
}

h2 {
    color: #a8020e;
    margin-bottom: 8px;
}

p {
    font-size: 14px;
    color: #444;
    margin-bottom: 18px;
}

.app-ref {
    background: #f9e5e6;
    color: #a8020e;
    font-weight: 700;
    padding: 10px;
    border-radius: 8px;
    margin-bottom: 20px;
}

.actions {
    display: flex;
    gap: 12px;
    justify-content: center;
}

button {
    padding: 12px 18px;
    border-radius: 10px;
    border: none;
    font-weight: 700;
    cursor: pointer;
    font-size: 14px;
}

.btn-cancel {
    background: #a8020e;
    color: #fff;
}

.btn-back {
    background: #eef2f6;
    color: #333;
}

.btn-cancel:hover {
    background: #8c010c;
}

.btn-back:hover {
    background: #dfe5eb;
}

@media (max-width: 480px) {
    .actions {
        flex-direction: column;
    }
}
</style>
</head>

<body>
<div class="modal-overlay">
    <div class="modal">
        <div class="icon">!</div>
        <h2>Cancel Appointment</h2>
        <p>
            You are about to cancel this appointment. This action is irreversible
            and will immediately update the appointment status.
        </p>

        <div class="app-ref">
            Appointment ID: <?php echo htmlspecialchars($appointment_id); ?>
        </div>

        <form method="post">
            <div class="actions">
                <button type="submit" name="confirm_cancel" class="btn-cancel">
                    Confirm Cancellation
                </button>
                <button type="button" class="btn-back"
                        onclick="window.history.back()">
                    Go Back
                </button>
            </div>
        </form>
    </div>
</div>
</body>
</html>
