<?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 data from the database
$sql = "SELECT t.ticket_id, ja.name AS applicant_name, ja.passport_no, t.from_location, t.to_location, 
        t.distance, t.ticket_date, t.amount, t.boarding_pass_upload
        FROM Ticketing t
        JOIN job_applicants ja ON t.job_applicant_id = ja.id
        WHERE t.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();

// Update ticket data if form is submitted
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $applicant_name = $_POST['applicant_name'];
    $passport_no = $_POST['passport_no'];
    $from_location = $_POST['from_location'];
    $to_location = $_POST['to_location'];
    $distance = $_POST['distance'];
    $ticket_date = $_POST['ticket_date'];
    $amount = $_POST['amount'];
    $boarding_pass_upload = $row['boarding_pass_upload']; // Default, in case no new file is uploaded

    // Handle boarding pass upload
    if (!empty($_FILES['boarding_pass']['name'])) {
        $target_dir = "uploads/";
        // Add unique prefix to prevent overwriting
        $new_filename = uniqid() . "-" . basename($_FILES['boarding_pass']['name']);
        $target_file = $target_dir . $new_filename;
        if (move_uploaded_file($_FILES['boarding_pass']['tmp_name'], $target_file)) {
            $boarding_pass_upload = $target_file;
        } else {
            echo "<div class='alert alert-danger mt-3'>Sorry, there was an error uploading your file.</div>";
        }
    }

    // Update the ticket in the database
    $update_sql = "UPDATE Ticketing SET 
                    from_location = ?, 
                    to_location = ?, 
                    distance = ?, 
                    ticket_date = ?, 
                    amount = ?, 
                    boarding_pass_upload = ? 
                    WHERE ticket_id = ?";

    $stmt_update = $con->prepare($update_sql);
    $stmt_update->bind_param("ssdsdsi", $from_location, $to_location, $distance, $ticket_date, $amount, $boarding_pass_upload, $ticket_id);
    
    if ($stmt_update->execute()) {
        header("Location: ticket_list.php"); // Redirect to the ticket list page after successful update
        exit;
    } else {
        echo "<div class='alert alert-danger mt-3'>Error updating ticket: " . $stmt_update->error . "</div>";
    }
}

?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Edit Ticket</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet" />
    <style>
        /* Background and fonts */
      /* Background and fonts */
body {
    background: #f4f6f8; /* very light gray */
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    min-height: 100vh;
    color: #212529; /* dark gray */
    margin: 0;
    padding-top: 80px;
    padding-bottom: 50px;
}

