<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
require './partials/head.php';
require '../config/message/error_success.php';
?>
<?php
function formatDateTime($datetime) {
    if (!$datetime) {
        return "—"; // return dash if null/empty
    }
    return date("M d, Y h:i A", strtotime($datetime));
}
?>

<body>
    <?php require './partials/sidebar.php'; ?>
    <main class="dashboard-main">
        <?php require './partials/header.php'; ?>
        <div class="dashboard-main-body">
            <div class="d-flex flex-wrap align-items-center justify-content-between gap-3 mb-24">
                <h6 class="fw-semibold mb-0">Pending shipping</h6>
                <ul class="d-flex align-items-center gap-2">
                    <li class="fw-medium">
                        <a href="dashboard" class="d-flex align-items-center gap-1 hover-text-primary">
                            <iconify-icon icon="solar:home-smile-angle-outline" class="icon text-lg"></iconify-icon>
                            Dashboard
                        </a>
                    </li>
                </ul>
            </div>
            <div class="row gy-4">
                <div class="col-lg-12">
                    <div class="card">
                        <div class="card-body">
                            <div class="table-responsive">
                                <table class="table table-hover style-1 bordered-table mb-0" id="dataTable">
                                    <thead>
                                        <tr>
                                            <th>#Date</th>
                                            <th>#Tracking Number</th>
                                            <th>User Name</th>
                                            <th>User Email</th>
                                            <th>User Number</th>
                                            <th>Status</th>
                                            <th>Action</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php
                                        // Fetch only pending packages
                                        $query = "SELECT * FROM packages WHERE status = 'processing' ORDER BY user_name ASC";
                                        $stmt = $conn->prepare($query);
                                        $stmt->execute();
                                        $result = $stmt->get_result();
                                
                                        if ($result->num_rows > 0) {
                                            while ($row = $result->fetch_assoc()) {
                                                // Choose a badge color based on status
                                                $status = strtolower($row['status']);
                                                $badgeClass = 'secondary'; // default
                                                if ($status === 'pending') $badgeClass = 'warning';
                                                if ($status === 'completed') $badgeClass = 'success';
                                                if ($status === 'delivered') $badgeClass = 'primary';
                                        ?>
                                                <tr>
                                                    <td><?= formatDateTime($row['date']) ?></td>
                                                    <td><?= $row['trackNo'] ?></td>
                                                    <td><?= $row['user_name'] ?></td>
                                                    <td><?= $row['user_email'] ?></td>
                                                    <td><?= $row['user_number'] ?></td>
                                                    <td>
                                                        <span class="badge bg-<?= $badgeClass ?>"><?= ucfirst($row['status']) ?></span>
                                                    </td>
                                                    <td>
                                                        <a href="details/<?= $row['unique_Id'] ?>" class="btn btn-warning btn-sm">
                                                            <i class="fas fa-eye" style="color: white;"></i> Details
                                                        </a>
                                                    </td>
                                                </tr>
                                        <?php
                                            }
                                        } else {
                                            echo "<tr><td colspan='7' class='text-center'><div class='alert alert-warning'>Opp! No Pending shipping found.</div></td></tr>";
                                        }
                                        ?>
                                    </tbody>
                                </table>

                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <?php require './partials/footer.php'; ?>
    </main>
    <?php require './partials/scripts.php'; ?>
</body>

</html>