<?php
session_start();
// Password protection check
if(!isset($_SESSION['admin_logged_in'])) {
    header("Location: login.php");
    exit;
}

include 'db_config.php';

// Pagination Logic
$limit = 10; 
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$start = ($page - 1) * $limit;

// Sabhi columns fetch karne ki query
$sql = "SELECT * FROM hsrp_applications ORDER BY created_at DESC LIMIT $start, $limit";
$result = $conn->query($sql);

// Total records count for pagination
$totalResult = $conn->query("SELECT COUNT(id) AS total FROM hsrp_applications");
$totalRows = $totalResult->fetch_assoc()['total'];
$totalPages = ceil($totalRows / $limit);
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>HSRP Admin - All Leads</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        :root { --hsrp-blue: #243875; --hsrp-yellow: #fde58a; }
        body { background-color: #f0f4f8; font-family: 'Poppins', sans-serif; }
        
        .admin-nav { background: var(--hsrp-blue); color: white; padding: 15px 30px; }
        
        .main-container { padding: 30px; }
        .data-card { 
            background: white; 
            border-radius: 15px; 
            box-shadow: 0 10px 25px rgba(0,0,0,0.05);
            padding: 20px;
        }

        /* Table Responsive Tweaks */
        .table-responsive { border-radius: 10px; overflow: hidden; }
        .table thead { background: var(--hsrp-blue); color: white; white-space: nowrap; }
        .table tbody td { vertical-align: middle; white-space: nowrap; font-size: 0.9rem; }
        
        .app-id-badge { background: #eef2ff; color: #324B96; font-weight: bold; border: 1px solid #d1dbff; }
        .utr-badge { background: #fff9db; color: #856404; font-weight: bold; border: 1px solid #ffeeba; }
        
        .pagination .page-link { color: var(--hsrp-blue); }
        .pagination .page-item.active .page-link { background-color: var(--hsrp-blue); border-color: var(--hsrp-blue); }
    </style>
</head>
<body>

<div class="admin-nav d-flex justify-content-between align-items-center">
    <h4 class="mb-0 fw-bold">HSRP Admin Portal</h4>
    <div>
        <span class="me-3 small text-white-50">Welcome, Admin</span>
        <a href="logout.php" class="btn btn-outline-light btn-sm">Logout</a>
    </div>
</div>

<div class="main-container">
    <div class="data-card">
        <div class="d-flex justify-content-between align-items-center mb-4">
            <h3 class="fw-bold mb-0">Customer Applications</h3>
            <span class="badge bg-dark">Total Leads: <?php echo $totalRows; ?></span>
        </div>

        <div class="table-responsive">
            <table class="table table-hover table-bordered">
                <thead>
                    <tr>
                        <th>Date</th>
                        <th>App. ID</th>
                        <th>UTR Number</th>
                        <th>Reg. Number</th>
                        <th>Full Name</th>
                        <th>Phone</th>
                        <th>State</th>
                        <th>Vehicle Type</th>
                        <th>Plates</th>
                        <th>Chassis (Last 6)</th>
                        <th>Engine (Last 6)</th>
                        <th>Email</th>
                        <th>Address</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if($result->num_rows > 0): ?>
                        <?php while($row = $result->fetch_assoc()): ?>
                        <tr>
                            <td><small><?php echo date('d-M-y H:i', strtotime($row['created_at'])); ?></small></td>
                            <td><span class="badge app-id-badge"><?php echo $row['app_id']; ?></span></td>
                            <td><span class="badge utr-badge"><?php echo ($row['utr_number'] ? $row['utr_number'] : 'PENDING'); ?></span></td>
                            <td class="fw-bold text-uppercase"><?php echo $row['registration_number']; ?></td>
                            <td><?php echo $row['full_name']; ?></td>
                            <td><?php echo $row['phone']; ?></td>
                            <td><?php echo $row['state']; ?></td>
                            <td>
                                <?php 
                                    if($row['vehicle_type'] == '1') echo "2-Wheeler";
                                    elseif($row['vehicle_type'] == '3') echo "4-Wheeler";
                                    else echo "Other";
                                ?>
                            </td>
                            <td><?php echo $row['number_of_plates']; ?></td>
                            <td><code><?php echo $row['chassis_number']; ?></code></td>
                            <td><code><?php echo $row['engine_number']; ?></code></td>
                            <td><small><?php echo $row['email']; ?></small></td>
                            <td>
                                <span class="d-inline-block text-truncate" style="max-width: 150px;" title="<?php echo $row['address']; ?>">
                                    <?php echo $row['address']; ?>
                                </span>
                            </td>
                        </tr>
                        <?php endwhile; ?>
                    <?php else: ?>
                        <tr><td colspan="13" class="text-center py-4">No applications found.</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>

        <nav class="mt-4">
            <ul class="pagination justify-content-center">
                <li class="page-item <?php echo ($page <= 1) ? 'disabled' : ''; ?>">
                    <a class="page-link" href="?page=<?php