<!-- Sidebar -->
<ul class="navbar-nav bg-gradient-dark sidebar sidebar-dark accordion" id="accordionSidebar">

    <!-- Sidebar - Brand -->
    <a class="sidebar-brand d-flex align-items-center justify-content-center" href="<?php echo strpos($_SERVER['PHP_SELF'], '/items/') !== false ? '../' : ''; ?>calendar-dashboard.php">
        <div class="sidebar-brand-icon">
            <i class="fas fa-hotel"></i>
        </div>
        <div class="sidebar-brand-text mx-2">
            Inventory Demo
        </div>
    </a>

    <!-- Divider -->
    <hr class="sidebar-divider my-0">

    <?php if(isset($_SESSION['uid'])): ?>
    
    <?php 
    // Detect if we're in a subfolder
    $inItemsFolder = strpos($_SERVER['PHP_SELF'], '/items/') !== false;
    $inPOSFolder = strpos($_SERVER['PHP_SELF'], '/pos/') !== false;
    $inSalesFolder = strpos($_SERVER['PHP_SELF'], '/Sales/') !== false;
    $inPurchaseFolder = strpos($_SERVER['PHP_SELF'], '/Purchase/') !== false;
    $inSalesReportsFolder = strpos($_SERVER['PHP_SELF'], '/SalesReports/') !== false;
    $inStoreSettingsFolder = strpos($_SERVER['PHP_SELF'], '/StoreSettings/') !== false;
    $inSubfolder = $inItemsFolder || $inPOSFolder || $inSalesFolder || $inPurchaseFolder || $inSalesReportsFolder || $inStoreSettingsFolder;
    $prefix = $inSubfolder ? '../' : '';
    $itemsPrefix = $inItemsFolder ? '' : ($inSubfolder ? '../items/' : 'items/');
    ?>
    
    <!-- Dashboard -->
    <li class="nav-item">
        <a class="nav-link" href="<?php echo $prefix; ?>dashboard.php">
            <i class="fas fa-fw fa-tachometer-alt"></i>
            <span>Dashboard</span>
        </a>
    </li>

    <!-- User Management (Admin Only) -->
    <?php if($_SESSION['usertype'] == 'admin'): ?>
    <li class="nav-item">
        <a class="nav-link" href="<?php echo $prefix; ?>user-management.php">
            <i class="fas fa-fw fa-users"></i>
            <span>User Management</span>
        </a>
    </li>
    
    <!-- Role Management (Admin Only) -->
    <li class="nav-item">
        <a class="nav-link" href="<?php echo $prefix; ?>role-management.php">
            <i class="fas fa-fw fa-shield-alt"></i>
            <span>Role Management</span>
        </a>
    </li>
    <?php endif; ?>

    <!-- Divider -->
    <hr class="sidebar-divider">

    <!-- Inventory Section -->
    <div class="sidebar-heading" style="font-size: 0.75rem; font-weight: 800; color: #858796;">
        <i class="fas fa-boxes mr-1"></i> INVENTORY
    </div>
 
    <!-- Item Management -->
    <li class="nav-item">
        <a class="nav-link collapsed" href="#" data-toggle="collapse" data-target="#collapseItems"
            aria-expanded="true" aria-controls="collapseItems">
            <i class="fas fa-fw fa-box"></i>
            <span>Items</span>
        </a>
        <div id="collapseItems" class="collapse" aria-labelledby="headingItems" data-parent="#accordionSidebar">
            <div class="bg-white py-2 collapse-inner rounded">
            
                <a class="collapse-item" href="<?php echo $itemsPrefix; ?>add-item.php">
                    <i class="fas fa-plus mr-1"></i> Add New Item
                </a>
            </div>
        </div>
    </li>

    <!-- Masters Management -->
    <li class="nav-item">
    <a class="nav-link" href="<?php echo $itemsPrefix; ?>masters-management.php">
            <i class="fas fa-fw fa-database"></i>
            <span>Masters Management</span>
        </a>
    </li>

    <!-- Customer Management -->
    <li class="nav-item">
        <a class="nav-link" href="<?php echo $prefix; ?>customer-management.php">
            <i class="fas fa-fw fa-users"></i>
            <span>Customer Management</span>
        </a>
    </li>

    <!-- Sales Management -->
    <li class="nav-item">
        <a class="nav-link collapsed" href="#" data-toggle="collapse" data-target="#collapseSales"
            aria-expanded="true" aria-controls="collapseSales">
            <i class="fas fa-fw fa-file-invoice-dollar" style="color: #f6c23e;"></i>
            <span>Sales</span>
        </a>
        <div id="collapseSales" class="collapse" aria-labelledby="headingSales" data-parent="#accordionSidebar">
            <div class="bg-white py-2 collapse-inner rounded">
                <a class="collapse-item" href="<?php echo $inSubfolder ? '../Sales/' : 'Sales/'; ?>index.php">
                    <i class="fas fa-plus mr-1" style="color: #f6c23e;"></i> New Sale
                </a>
                
            </div>
        </div>
    </li>

    <!-- Purchase Management -->
    <li class="nav-item">
        <a class="nav-link collapsed" href="#" data-toggle="collapse" data-target="#collapsePurchase"
            aria-expanded="true" aria-controls="collapsePurchase">
            <i class="fas fa-fw fa-shopping-cart" style="color: #4e73df;"></i>
            <span>Purchase</span>
        </a>
        <div id="collapsePurchase" class="collapse" aria-labelledby="headingPurchase" data-parent="#accordionSidebar">
            <div class="bg-white py-2 collapse-inner rounded">
                <a class="collapse-item" href="<?php echo $inSubfolder ? '../Purchase/' : 'Purchase/'; ?>index.php">
                    <i class="fas fa-plus mr-1" style="color: #4e73df;"></i> New Purchase
                </a>
                <a class="collapse-item" href="<?php echo $inSubfolder ? '../Purchase/' : 'Purchase/'; ?>products.php">
                    <i class="fas fa-box-open mr-1"></i> Products (Items)
                </a>
                <a class="collapse-item" href="<?php echo $inSubfolder ? '../Purchase/' : 'Purchase/'; ?>supplier-ledger-report.php">
                    <i class="fas fa-file-invoice mr-1"></i> Supplier Ledger
                </a>
            </div>
        </div>
    </li>

    <!-- Divider -->
    <hr class="sidebar-divider">

    <!-- Reports Section -->
    <div class="sidebar-heading" style="font-size: 0.75rem; font-weight: 800; color: #858796;">
        <i class="fas fa-chart-line mr-1"></i> REPORTS
    </div>

    <!-- Sales Reports -->
    <li class="nav-item">
        <a class="nav-link collapsed" href="#" data-toggle="collapse" data-target="#collapseSalesReports"
            aria-expanded="true" aria-controls="collapseSalesReports">
            <i class="fas fa-fw fa-file-invoice" style="color: #1cc88a;"></i>
            <span>Sales Reports</span>
        </a>
        <div id="collapseSalesReports" class="collapse" aria-labelledby="headingSalesReports" data-parent="#accordionSidebar">
            <div class="bg-white py-2 collapse-inner rounded">
                <a class="collapse-item" href="<?php echo $inSubfolder ? '../SalesReports/' : 'SalesReports/'; ?>index.php">
                    <i class="fas fa-tachometer-alt mr-1" style="color: #1cc88a;"></i> Reports Dashboard
                </a>
                <div class="dropdown-divider"></div>
                <a class="collapse-item" href="<?php echo $inSubfolder ? '../SalesReports/' : 'SalesReports/'; ?>gst-sales-report.php">
                    <i class="fas fa-receipt mr-1"></i> GST Sales Report
                </a>
                <a class="collapse-item" href="<?php echo $inSubfolder ? '../SalesReports/' : 'SalesReports/'; ?>bill-wise-sales-report.php">
                    <i class="fas fa-file-invoice-dollar mr-1"></i> Bill-wise Report
                </a>
                <a class="collapse-item" href="<?php echo $inSubfolder ? '../SalesReports/' : 'SalesReports/'; ?>item-wise-sales-report.php">
                    <i class="fas fa-box-open mr-1"></i> Item-wise Report
                </a>
                <a class="collapse-item" href="<?php echo $inSubfolder ? '../SalesReports/' : 'SalesReports/'; ?>category-wise-sales-report.php">
                    <i class="fas fa-layer-group mr-1"></i> Category-wise Report
                </a>
                <a class="collapse-item" href="<?php echo $inSubfolder ? '../SalesReports/' : 'SalesReports/'; ?>customer-accounts-report.php">
                    <i class="fas fa-users mr-1"></i> Customer Accounts
                </a>
                <a class="collapse-item" href="<?php echo $inSubfolder ? '../SalesReports/' : 'SalesReports/'; ?>location-wise-sales-report.php">
                    <i class="fas fa-map-marker-alt mr-1"></i> Location-wise Report
                </a>
                <div class="dropdown-divider"></div>
                <a class="collapse-item" href="<?php echo $inSubfolder ? '../SalesReports/' : 'SalesReports/'; ?>item-stock-report.php">
                    <i class="fas fa-warehouse mr-1"></i> Item Stock Report
                </a>
            </div>
        </div>
    </li>

    <!-- Divider -->
    <hr class="sidebar-divider">

    <!-- System Section -->
    <div class="sidebar-heading" style="font-size: 0.75rem; font-weight: 800; color: #858796;">
        <i class="fas fa-cog mr-1"></i> SYSTEM
    </div>

    <!-- Store Settings -->
    <li class="nav-item">
        <a class="nav-link" href="<?php echo $inSubfolder ? '../StoreSettings/' : 'StoreSettings/'; ?>index.php">
            <i class="fas fa-fw fa-store" style="color: #f6c23e;"></i>
            <span>Store Settings</span>
        </a>
    </li>

    <li class="nav-item">
        <a class="nav-link" href="<?php echo $prefix; ?>logout.php">
            <i class="fas fa-fw fa-sign-out-alt text-danger"></i>
            <span>Logout</span>
        </a>
    </li>

    <?php else: ?>
    
    <!-- Authentication Section -->
    <div class="sidebar-heading" style="font-size: 0.75rem; font-weight: 800; color: #858796;">
        <i class="fas fa-lock mr-1"></i> AUTHENTICATION
    </div>

    <!-- Login Access -->
    <li class="nav-item">
        <a class="nav-link" href="<?php echo $prefix; ?>login.php">
            <i class="fas fa-fw fa-sign-in-alt"></i>
            <span>User Login</span>
        </a>
    </li>

    <?php endif; ?>

    <!-- Sidebar Toggler (Sidebar) -->
    <div class="text-center d-none d-md-inline">
        <button class="rounded-circle border-0" id="sidebarToggle"></button>
    </div>

    <!-- Sidebar Toggler (Sidebar) -->
    <div class="text-center d-none d-md-inline">
        <button class="rounded-circle border-0" id="sidebarToggle"></button>
    </div>

</ul>

<!-- End of Sidebar -->