<?php
session_start();

if (!isset($_SESSION['user_id'])) {
    header("Location: /beauty_pos/login.php");
    exit;
}

include_once "../config/database.php";

if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
    header("Location: /beauty_pos/appointments/index.php");
    exit;
}

$id = (int)$_GET['id'];

$check = mysqli_query($conn,"
SELECT id
FROM appointments
WHERE id='$id'
LIMIT 1
");

if(mysqli_num_rows($check)==0){

    $_SESSION['error']="Appointment not found.";

    header("Location: /beauty_pos/appointments/index.php");
    exit;

}

$delete=mysqli_query($conn,"
DELETE FROM appointments
WHERE id='$id'
");

if($delete){

    $_SESSION['success']="Appointment deleted successfully.";

}else{

    $_SESSION['error']="Unable to delete appointment.";

}

header("Location: /beauty_pos/appointments/index.php");
exit;