<?php
// Ensure database connection is available
if (!isset($con)) {
    include_once(__DIR__ . '/config.php');
}
?>
<div id="layoutSidenav_nav">
    <nav class="sb-sidenav accordion sb-sidenav-dark" id="sidenavAccordion">
        <div class="sb-sidenav-menu">
            <div class="nav">
                <div class="sb-sidenav-menu-heading">Core</div>
                <a class="nav-link" href="dashboard.php">
                    <div class="sb-nav-link-icon"><i class="fas fa-tachometer-alt"></i></div>
                    Dashboard
                </a>
                <div class="sb-sidenav-menu-heading">Product Management</div>

                <!--Categories --->
                <a class="nav-link collapsed" href="#" data-bs-toggle="collapse" data-bs-target="#collapseLayouts"
                    aria-expanded="false" aria-controls="collapseLayouts">
                    <div class="sb-nav-link-icon"><i class="fas fa-columns"></i></div>
                    Categories
                    <div class="sb-sidenav-collapse-arrow"><i class="fas fa-angle-down"></i></div>
                </a>
                <div class="collapse" id="collapseLayouts" aria-labelledby="headingOne"
                    data-bs-parent="#sidenavAccordion">
                    <nav class="sb-sidenav-menu-nested nav">
                        <a class="nav-link" href="add-category.php">Add</a>
                        <a class="nav-link" href="manage-categories.php">Manage</a>
                    </nav>
                </div>

                <!--- Sub-Categories --->
                <a class="nav-link collapsed" href="#" data-bs-toggle="collapse" data-bs-target="#subcat"
                    aria-expanded="false" aria-controls="subcat">
                    <div class="sb-nav-link-icon"><i class="fas fa-columns"></i></div>
                    Sub-Categories
                    <div class="sb-sidenav-collapse-arrow"><i class="fas fa-angle-down"></i></div>
                </a>
                <div class="collapse" id="subcat" aria-labelledby="headingOne" data-bs-parent="#sidenavAccordion">
                    <nav class="sb-sidenav-menu-nested nav">
                        <a class="nav-link" href="add-subcategories.php">Add</a>
                        <a class="nav-link" href="manage-subcategories.php">Manage</a>
                    </nav>
                </div>

                <!--- Products --->
                <a class="nav-link collapsed" href="#" data-bs-toggle="collapse" data-bs-target="#product"
                    aria-expanded="false" aria-controls="product">
                    <div class="sb-nav-link-icon"><i class="fas fa-columns"></i></div>
                    Products
                    <div class="sb-sidenav-collapse-arrow"><i class="fas fa-angle-down"></i></div>
                </a>
                <div class="collapse" id="product" aria-labelledby="headingOne" data-bs-parent="#sidenavAccordion">
                    <nav class="sb-sidenav-menu-nested nav">
                        <a class="nav-link" href="insert-product.php">Add</a>
                        <a class="nav-link" href="manage-products.php">Manage</a>
                        <a class="nav-link" href="inventory.php">Inventory</a>
                        <a class="nav-link" href="../vendor-store.php?vid=0" target="_blank">Official Store</a>
                    </nav>
                </div>



                <div class="sb-sidenav-menu-heading">Order Management</div>
                <!--- Products --->
                <a class="nav-link collapsed" href="#" data-bs-toggle="collapse" data-bs-target="#orders"
                    aria-expanded="false" aria-controls="orders">
                    <div class="sb-nav-link-icon"><i class="fas fa-shopping-cart"></i></div>
                    Orders
                    <div class="sb-sidenav-collapse-arrow"><i class="fas fa-angle-down"></i></div>
                </a>
                <div class="collapse" id="orders" aria-labelledby="headingOne" data-bs-parent="#sidenavAccordion">
                    <nav class="sb-sidenav-menu-nested nav">
                        <?php $ret = mysqli_query($con, "select count(id) as totalorders,
count(if((orderStatus='' || orderStatus is null),0,null)) as neworders,
count(if(orderStatus='Packed', 0,null)) as packedorders,
count(if(orderStatus='Dispatched',  0,null)) as dispatchedorders,
count(if(orderStatus='In Transit',  0,null)) as intransitorders,
count(if(orderStatus='Out For Delivery', 0,null)) as outfdorders,
count(if(orderStatus='Delivered', 0,null)) as deliveredorders,
count(if(orderStatus='Cancelled', 0,null)) as cancelledorders
from orders;");
                        $results = mysqli_fetch_array($ret);
                        $torders = $results['totalorders'];
                        $norders = $results['neworders'];
                        $porders = $results['packedorders'];
                        $dtorders = $results['dispatchedorders'];
                        $intorders = $results['intransitorders'];
                        $otforders = $results['outfdorders'];
                        $deliveredorders = $results['deliveredorders'];
                        $cancelledorders = $results['cancelledorders'];
                        ?>
                        <a class="nav-link" href="all-orders.php">
                            All Orders <span class="badge bg-light text-dark ms-2">
                                <?php echo $torders; ?>
                            </span>
                        </a>
                        <a class="nav-link" href="new-order.php">
                            New Orders <span class="badge bg-warning text-dark ms-2">
                                <?php echo $norders; ?>
                            </span>
                        </a>
                        <a class="nav-link" href="packed-orders.php">
                            Packed <span class="badge bg-info text-dark ms-2">
                                <?php echo $porders; ?>
                            </span>
                        </a>
                        <a class="nav-link" href="dispatched-orders.php">
                            Dispatched <span class="badge bg-primary text-white ms-2">
                                <?php echo $dtorders; ?>
                            </span>
                        </a>
                        <a class="nav-link" href="intransit-orders.php">
                            In Transit <span class="badge bg-primary text-white ms-2">
                                <?php echo $intorders; ?>
                            </span>
                        </a>
                        <a class="nav-link" href="outfordelivery-orders.php">
                            Out for Delivery <span class="badge bg-primary text-white ms-2">
                                <?php echo $otforders; ?>
                            </span>
                        </a>
                        <a class="nav-link" href="delivered-orders.php">
                            Delivered <span class="badge bg-success text-white ms-2">
                                <?php echo $deliveredorders; ?>
                            </span>
                        </a>
                        <a class="nav-link" href="cancelled-orders.php">
                            Cancelled <span class="badge bg-danger text-white ms-2">
                                <?php echo $cancelledorders; ?>
                            </span>
                        </a>
                    </nav>
                </div>

                <div class="sb-sidenav-menu-heading">Reports</div>

                <!--Categories --->
                <a class="nav-link collapsed" href="#" data-bs-toggle="collapse" data-bs-target="#reports"
                    aria-expanded="false" aria-controls="collapseLayouts">
                    <div class="sb-nav-link-icon"><i class="fas fa-chart-bar"></i></div>
                    Reports
                    <div class="sb-sidenav-collapse-arrow"><i class="fas fa-angle-down"></i></div>
                </a>
                <div class="collapse" id="reports" aria-labelledby="headingOne" data-bs-parent="#sidenavAccordion">
                    <nav class="sb-sidenav-menu-nested nav">
                        <a class="nav-link" href="bwdates-ordersreport.php">B/w Dates Orders</a>
                        <a class="nav-link" href="sales-report.php">Sales Report</a>
                    </nav>
                </div>


                <?php if (has_permission('manage_users')): ?>
                <a class="nav-link" href="registered-users.php">
                    <div class="sb-nav-link-icon"><i class="fas fa-users"></i></div>
                    Registered Users
                </a>
                <?php endif; ?>

                <div class="sb-sidenav-menu-heading">Settings</div>

                <?php if (has_permission('manage_locations')): ?>
                <a class="nav-link" href="manage-locations.php">
                    <div class="sb-nav-link-icon"><i class="fas fa-map-marker-alt"></i></div>
                    Manage Locations
                </a>
                <a class="nav-link" href="manage-pickup-stations.php">
                    <div class="sb-nav-link-icon"><i class="fas fa-truck-loading"></i></div>
                    Manage Pickup Stations
                </a>
                <?php endif; ?>

                <?php if (has_permission('manage_banners')): ?>
                <a class="nav-link" href="manage-banners.php">
                    <div class="sb-nav-link-icon"><i class="fas fa-images"></i></div>
                    Manage Banners
                </a>
                <?php endif; ?>

                <div class="sb-sidenav-menu-heading">System</div>

                <!-- Vendors -->
                <a class="nav-link collapsed" href="#" data-bs-toggle="collapse" data-bs-target="#vendors">
                    <div class="sb-nav-link-icon"><i class="fas fa-store"></i></div>
                    Vendors
                    <div class="sb-sidenav-collapse-arrow"><i class="fas fa-angle-down"></i></div>
                </a>
                <div class="collapse" id="vendors" data-bs-parent="#sidenavAccordion">
                    <nav class="sb-sidenav-menu-nested nav">
                        <a class="nav-link" href="pending-vendors.php">Pending Applications</a>
                        <a class="nav-link" href="pending-products.php">Pending Products</a>
                        <a class="nav-link" href="all-vendors.php">All Vendors</a>
                        <a class="nav-link" href="vendor-earnings.php">Vendor Earnings</a>
                    </nav>
                </div>

                <?php if (has_permission('manage_staff')): ?>
                    <a class="nav-link" href="manage-staff.php">
                        <div class="sb-nav-link-icon"><i class="fas fa-users-cog"></i></div>
                        Staff Management
                    </a>
                <?php endif; ?>

                <?php if (has_permission('manage_settings')): ?>
                    <a class="nav-link" href="manage-permissions.php">
                        <div class="sb-nav-link-icon"><i class="fas fa-user-shield"></i></div>
                        Permissions
                    </a>
                    <a class="nav-link" href="activity-log.php">
                        <div class="sb-nav-link-icon"><i class="fas fa-history"></i></div>
                        Activity Log
                    </a>
                <?php endif; ?>

            </div>
        </div>
        <div class="sb-sidenav-footer">
            <div class="small">Logged in as:</div>
            <strong>
                <?php echo htmlentities($_SESSION['username']); ?>
            </strong>
        </div>
    </nav>
</div>