<header class="sticky-top main-navbar">
    <nav class="navbar navbar-expand-lg navbar-light">
        <div class="container-fluid">
            <!-- Sidebar Toggle Button in Header for Small Screens -->
            <button class="btn btn-primary sidebar-toggle d-none d-lg-none" aria-label="Toggle sidebar">
                <i class="fa fa-chevron-left"></i>
            </button>
            <button class="navbar-toggler" type="button" data-bs-toggle="offcanvas" data-bs-target="#offcanvasNavbar"
                aria-controls="offcanvasNavbar" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>
            <a class="navbar-brand" href="index.php">
                <!--<img src="./assets/image/logo.png" alt="" class="img-fluid"
                    style="border-radius: 4px;border: 3px solid #40599b;width: 160px;background: #40599b;">-->
                <span
                    style="display: inline-block;border-radius: 4px;border: 3px solid #40599b;background: #40599b;color: white;padding: 1px 20px;font-size: 24px;font-weight: bold;">PIMS</span>
            </a>
            <!-- Sidebar Toggle Button in Header for Large Screens -->
            <button class="btn btn-primary sidebar-toggle d-none d-lg-block" id="sidebarToggle"
                aria-label="Toggle sidebar">
                <i class="fa fa-chevron-left"></i>
            </button>
            <div class="collapse navbar-collapse d-none d-lg-block" id="navbarNav">
                <ul class="navbar-nav ms-auto mb-2 mb-lg-0">
                    <li class="nav-item">
                        <a class="nav-link active" style="" aria-current="page" href="index.php">
                            <i class="fa fa-home"></i> Home
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="btn btn-sm btn-primary text-white mt-2" href="./leadx/" data-bs-toggle="modal">
                            LeadX <i class="fa fa-sign-in"></i></a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="#logoutModal" data-bs-toggle="modal"> Logout
                            <i class="fa fa-sign-out"></i>
                        </a>
                    </li>
                </ul>
            </div>
            <!-- Offcanvas Navbar (for small screens) -->
            <div class="offcanvas offcanvas-start d-lg-none" tabindex="-1" id="offcanvasNavbar"
                aria-labelledby="offcanvasNavbarLabel">
                <div class="offcanvas-body">
                    <button type="button" class="btn-close btn-nav-close" data-bs-dismiss="offcanvas"
                        aria-label="Close"></button>
                    <ul class="navbar-nav ms-auto mb-2 mb-lg-0">
                        <li class="nav-item">
                            <a class="nav-link active" aria-current="page" href="index.php">Home</a>
                        </li>
                        <li class="nav-item dropdown">
                            <a class="nav-link dropdown-toggle" href="#" id="userList" role="button"
                                aria-expanded="false">
                                User List
                            </a>
                            <ul class="dropdown-menu" aria-labelledby="userList">
                                <li><a class="dropdown-item" href="view-client.php">Student</a></li>
                                <li><a class="dropdown-item" href="view-subFranchiee.php">Sub Associate</a>
                                </li>
                            </ul>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="service.php?show_list">Courses</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="view-productList.php">Work List</a>
                        </li>
                        <li class="nav-item dropdown">
                            <a class="nav-link dropdown-toggle" href="#" id="userList" role="button"
                                aria-expanded="false">
                                Payout
                            </a>
                            <ul class="dropdown-menu" aria-labelledby="userList">
                                <li><a class="dropdown-item" href="payout-history.php">Payout History</a></li>
                                <li><a class="dropdown-item" href="payout.php?show_list">RE Payout</a>
                                </li>
                            </ul>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="notice.php">Notice</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="news.php?show_list">News</a>
                        </li>
                        <!--<li class="nav-item">
                                <a class="nav-link" href="documents.php?show_list">Documents</a>
                            </li>-->
                        <li class="nav-item">
                            <a class="nav-link" href="profile.php">Profile</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="#logoutModal" data-bs-toggle="modal"> Logout <i
                                    class="fa fa-sign-out"></i></a>
                        </li>
                    </ul>
                </div>
            </div>
            <!-- Placeholder for User Profile Icon in Mobile View -->
            <a class="navbar-brand d-block d-md-none" href="profile.php" style="width: 0;">
                <i class="fa fa-user text-center"
                    style="background: #fff;padding: 5px;border: 2px solid #40599b;border-radius: 50%;color: #40599b;font-size: 16px;width: 30px;"></i>
            </a>
        </div>
    </nav>
</header>

