<?php
session_start();
include "dbconnect.php";

// Pagination
$limit = 10;
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$offset = ($page - 1) * $limit;

// Search
$search = $_POST['search'] ?? $_GET['search'] ?? '';
$search_query = !empty($search) ? "AND (ja.name LIKE '%$search%' OR ja.passport_no LIKE '%$search%')" : "";

// Data query
$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 1=1 $search_query
        ORDER BY t.ticket_id DESC
        LIMIT $limit OFFSET $offset";

$result = $con->query($sql);

// Count query
$count_sql = "SELECT COUNT(t.ticket_id) AS total FROM Ticketing t
              JOIN job_applicants ja ON t.job_applicant_id = ja.id
              WHERE 1=1 $search_query";
$count_result = $con->query($count_sql);
$total_rows = $count_result->fetch_assoc()['total'];
$total_pages = ceil($total_rows / $limit);
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Ticket List</title>
    <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, #f0f4ff, #e6f0ff);
            font-family: 'Segoe UI', sans-serif;
        }
        .container {
            padding: 40px 20px;
        }
        h3 {
            font-size: 2rem;
            color: #3366cc;
            font-weight: 600;
            text-align: center;
            margin-bottom: 30px;
        }
        .logout-btn {
            position: fixed;
            top: 20px;
            right: 20px;
            z-index: 1000;
        }
        .logout-btn a {
            background-color: #dc3545;
            color: white;
            padding: 10px 18px;
            border-radius: 6px;
            text-decoration: none;
            font-weight: 600;
        }
        .logout-btn a:hover {
            background-color: #c82333;
        }
        .form-control {
            border-radius: 5px;
        }
        .btn-primary, .btn-outline-danger {
            border-radius: 5px;
        }
        .table th, .table td {
            vertical-align: middle;
            text-align: center;
        }
        .table-striped tbody tr:nth-of-type(odd) {
            background-color: #f8faff;
        }
        .pagination {
            justify-content: center;
            margin-top: 20px;
        }
        .pagination .page-link {
            border-radius: 6px;
            color: #3366cc;
            border: 1px solid #ccc;
        }
        .pagination .page-item.active .page-link {
            background-color: #3366cc;
            color: white;
            border-color: #3366cc;
        }
        .pagination .page-link:hover {
            background-color: #2952a3;
            color: white;
        }
        @media (max-width: 768px) {
            .table th, .table td {
                font-size: 13px;
            }
            .btn, .form-control {
                font-size: 13px;
            }
        }
    </style>
</head>
<body>

<div class="container">
    <h3><i class="bi bi-ticket-detailed"></i> Ticket Management List</h3>

    <!-- Logout Button -->
    <div class="logout-btn">
        <a href="logout.php"><i class="bi bi-box-arrow-right me-1"></i>Logout</a>
    </div>

    <!-- Search -->
    <form method="POST" class="d-flex justify-content-center mb-4 flex-wrap gap-2">
        <input type="text" name="search" class="form-control" placeholder="Search by Name or Passport" value="<?= htmlspecialchars($search) ?>" style="max-width: 300px;">
        <button type="submit" class="btn btn-primary"><i class="bi bi-search me-1"></i>Search</button>
    </form>

    <!-- Table -->
    <div class="table-responsive">
        <table class="table table-striped table-bordered">
            <thead class="table-light">
                <tr>
                    <th>#</th>
                    <th><i class="bi bi-person-fill text-primary"></i> Name</th>
                    <th><i class="bi bi-credit-card-2-front text-secondary"></i> Passport</th>
                    <th><i class="bi bi-geo-alt-fill text-success"></i> From</th>
                    <th><i class="bi bi-geo-fill text-danger"></i> To</th>
                    <th><i class="bi bi-rulers text-info"></i> Distance</th>
                    <th><i class="bi bi-calendar-event text-warning"></i> Date</th>
                    <th><i class="bi bi-currency-rupee text-dark"></i> Amount</th>
                    <th><i class="bi bi-file-earmark-arrow-down text-muted"></i> Boarding Pass</th>
                    <th><i class="bi bi-gear-fill"></i> Actions</th>
                </tr>
            </thead>
            <tbody>
            <?php
            if ($result && $result->num_rows > 0) {
                while ($row = $result->fetch_assoc()) {
                    echo "<tr>";
                    echo "<td>{$row['ticket_id']}</td>";
                    echo "<td><i class='bi bi-person me-1 text-primary'></i>" . htmlspecialchars($row['applicant_name']) . "</td>";
                    echo "<td><i class='bi bi-credit-card text-secondary me-1'></i>" . htmlspecialchars($row['passport_no']) . "</td>";
                    echo "<td><i class='bi bi-geo-alt text-success me-1'></i>" . htmlspecialchars($row['from_location']) . "</td>";
                    echo "<td><i class='bi bi-geo text-danger me-1'></i>" . htmlspecialchars($row['to_location']) . "</td>";
                    echo "<td><i class='bi bi-rulers text-info me-1'></i>" . htmlspecialchars($row['distance']) . " km</td>";
                    echo "<td><i class='bi bi-calendar text-warning me-1'></i>" . htmlspecialchars($row['ticket_date']) . "</td>";
                    echo "<td><i class='bi bi-currency-rupee text-dark me-1'></i>" . number_format($row['amount'], 2) . "</td>";
                    echo "<td>";
                    if (!empty($row['boarding_pass_upload'])) {
                        echo "<a href='" . htmlspecialchars($row['boarding_pass_upload']) . "' target='_blank'><i class='bi bi-file-earmark-pdf-fill text-danger me-1'></i>View</a>";
                    } else {
                        echo "N/A";
                    }
                    echo "</td>";
                    echo "<td class='d-flex justify-content-center gap-2'>";
                    echo "<a href='ticketedit.php?id={$row['ticket_id']}' class='btn btn-sm btn-primary'><i class='bi bi-pencil-square'></i></a>";
                   
                    echo "</td>";
                    echo "</tr>";
                }
            } else {
                echo "<tr><td colspan='10' class='text-center'>No records found</td></tr>";
            }
            ?>
            </tbody>
        </table>
    </div>

    <!-- Pagination -->
    <nav>
        <ul class="pagination">
            <li class="page-item <?= ($page <= 1) ? 'disabled' : '' ?>">
                <a class="page-link" href="?page=1<?= ($search ? "&search=" . urlencode($search) : "") ?>">&laquo;</a>
            </li>
            <li class="page-item <?= ($page <= 1) ? 'disabled' : '' ?>">
                <a class="page-link" href="?page=<?= $page - 1 ?><?= ($search ? "&search=" . urlencode($search) : "") ?>">&lsaquo;</a>
            </li>
            <?php for ($i = 1; $i <= $total_pages; $i++) { ?>
                <li class="page-item <?= ($i == $page) ? 'active' : '' ?>">
                    <a class="page-link" href="?page=<?= $i ?><?= ($search ? "&search=" . urlencode($search) : "") ?>"><?= $i ?></a>
                </li>
            <?php } ?>
            <li class="page-item <?= ($page >= $total_pages) ? 'disabled' : '' ?>">
                <a class="page-link" href="?page=<?= $page + 1 ?><?= ($search ? "&search=" . urlencode($search) : "") ?>">&rsaquo;</a>
            </li>
            <li class="page-item <?= ($page >= $total_pages) ? 'disabled' : '' ?>">
                <a class="page-link" href="?page=<?= $total_pages ?><?= ($search ? "&search=" . urlencode($search) : "") ?>">&raquo;</a>
            </li>
        </ul>
    </nav>
</div>

</body>
</html>
