<?php
$current_page = basename($_SERVER['PHP_SELF']);
$role_id = $_SESSION['role_id'];
$is_super_admin = $role_id == 1;
$product_pages = ['products.php','add_product.php','edit_product.php','view_product.php','categories.php','brands.php','imei_stock.php','add_imei.php','edit_imei.php','view_imei_history.php','stock_adjustment.php','low_stock.php'];
$supplier_pages = ['suppliers.php','add_supplier.php','edit_supplier.php','view_supplier.php','purchases.php','add_purchase.php','view_purchase.php','print_purchase.php','supplier_payments.php'];
$repair_pages = ['repair_jobs.php','add_repair_job.php','edit_repair_job.php','technician_panel.php','repair_invoice.php','print_job_card.php','view_repair_job.php','repair_payment.php','print_repair_invoice.php'];
$grn_pages = ['grn_list.php','create_grn.php','view_grn.php','print_grn.php'];
$warranty_pages = ['warranties.php','warranty_claims.php'];
$customer_pages = ['customers.php','customer_view.php'];
$finance_pages = ['payments.php','expenses.php'];
$report_pages = ['sales_report.php','repair_report.php','stock_report.php','profit_loss_report.php','customer_due_report.php','supplier_due_report.php','expense_report.php','imei_report.php'];
$admin_pages = ['users.php','roles.php','permissions.php','profile.php'];
?>
<div class="sidebar">
    <div class="sidebar-header text-center py-3">
        <img src="<?= htmlspecialchars(getShopLogoUrl()) ?>?v=<?= time() ?>" alt="Logo" class="img-fluid" style="max-height:50px;" onerror="this.style.display='none'">
        <div class="fw-bold text-white small mt-1"><?= htmlspecialchars(getSetting('shop_name', 'DigiMobiz')) ?></div>
    </div>
    <ul class="nav flex-column">
        <?php if ($is_super_admin || hasPermission('dashboard_view')): ?>
        <li class="nav-item">
            <a class="nav-link <?= $current_page == 'dashboard.php' ? 'active' : '' ?>" href="<?= BASE_URL ?>admin/dashboard.php">
                <i class="fas fa-tachometer-alt"></i> Dashboard
            </a>
        </li>
        <?php endif; ?>

        <?php if ($is_super_admin || hasPermission('product_management') || hasPermission('category_management') || hasPermission('stock_view')): ?>
        <li class="nav-item">
            <a class="nav-link <?= in_array($current_page, $product_pages) ? 'active' : '' ?>" href="#" data-bs-toggle="collapse" data-bs-target="#productsMenu">
                <i class="fas fa-boxes"></i> Products <i class="fas fa-chevron-down float-end"></i>
            </a>
            <ul class="collapse submenu <?= in_array($current_page, $product_pages) ? 'show' : '' ?>" id="productsMenu">
                <?php if ($is_super_admin || hasPermission('product_management')): ?>
                <li><a class="nav-link" href="<?= BASE_URL ?>products/products.php"><i class="fas fa-circle"></i> All Products</a></li>
                <li><a class="nav-link" href="<?= BASE_URL ?>products/add_product.php"><i class="fas fa-circle"></i> Add Product</a></li>
                <?php endif; ?>
                <?php if ($is_super_admin || hasPermission('category_management')): ?>
                <li><a class="nav-link" href="<?= BASE_URL ?>products/categories.php"><i class="fas fa-circle"></i> Categories</a></li>
                <li><a class="nav-link" href="<?= BASE_URL ?>products/brands.php"><i class="fas fa-circle"></i> Brands</a></li>
                <?php endif; ?>
                <?php if ($is_super_admin || hasPermission('stock_view')): ?>
                <li><a class="nav-link" href="<?= BASE_URL ?>products/imei_stock.php"><i class="fas fa-circle"></i> IMEI Stock</a></li>
                <?php endif; ?>
                <?php if ($is_super_admin || hasPermission('product_management')): ?>
                <li><a class="nav-link" href="<?= BASE_URL ?>products/stock_adjustment.php"><i class="fas fa-circle"></i> Stock Adjustment</a></li>
                <?php endif; ?>
                <?php if ($is_super_admin || hasPermission('stock_view') || hasPermission('product_management')): ?>
                <li><a class="nav-link" href="<?= BASE_URL ?>products/low_stock.php"><i class="fas fa-circle"></i> Low Stock</a></li>
                <?php endif; ?>
            </ul>
        </li>
        <?php endif; ?>

        <?php if ($is_super_admin || hasPermission('supplier_management') || hasPermission('purchase_management')): ?>
        <li class="nav-item">
            <a class="nav-link <?= in_array($current_page, $supplier_pages) ? 'active' : '' ?>" href="#" data-bs-toggle="collapse" data-bs-target="#suppliersMenu">
                <i class="fas fa-truck"></i> Suppliers <i class="fas fa-chevron-down float-end"></i>
            </a>
            <ul class="collapse submenu <?= in_array($current_page, $supplier_pages) ? 'show' : '' ?>" id="suppliersMenu">
                <?php if ($is_super_admin || hasPermission('supplier_management')): ?>
                <li><a class="nav-link" href="<?= BASE_URL ?>suppliers/suppliers.php"><i class="fas fa-circle"></i> Suppliers</a></li>
                <li><a class="nav-link" href="<?= BASE_URL ?>suppliers/supplier_payments.php"><i class="fas fa-circle"></i> Supplier Payments</a></li>
                <?php endif; ?>
                <?php if ($is_super_admin || hasPermission('purchase_management')): ?>
                <li><a class="nav-link" href="<?= BASE_URL ?>suppliers/purchases.php"><i class="fas fa-circle"></i> Purchases</a></li>
                <li><a class="nav-link" href="<?= BASE_URL ?>suppliers/add_purchase.php"><i class="fas fa-circle"></i> Add Purchase</a></li>
                <?php endif; ?>
                <?php if ($is_super_admin || hasPermission('grn_manage')): ?>
                <li><a class="nav-link" href="<?= BASE_URL ?>grn/grn_list.php"><i class="fas fa-circle"></i> GRN List</a></li>
                <li><a class="nav-link" href="<?= BASE_URL ?>grn/create_grn.php"><i class="fas fa-circle"></i> Create GRN</a></li>
                <?php endif; ?>
            </ul>
        </li>
        <?php endif; ?>

        <?php if ($is_super_admin || hasPermission('pos_access')): ?>
        <li class="nav-item">
            <a class="nav-link <?= $current_page == 'pos.php' ? 'active' : '' ?>" href="<?= BASE_URL ?>pos/pos.php">
                <i class="fas fa-shopping-cart"></i> POS / Billing
            </a>
        </li>
        <?php endif; ?>

        <?php if ($is_super_admin || hasPermission('customer_management')): ?>
        <li class="nav-item">
            <a class="nav-link <?= in_array($current_page, $customer_pages) ? 'active' : '' ?>" href="<?= BASE_URL ?>customers/customers.php">
                <i class="fas fa-users"></i> Customers
            </a>
        </li>
        <?php endif; ?>

        <?php if ($is_super_admin || hasPermission('repair_management') || hasPermission('technician_panel')): ?>
        <li class="nav-item">
            <a class="nav-link <?= in_array($current_page, $repair_pages) ? 'active' : '' ?>" href="#" data-bs-toggle="collapse" data-bs-target="#repairsMenu">
                <i class="fas fa-tools"></i> Repairs <i class="fas fa-chevron-down float-end"></i>
            </a>
            <ul class="collapse submenu <?= in_array($current_page, $repair_pages) ? 'show' : '' ?>" id="repairsMenu">
                <?php if ($is_super_admin || hasPermission('repair_management')): ?>
                <li><a class="nav-link" href="<?= BASE_URL ?>repairs/repair_jobs.php"><i class="fas fa-circle"></i> All Jobs</a></li>
                <li><a class="nav-link" href="<?= BASE_URL ?>repairs/add_repair_job.php"><i class="fas fa-circle"></i> New Job Card</a></li>
                <?php endif; ?>
                <?php if ($is_super_admin || hasPermission('technician_panel')): ?>
                <li><a class="nav-link" href="<?= BASE_URL ?>repairs/technician_panel.php"><i class="fas fa-circle"></i> My Jobs</a></li>
                <?php endif; ?>
            </ul>
        </li>
        <?php endif; ?>

        <?php if ($is_super_admin || hasPermission('warranty_management')): ?>
        <li class="nav-item">
            <a class="nav-link <?= in_array($current_page, $warranty_pages) ? 'active' : '' ?>" href="#" data-bs-toggle="collapse" data-bs-target="#warrantyMenu">
                <i class="fas fa-shield-alt"></i> Warranty <i class="fas fa-chevron-down float-end"></i>
            </a>
            <ul class="collapse submenu <?= in_array($current_page, $warranty_pages) ? 'show' : '' ?>" id="warrantyMenu">
                <li><a class="nav-link" href="<?= BASE_URL ?>warranty/warranties.php"><i class="fas fa-circle"></i> Warranties</a></li>
                <li><a class="nav-link" href="<?= BASE_URL ?>warranty/warranty_claims.php"><i class="fas fa-circle"></i> Claims</a></li>
            </ul>
        </li>
        <?php endif; ?>

        <?php if ($is_super_admin || hasPermission('return_management')): ?>
        <li class="nav-item">
            <a class="nav-link <?= in_array($current_page, ['returns.php']) ? 'active' : '' ?>" href="<?= BASE_URL ?>returns/returns.php">
                <i class="fas fa-undo"></i> Returns
            </a>
        </li>
        <?php endif; ?>

        <?php if ($is_super_admin || hasPermission('payment_view') || hasPermission('expense_management')): ?>
        <li class="nav-item">
            <a class="nav-link <?= in_array($current_page, $finance_pages) ? 'active' : '' ?>" href="#" data-bs-toggle="collapse" data-bs-target="#financeMenu">
                <i class="fas fa-money-bill"></i> Finance <i class="fas fa-chevron-down float-end"></i>
            </a>
            <ul class="collapse submenu <?= in_array($current_page, $finance_pages) ? 'show' : '' ?>" id="financeMenu">
                <?php if ($is_super_admin || hasPermission('payment_view')): ?>
                <li><a class="nav-link" href="<?= BASE_URL ?>payments/payments.php"><i class="fas fa-circle"></i> Payments</a></li>
                <?php endif; ?>
                <?php if ($is_super_admin || hasPermission('expense_management')): ?>
                <li><a class="nav-link" href="<?= BASE_URL ?>expenses/expenses.php"><i class="fas fa-circle"></i> Expenses</a></li>
                <?php endif; ?>
            </ul>
        </li>
        <?php endif; ?>

        <?php if ($is_super_admin || hasPermission('report_view')): ?>
        <li class="nav-item">
            <a class="nav-link <?= in_array($current_page, $report_pages) ? 'active' : '' ?>" href="#" data-bs-toggle="collapse" data-bs-target="#reportsMenu">
                <i class="fas fa-chart-bar"></i> Reports <i class="fas fa-chevron-down float-end"></i>
            </a>
            <ul class="collapse submenu <?= in_array($current_page, $report_pages) ? 'show' : '' ?>" id="reportsMenu">
                <li><a class="nav-link" href="<?= BASE_URL ?>reports/sales_report.php"><i class="fas fa-circle"></i> Sales Report</a></li>
                <li><a class="nav-link" href="<?= BASE_URL ?>reports/repair_report.php"><i class="fas fa-circle"></i> Repair Report</a></li>
                <li><a class="nav-link" href="<?= BASE_URL ?>reports/stock_report.php"><i class="fas fa-circle"></i> Stock Report</a></li>
                <li><a class="nav-link" href="<?= BASE_URL ?>reports/profit_loss_report.php"><i class="fas fa-circle"></i> Profit & Loss</a></li>
                <li><a class="nav-link" href="<?= BASE_URL ?>reports/customer_due_report.php"><i class="fas fa-circle"></i> Customer Dues</a></li>
                <li><a class="nav-link" href="<?= BASE_URL ?>reports/supplier_due_report.php"><i class="fas fa-circle"></i> Supplier Dues</a></li>
                <li><a class="nav-link" href="<?= BASE_URL ?>reports/expense_report.php"><i class="fas fa-circle"></i> Expenses Report</a></li>
                <li><a class="nav-link" href="<?= BASE_URL ?>reports/imei_report.php"><i class="fas fa-circle"></i> IMEI History</a></li>
            </ul>
        </li>
        <?php endif; ?>

        <?php if ($is_super_admin || hasPermission('user_management') || hasPermission('settings_access')): ?>
        <li class="nav-item">
            <a class="nav-link <?= in_array($current_page, $admin_pages) ? 'active' : '' ?>" href="#" data-bs-toggle="collapse" data-bs-target="#adminMenu">
                <i class="fas fa-cog"></i> Administration <i class="fas fa-chevron-down float-end"></i>
            </a>
            <ul class="collapse submenu <?= in_array($current_page, $admin_pages) ? 'show' : '' ?>" id="adminMenu">
                <?php if ($is_super_admin || hasPermission('user_management')): ?>
                <li><a class="nav-link" href="<?= BASE_URL ?>admin/users.php"><i class="fas fa-circle"></i> Users</a></li>
                <li><a class="nav-link" href="<?= BASE_URL ?>admin/roles.php"><i class="fas fa-circle"></i> Roles</a></li>
                <li><a class="nav-link" href="<?= BASE_URL ?>admin/permissions.php"><i class="fas fa-circle"></i> Permissions</a></li>
                <?php endif; ?>
                <?php if ($is_super_admin): ?>
                <li><a class="nav-link" href="<?= BASE_URL ?>settings/demo_data.php"><i class="fas fa-circle"></i> Demo Data</a></li>
                <?php endif; ?>
                <?php if ($is_super_admin || hasPermission('settings_access')): ?>
                <li><a class="nav-link" href="<?= BASE_URL ?>settings/shop_settings.php"><i class="fas fa-circle"></i> Settings</a></li>
                <?php endif; ?>
            </ul>
        </li>
        <?php endif; ?>
    </ul>
</div>
