<?php
require './partials/head.php';
require '../config/message/error_success.php';
?>

<body>
    <?php require './partials/sidebar.php'; ?>
    <main class="dashboard-main">
        <?php require './partials/header.php'; ?>
        <!-- </?php require './successerror.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">Manage Shipping</h6>
                <form class="navbar-search d-flex" method="GET" action="">
                    <input type="text" name="search" placeholder="Search by trackNo, name, email, package name" class="form-control" value="<?= isset($_GET['search']) ? $_GET['search'] : '' ?>" style="max-width: 250px;">
                    <button type="submit" class="btn btn-primary-100 ms-2 text-primary-600 radius-8 px-14 py-6 text-sm">
                        <iconify-icon icon="ion:search-outline" class="icon"></iconify-icon> Search
                    </button>
                </form>

                <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
                                        $limit = 10;
                                        $page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
                                        $offset = ($page - 1) * $limit;

                                        $search = isset($_GET['search']) ? $_GET['search'] : '';
                                        $query = "SELECT * FROM packages 
                                        WHERE trackNo LIKE ? 
                                        OR user_email LIKE ? 
                                        OR user_name LIKE ? 
                                        OR package_name LIKE ? 
                                        ORDER BY user_name ASC 
                                        LIMIT ?, ?";
                                        $stmt = $conn->prepare($query);
                                        
                                        $search_term = "%{$search}%";
                                        $stmt->bind_param("ssssii", $search_term, $search_term, $search_term, $search_term, $offset, $limit);
                                        
                                        $stmt->execute();
                                        $result = $stmt->get_result();
                              

                                        $countQuery = "SELECT COUNT(*) as total FROM packages WHERE user_name LIKE ?";
                                        $countStmt = $conn->prepare($countQuery);
                                        $countStmt->bind_param("s", $search_term);
                                        $countStmt->execute();
                                        $countResult = $countStmt->get_result();
                                        $totalUsers = $countResult->fetch_assoc()['total'];
                                        $totalPages = ceil($totalUsers / $limit);

                                        if ($result->num_rows > 0) {
                                            while ($row = $result->fetch_assoc()) {
                                        ?>
                                                <tr>
                                                <td><?= date("M j Y", strtotime($row['date'])); ?></td>

                                                    <td><?= $row['trackNo'] ?></td>
                                                    <td><?= $row['user_name'] ?></td>
                                                    <td><?= $row['user_email'] ?></td>
                                                    <!-- <td></?= $row['user_number'] ?></td> -->
                                                    <td>
                                                        <?php
                                                        $buttonClass = '';
                                                        $statusLabel = ucfirst($row['status']);
                                                        switch ($row['status']) {
                                                            case 'completed':
                                                                $buttonClass = 'btn btn-outline-success btn-sm';
                                                                break;
                                                            case 'cancelled':
                                                                $buttonClass = 'btn btn-outline-danger btn-sm';
                                                                break;
                                                            case 'processing':
                                                                $buttonClass = 'btn btn-outline-info btn-sm';
                                                                break;
                                                            case 'pending':
                                                                $buttonClass = 'btn btn-outline-warning btn-sm';
                                                                break;
                                                            default:
                                                                $buttonClass = 'btn btn-outline-info btn-sm';
                                                                break;
                                                        }
                                                        ?>
                                                        <button type="button" class="<?= $buttonClass; ?>"><?= $statusLabel; ?></button>
                                                    </td>
                                                    <td>
                                                        <a href="details/<?= ($row['unique_Id']) ?>" class="btn btn-primary btn-sm">
                                                            <i class="fas fa-eye" style="color: white;"></i> Details
                                                        </a>
                                                    </td>
                                                </tr>
                                        <?php
                                            }
                                        } else {
                                            echo "<tr><td colspan='6' class='text-center'>Opp! No shipping  record found yet!!.</td></tr>";
                                        }
                                        ?>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                    <?php if ($totalPages > 1) : ?>
                        <nav class="d-flex justify-content-end mt-3">
                            <ul class="pagination">
                                <?php if ($page > 1) : ?>
                                    <li class="page-item">
                                        <a class="page-link" href="manage-shipping?page=<?= $page - 1 ?>&search=<?= $search ?>">Previous</a>
                                    </li>
                                <?php endif; ?>

                                <?php for ($i = 1; $i <= $totalPages; $i++) : ?>
                                    <li class="page-item <?= ($i == $page) ? 'active' : '' ?>">
                                        <a class="page-link" href="manage-shipping?page=<?= $i ?>&search=<?= $search ?>"><?= $i ?></a>
                                    </li>
                                <?php endfor; ?>

                                <?php if ($page < $totalPages) : ?>
                                    <li class="page-item">
                                        <a class="page-link" href="manage-shipping?page=<?= $page + 1 ?>&search=<?= $search ?>">Next</a>
                                    </li>
                                <?php endif; ?>
                            </ul>
                        </nav>
                    <?php endif; ?>
                </div>
            </div>
        </div>
        <?php require './partials/footer.php'; ?>
    </main>
    <?php require './partials/scripts.php'; ?>
</body>
</html>