<?php session_start();
include_once('includes/config.php');
if (strlen($_SESSION["aid"]) == 0) {
    header('location:logout.php');
} else {




    ?>
    <!DOCTYPE html>
    <html lang="en">

    <head>
        <meta charset="utf-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
        <meta name="description" content="" />
        <meta name="author" content="" />
        <title>Shooping Portal | Manage Dispatched Orders</title>
        <link href="https://cdn.jsdelivr.net/npm/simple-datatables@latest/dist/style.css" rel="stylesheet" />
        <link href="css/styles.css" rel="stylesheet" />
        <link href="css/admin-professional.css" rel="stylesheet" />
        <script src="js/all.min.js" crossorigin="anonymous"></script>
    </head>

    <body class="sb-nav-fixed">
        <?php include_once('includes/header.php'); ?>
        <div id="layoutSidenav">
            <?php include_once('includes/sidebar.php'); ?>
            <div id="layoutSidenav_content">
                <main>
                    <div class="container-fluid px-4">
                        <!-- Page Header -->
                        <div class="page-header mt-4">
                            <div>
                                <h1><i class="fas fa-shipping-fast me-2"></i> Manage Dispatched Orders</h1>
                                <p>Orders dispatched and en route</p>
                            </div>
                        </div>

                        <!-- Orders Table -->
                        <div class="table-card">
                            <table class="table table-hover" id="datatablesSimple">
                                <thead>
                                    <tr>
                                        <th>#</th>
                                        <th>Order No.</th>
                                        <th>Order By</th>
                                        <th>Order Amount</th>
                                        <th>Order Date</th>
                                        <th>Order Status</th>
                                        <th>Action</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php $query = mysqli_query($con, "SELECT orders.id,orderNumber,totalAmount,orderStatus,orderDate,user_details.name,user_details.contactno 
                                    FROM `orders` join users on users.id=orders.userId left join user_details on user_details.user_id=users.id where (orderStatus='Dispatched')");
                                    $cnt = 1;
                                    while ($row = mysqli_fetch_array($query)) {
                                        ?>
                                        <tr>
                                            <td><?php echo htmlentities($cnt); ?></td>
                                            <td><strong><?php echo htmlentities($row['orderNumber']); ?></strong></td>
                                            <td><?php echo htmlentities($row['name']); ?></td>
                                            <td><strong>$<?php echo htmlentities($row['totalAmount']); ?></strong></td>
                                            <td><?php echo htmlentities($row['orderDate']); ?></td>
                                            <td>
                                                <span class="status-badge dispatched">Dispatched</span>
                                            </td>
                                            <td>
                                                <a href="order-details.php?orderid=<?php echo $row['id'] ?>"
                                                    class="btn btn-sm btn-outline-primary" title="View Order Details">
                                                    <i class="fas fa-eye me-1"></i> View
                                                </a>
                                            </td>
                                        </tr>
                                        <?php $cnt = $cnt + 1;
                                    } ?>

                                </tbody>
                            </table>
                        </div>
                    </div>
                </main>
                <?php include_once('includes/footer.php'); ?>
                </footer>
            </div>
        </div>
        <script src="js/bootstrap.bundle.min.js" crossorigin="anonymous"></script>
        <script src="js/scripts.js"></script>
        <script src="https://cdn.jsdelivr.net/npm/simple-datatables@latest" crossorigin="anonymous"></script>
        <script src="js/datatables-simple-demo.js"></script>
    </body>

    </html>
<?php } ?>
