<?php
session_start();
include('con.php');
include('header.php');

if (!isset($_SESSION['username'])) {
    header("Location: login.php");
    exit;
}

$username = $_SESSION['username'];

// Fetch PUCC history for logged-in user
$result = mysqli_query($ahk_conn, "SELECT * FROM puchistory WHERE custno='$username' ORDER BY id DESC");
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>PUCC History</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        .status-track {
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 15px;
        }
        .circle {
            width: 15px;
            height: 15px;
            border-radius: 50%;
            background-color: #ccc;
            transition: background-color 0.3s;
        }
        .circle.active-blue { background-color: #0d6efd; }   /* Blue */
        .circle.active-yellow { background-color: #ffc107; } /* Yellow */
        .circle.active-green { background-color: #28a745; }  /* Green */
    </style>
</head>
<body>
<div class="container mt-5">
    <h4 class="text-center text-primary mb-4">PUCC History</h4>
    <table class="table table-bordered table-striped align-middle text-center">
        <thead class="table-dark">
            <tr>
                <th>ID</th>
                <th>Vehicle Number</th>
                <th>Transaction ID</th>
                <th>Date</th>
                <th>Status</th>
                <th>Uploaded Image</th>
                <th>Certificate</th>
            </tr>
        </thead>
        <tbody>
        <?php if (mysqli_num_rows($result) > 0): ?>
            <?php while ($row = mysqli_fetch_assoc($result)): ?>
                <tr>
                    <td><?php echo $row['id']; ?></td>
                    <td><?php echo htmlspecialchars($row['number']); ?></td>
                    <td><?php echo htmlspecialchars($row['tnsid']); ?></td>
                    <td><?php echo htmlspecialchars($row['date']); ?></td>
                    <td>
                        <div class="status-track">
                            <?php
                            $status = intval($row['status']);
                            // Circle 1 (blue)
                            echo '<div class="circle ' . ($status >= 0 ? 'active-blue' : '') . '"></div>';
                            // Circle 2 (yellow)
                            echo '<div class="circle ' . ($status >= 1 ? 'active-yellow' : '') . '"></div>';
                            // Circle 3 (green)
                            echo '<div class="circle ' . ($status >= 2 ? 'active-green' : '') . '"></div>';
                            ?>
                        </div>
                        <small>
                            <?php
                            if ($status == 0) echo "Pending";
                            elseif ($status == 1) echo "In Process";
                            elseif ($status == 2) echo "Completed";
                            else echo "Unknown";
                            ?>
                        </small>
                    </td>
                    <td>
                        <?php if (!empty($row['backimage'])): ?>
                            <a href="<?php echo htmlspecialchars($row['backimage']); ?>" target="_blank" class="btn btn-sm btn-outline-primary">
                                View
                            </a>
                        <?php else: ?>
                            N/A
                        <?php endif; ?>
                    </td>
                    <td>
                        <?php if (!empty($row['cert'])): ?>
                            <a href="<?php echo htmlspecialchars($row['cert']); ?>" download class="btn btn-sm btn-success">
                                Download
                            </a>
                        <?php else: ?>
                            <button class="btn btn-sm btn-secondary" disabled>Download</button>
                        <?php endif; ?>
                    </td>
                </tr>
            <?php endwhile; ?>
        <?php else: ?>
            <tr><td colspan="7" class="text-center text-muted">No PUCC records found.</td></tr>
        <?php endif; ?>
        </tbody>
    </table>
</div>
<?php include('footer.php'); ?>
</body>
</html>
