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

$msg = '';

// Handle status update
if (isset($_POST['update_status'])) {
    $id = intval($_POST['history_id']);
    $status = intval($_POST['status']);
    mysqli_query($ahk_conn, "UPDATE dlhistory SET status='$status' WHERE id='$id'");
    $msg = "Status updated successfully!";
}

$result = mysqli_query($ahk_conn, "
    SELECT * 
    FROM dlhistory 
    WHERE status IN (0,1) 
    ORDER BY id DESC
");
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>DL History</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
    <style>
        .status-track {
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 10px;
        }
        .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 mb-4 text-primary">DL History</h4>

    <?php if ($msg): ?>
    <script>
        Swal.fire({
            icon: 'info',
            title: 'Notice',
            text: <?php echo json_encode($msg); ?>
        });
    </script>
    <?php endif; ?>

    <table class="table table-bordered table-striped text-center align-middle">
        <thead class="table-dark">
            <tr>
                <th>ID</th>
                <th>DL Number</th>
                <th>Mobile Number</th>
                <th>Order ID</th>
                <th>Date</th>
                <th>Status</th>
                <th>Update Status</th>
            </tr>
        </thead>
        <tbody>
        <?php while ($row = mysqli_fetch_assoc($result)): ?>
            <tr>
                <td><?php echo $row['id']; ?></td>
                <td><?php echo htmlspecialchars($row['dl_no']); ?></td>
                <td><?php echo htmlspecialchars($row['mobile_no']); ?></td>
                <td><?php echo htmlspecialchars($row['order_id']); ?></td>
                <td><?php echo htmlspecialchars($row['date']); ?></td>
                <td>
                    <div class="status-track">
                        <?php
                            $status = intval($row['status']);
                            echo '<div class="circle ' . ($status >= 0 ? 'active-blue' : '') . '"></div>';
                            echo '<div class="circle ' . ($status >= 1 ? 'active-yellow' : '') . '"></div>';
                            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>
                    <form method="POST">
                        <select name="status" class="form-select mb-1">
                            <option value="0" <?php echo ($row['status']==0)?'selected':''; ?>>Pending</option>
                            <option value="1" <?php echo ($row['status']==1)?'selected':''; ?>>In Process</option>
                            <option value="2" <?php echo ($row['status']==2)?'selected':''; ?>>Completed</option>
                        </select>
                        <input type="hidden" name="history_id" value="<?php echo $row['id']; ?>">
                        <button type="submit" name="update_status" class="btn btn-sm btn-primary w-100">Update</button>
                    </form>
                </td>
            </tr>
        <?php endwhile; ?>
        </tbody>
    </table>
</div>
</body>
</html>
