<?php
session_start();
include "dbconnect.php"; // Database Connection

// Check if ticket ID is passed in the URL
if (!isset($_GET['id']) || empty($_GET['id'])) {
    echo "Ticket ID is required!";
    exit;
}

$ticket_id = $_GET['id'];

// Fetch the ticket details to check for the boarding pass file and delete it if necessary
$sql = "SELECT boarding_pass_upload FROM Ticketing WHERE ticket_id = ?";
$stmt = $con->prepare($sql);
$stmt->bind_param("i", $ticket_id);
$stmt->execute();
$result = $stmt->get_result();

if ($result->num_rows == 0) {
    echo "Ticket not found!";
    exit;
}

$row = $result->fetch_assoc();

// Check if there is a boarding pass file and delete it from the server
if (!empty($row['boarding_pass_upload']) && file_exists($row['boarding_pass_upload'])) {
    unlink($row['boarding_pass_upload']); // Delete the boarding pass file from the server
}

// Delete the ticket record from the database
$delete_sql = "DELETE FROM Ticketing WHERE ticket_id = ?";
$stmt_delete = $con->prepare($delete_sql);
$stmt_delete->bind_param("i", $ticket_id);

if ($stmt_delete->execute()) {
    header("Location: ticketdelete.php"); // Redirect to the ticket list page after successful deletion
    exit;
} else {
    echo "Error deleting ticket: " . $stmt_delete->error;
}
?>
