<?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 Request</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>Name</th>
                                            <th>Number</th>
                                            <th>Email</th>
                                            <th>Details</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 ship 
                                        WHERE demail LIKE ? 
                                        ORDER BY demail ASC 
                                        LIMIT ?, ?";

                                        $stmt = $conn->prepare($query);

                                        $search_term = "%{$search}%";
                                        $stmt->bind_param("sii", $search_term, $offset, $limit);

                                        $stmt->execute();
                                        $result = $stmt->get_result();

                                        $countQuery = "SELECT COUNT(*) as total FROM ship WHERE demail 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['dname'] ?></td>
                                                    <td><?= $row['dphone'] ?></td>
                                                    <td><?= $row['demail'] ?></td>
                                                    <td>
                                                        <a href="detail/<?= ($row['request_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 request 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-request?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-request?page=<?= $i ?>&search=<?= $search ?>"><?= $i ?></a>
                                    </li>
                                <?php endfor; ?>

                                <?php if ($page < $totalPages) : ?>
                                    <li class="page-item">
                                        <a class="page-link" href="manage-request?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>