<!-- Sidebar (Desktop View) -->
<div class="sidebar d-none d-lg-block">
    <ul class="sidebar-menu mb-5">
        <li class="sidebar-item">
            <a href="index.php" class="sidebar-link">
                <i class="fa fa-home"></i>
                <span class="sidebar-text">Home</span>
            </a>
        </li>
        <li class="sidebar-item sidebar-dropdown">
            <a href="#" class="sidebar-link">
                <i class="fa fa-users"></i>
                <span class="sidebar-text">User List</span>
                <i class="fa fa-chevron-down dropdown-icon ms-auto"></i>
            </a>
            <ul class="sidebar-submenu">
                <li><a href="view-client.php">Students</a></li>
                <li><a href="view-subFranchiee.php">Sub Associate</a></li>
            </ul>
        </li>
        <li class="sidebar-item">
            <a href="service.php?show_list" class="sidebar-link">
                <i class="fa fa-chalkboard"></i>
                <span class="sidebar-text">Courses</span>
            </a>
        </li>
        <li class="sidebar-item">
            <a href="view-productList.php" class="sidebar-link">
                <i class="fab fa-product-hunt"></i>
                <span class="sidebar-text">Work List</span>
            </a>
        </li>
        <li class="sidebar-item sidebar-dropdown">
            <a href="#" class="sidebar-link">
                <i class="fa fa-money-bill"></i>
                <span class="sidebar-text">Payout</span>
                <i class="fa fa-chevron-down dropdown-icon ms-auto"></i>
            </a>
            <ul class="sidebar-submenu">
                <li><a href="payout-history.php">Payout History</a></li>
                <li><a href="payout.php?show_list">Sub Associate Payout</a></li>
            </ul>
        </li>
        <li class="sidebar-item">
            <a href="notice.php" class="sidebar-link">
                <i class="fa fa-bell"></i>
                <span class="sidebar-text">Notice</span>
            </a>
        </li>
        <li class="sidebar-item">
            <a href="news.php?show_list" class="sidebar-link">
                <i class="fa fa-newspaper"></i>
                <span class="sidebar-text">News</span>
            </a>
        </li>
        <li class="sidebar-item">
            <a href="profile.php" class="sidebar-link">
                <i class="fa fa-user-circle"></i>
                <span class="sidebar-text">Profile</span>
            </a>
        </li>
    </ul>
</div>

<!-- Logout Modal -->
<div class="modal fade" id="logoutModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel">Ready to Leave?</h5>
                <button class="btn-close" type="button" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                Select "Logout" below if you are ready to end your current session.
            </div>
            <div class="modal-footer">
                <button class="btn btn-secondary" type="button" data-bs-dismiss="modal">Cancel</button>
                <a class="btn btn-primary" href="logout.php">Logout</a>
            </div>
        </div>
    </div>
</div>



<script>
document.addEventListener("DOMContentLoaded", function() {
    const sidebar = document.querySelector(".sidebar");
    const toggleBtn = document.getElementById("sidebarToggle");
    const innerBody = document.getElementById("inner-body"); // Use correct and consistent ID

    function updateMarginForScreenSize() {
        if (window.innerWidth >= 768) {
            // Desktop: set default margin if not expanded
            if (sidebar.classList.contains("expanded")) {
                innerBody.style.marginLeft = "200px";
            } else {
                innerBody.style.marginLeft = "60px";
            }
        } else {
            // Mobile: always 0
            innerBody.style.marginLeft = "0";
        }
    }

    // Run on load
    updateMarginForScreenSize();

    // Run on resize
    window.addEventListener('resize', updateMarginForScreenSize);

    // Toggle button click
    toggleBtn.addEventListener("click", () => {
        sidebar.classList.toggle("expanded");
        toggleBtn.classList.toggle("rotated");

        updateMarginForScreenSize(); // ✅ recalculate after toggle

        // Close dropdowns when collapsed
        if (!sidebar.classList.contains("expanded")) {
            closeAllDropdowns();
        }
    });

    // Sidebar hover for desktop only
    sidebar.addEventListener("mouseenter", () => {
        if (window.innerWidth >= 768 && !sidebar.classList.contains("expanded")) {
            innerBody.style.marginLeft = "200px";
        }
    });

    sidebar.addEventListener("mouseleave", () => {
        if (window.innerWidth >= 768 && !sidebar.classList.contains("expanded")) {
            innerBody.style.marginLeft = "60px";
            closeAllDropdowns();
        }
    });

    // Accordion-style dropdowns
    document.querySelectorAll(".sidebar-dropdown").forEach(drop => {
        const link = drop.querySelector(".sidebar-link");
        link.addEventListener("click", function(e) {
            e.preventDefault();

            // Accordion: close all others
            document.querySelectorAll(".sidebar-dropdown").forEach(otherDrop => {
                if (otherDrop !== drop) {
                    otherDrop.classList.remove("open");
                }
            });

            // Toggle this one
            drop.classList.toggle("open");
        });
    });

    // Helper to close all dropdowns
    function closeAllDropdowns() {
        document.querySelectorAll(".sidebar-dropdown").forEach(drop => {
            drop.classList.remove("open");
        });
    }
});
</script>