/* Container styling */
.container {
    max-width: 600px;
    background: #ffffff; /* pure white */
    padding: 30px 40px;
    border-radius: 8px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

/* Title */
h3 {
    font-size: 2.5rem;
    font-weight: 700;
    text-align: center;
    margin-bottom: 40px;
    color: #0d6efd; /* bootstrap primary blue */
}

/* Form group margin */
.form-group {
    margin-bottom: 1.5rem;
}

/* Form controls */
.form-control {
    border-radius: 6px;
    border: 1px solid #ced4da;
    padding: 10px 12px;
    font-size: 1rem;
    background: #fff;
    color: #495057;
    transition: border-color 0.3s ease, box-shadow 0.3s ease;
}
.form-control:focus {
    border-color: #0d6efd;
    box-shadow: 0 0 0 0.25rem rgba(13, 110, 253, 0.25);
    outline: none;
}

/* Labels */
label {
    font-weight: 600;
    color: #212529;
}

/* Form text */
.form-text {
    color: #6c757d;
    font-size: 0.85rem;
}

/* Buttons */
.btn-primary {
    background-color: #0d6efd;
    border: none;
    padding: 12px 25px;
    font-weight: 600;
    border-radius: 6px;
    width: 100%;
    font-size: 1.1rem;
    box-shadow: 0 4px 8px rgba(13, 110, 253, 0.4);
    transition: background-color 0.3s ease;
}
.btn-primary:hover,
.btn-primary:focus {
    background-color: #084298;
    box-shadow: 0 6px 12px rgba(8, 66, 152, 0.6);
}

/* Logout button */
.logout-btn {
    position: fixed;
    top: 20px;
    right: 20px;
    background-color: #dc3545;
    color: white;
    border-radius: 6px;
    padding: 10px 22px;
    font-weight: 700;
    box-shadow: 0 2px 8px rgba(220, 53, 69, 0.5);
    text-decoration: none;
    transition: background-color 0.3s ease;
    z-index: 1000;
}
.logout-btn:hover,
.logout-btn:focus {
    background-color: #a71d2a;
    color: white;
    text-decoration: none;
}

/* Current Boarding Pass link */
p strong {
    color: #0d6efd;
}
p a {
    color: #0d6efd;
    text-decoration: underline;
}
p a:hover {
    color: #084298;
    text-decoration: none;
}

/* Responsive */
@media (max-width: 767px) {
    body {
        padding-top: 100px;
        padding-left: 15px;
        padding-right: 15px;
    }
    .container {
        padding: 25px 20px;
        max-width: 100%;
    }
    h3 {
        font-size: 2rem;
        margin-bottom: 30px;
    }
    .btn-primary {
        font-size: 1rem;
        padding: 12px;
    }
    .logout-btn {
        padding: 8px 16px;
        font-size: 0.9rem;
        top: 15px;
        right: 15px;
    }
}

    </style>
</head>
<body>

<!-- Logout Button -->
<a href="logout.php" class="logout-btn" aria-label="Logout">Logout</a>

<div class="container" role="main" aria-labelledby="edit-ticket-title">
    <h3 id="edit-ticket-title">Edit Ticket</h3>

    <!-- Edit Ticket Form -->
    <form method="POST" enctype="multipart/form-data" novalidate>
        <div class="form-group">
            <label for="applicant_name">Applicant Name</label>
            <input type="text" class="form-control" id="applicant_name" name="applicant_name" value="<?php echo htmlspecialchars($row['applicant_name']); ?>" readonly />
        </div>

        <div class="form-group">
            <label for="passport_no">Passport No</label>
            <input type="text" class="form-control" id="passport_no" name="passport_no" value="<?php echo htmlspecialchars($row['passport_no']); ?>" readonly />
        </div>

        <div class="form-group">
            <label for="from_location">From Location</label>
            <input type="text" class="form-control" id="from_location" name="from_location" value="<?php echo htmlspecialchars($row['from_location']); ?>" required />
        </div>

        <div class="form-group">
            <label for="to_location">To Location</label>
            <input type="text" class="form-control" id="to_location" name="to_location" value="<?php echo htmlspecialchars($row['to_location']); ?>" required />
        </div>

        <div class="form-group">
            <label for="distance">Distance (in km)</label>
            <input type="number" class="form-control" id="distance" name="distance" value="<?php echo htmlspecialchars($row['distance']); ?>" required min="0" step="0.01" />
        </div>

        <div class="form-group">
            <label for="ticket_date">Ticket Date</label>
            <input type="date" class="form-control" id="ticket_date" name="ticket_date" value="<?php echo htmlspecialchars($row['ticket_date']); ?>" required />
        </div>

        <div class="form-group">
            <label for="amount">Amount (INR)</label>
            <input type="number" class="form-control" id="amount" name="amount" value="<?php echo htmlspecialchars($row['amount']); ?>" required min="0" step="0.01" />
        </div>

        <div class="form-group">
            <label for="boarding_pass">Boarding Pass</label>
            <input type="file" class="form-control" id="boarding_pass" name="boarding_pass" accept="image/*,application/pdf" />
            <small class="form-text">Leave it blank if you don't want to update the boarding pass.</small>
            <?php if ($row['boarding_pass_upload']) { ?>
                <p><strong>Current Boarding Pass:</strong> <a href="<?php echo htmlspecialchars($row['boarding_pass_upload']); ?>" target="_blank" rel="noopener noreferrer">View</a></p>
            <?php } ?>
        </div>

        <button type="submit" class="btn btn-primary" aria-label="Update Ticket">Update Ticket</button>
    </form>
</div>

</body>
</html>
