<?php
session_start();
include "dbconnect.php";

if (!isset($_GET['id']) || empty($_GET['id'])) {
    echo "Ticket ID is required!";
    exit;
}

$ticket_id = $_GET['id'];

// Fetch ticket data
$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();

// Handle update
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $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'];

    // File upload
    if (!empty($_FILES['boarding_pass']['name'])) {
        $target_dir = "uploads/";
        $target_file = $target_dir . basename($_FILES['boarding_pass']['name']);
        if (move_uploaded_file($_FILES['boarding_pass']['tmp_name'], $target_file)) {
            $boarding_pass_upload = $target_file;
        } else {
            echo "Error uploading file.";
        }
    }

    // Update query
    $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");
        exit;
    } else {
        echo "Error updating ticket: " . $stmt_update->error;
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Edit Ticket</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <!-- Bootstrap + Icons -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.5/font/bootstrap-icons.css" rel="stylesheet">

    <style>
        body {
            background: linear-gradient(135deg, #e0f7fa, #ffffff);
            font-family: 'Segoe UI', sans-serif;
        }

        .container {
            max-width: 800px;
            margin: auto;
            padding: 40px 20px;
        }

        h3 {
            color: #007bff;
            font-weight: 600;
            text-align: center;
            margin-bottom: 30px;
        }

        .card {
            box-shadow: 0 0 25px rgba(0, 0, 0, 0.08);
            border: none;
            border-radius: 12px;
        }

        .card-body {
            padding: 30px;
        }

        .form-label {
            font-weight: 600;
        }

        .form-control {
            border-radius: 8px;
        }

        .btn-primary {
            border-radius: 8px;
        }

        .form-text {
            font-size: 0.85rem;
            color: #6c757d;
        }

        .logout-btn {
            position: absolute;
            top: 20px;
            right: 20px;
        }

        .logout-btn a {
            background-color: #dc3545;
            color: white;
            padding: 10px 16px;
            border-radius: 30px;
            text-decoration: none;
            font-weight: 600;
        }

        .logout-btn a:hover {
            background-color: #c82333;
        }

        @media (max-width: 576px) {
            .logout-btn {
                position: static;
                text-align: right;
                margin-bottom: 20px;
            }

            h3 {
                font-size: 1.5rem;
            }

            .card-body {
                padding: 20px;
            }
        }
    </style>
</head>
<body>

<!-- Logout Button -->
<div class="logout-btn">
    <a href="logout.php"><i class="bi bi-box-arrow-right"></i> Logout</a>
</div>

<div class="container">
    <h3><i class="bi bi-pencil-square"></i> Edit Ticket</h3>

    <div class="card">
        <div class="card-body">

            <form method="POST" enctype="multipart/form-data">
                <div class="mb-3">
                    <label for="applicant_name" class="form-label"><i class="bi bi-person-fill"></i> Applicant Name</label>
                    <input type="text" class="form-control" id="applicant_name" name="applicant_name" value="<?= htmlspecialchars($row['applicant_name']) ?>" readonly>
                </div>

                <div class="mb-3">
                    <label for="passport_no" class="form-label"><i class="bi bi-passport"></i> Passport No</label>
                    <input type="text" class="form-control" id="passport_no" name="passport_no" value="<?= htmlspecialchars($row['passport_no']) ?>" readonly>
                </div>

                <div class="mb-3">
                    <label for="from_location" class="form-label"><i class="bi bi-geo-alt-fill"></i> From Location</label>
                    <input type="text" class="form-control" id="from_location" name="from_location" value="<?= htmlspecialchars($row['from_location']) ?>" required>
                </div>

                <div class="mb-3">
                    <label for="to_location" class="form-label"><i class="bi bi-geo-fill"></i> To Location</label>
                    <input type="text" class="form-control" id="to_location" name="to_location" value="<?= htmlspecialchars($row['to_location']) ?>" required>
                </div>

                <div class="mb-3">
                    <label for="distance" class="form-label"><i class="bi bi-signpost-2-fill"></i> Distance (in km)</label>
                    <input type="number" step="0.1" class="form-control" id="distance" name="distance" value="<?= htmlspecialchars($row['distance']) ?>" required>
                </div>

                <div class="mb-3">
                    <label for="ticket_date" class="form-label"><i class="bi bi-calendar-event-fill"></i> Ticket Date</label>
                    <input type="date" class="form-control" id="ticket_date" name="ticket_date" value="<?= htmlspecialchars($row['ticket_date']) ?>" required>
                </div>

                <div class="mb-3">
                    <label for="amount" class="form-label"><i class="bi bi-currency-rupee"></i> Amount (INR)</label>
                    <input type="number" step="0.01" class="form-control" id="amount" name="amount" value="<?= htmlspecialchars($row['amount']) ?>" required>
                </div>

                <div class="mb-4">
                    <label for="boarding_pass" class="form-label"><i class="bi bi-file-earmark-arrow-up-fill"></i> Boarding Pass</label>
                    <input type="file" class="form-control" id="boarding_pass" name="boarding_pass">
                    <small class="form-text">Leave blank to keep the existing file.</small>
                    <?php if (!empty($row['boarding_pass_upload'])): ?>
                        <p class="mt-2">
                            <strong>Current:</strong> 
                            <a href="<?= $row['boarding_pass_upload'] ?>" target="_blank" class="text-primary">View Boarding Pass</a>
                        </p>
                    <?php endif; ?>
                </div>

                <div class="d-grid">
                    <button type="submit" class="btn btn-primary"><i class="bi bi-save2-fill"></i> Update Ticket</button>
                </div>
            </form>

        </div>
    </div>
</div>

<!-- Bootstrap Bundle JS -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>

</body>
</html>
