<header class="sticky-top main-navbar">
    <nav class="navbar navbar-expand-lg navbar-dark">
        <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" 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="#logoutModal" data-bs-toggle="modal">
                            Exit LeadX <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 text-white active" aria-current="page" href="index.php"><i
                                    class="fa fa-home"></i> Home </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link text-white" href="new_entry.php"><i class="fa fa-user-plus"></i> New
                                Entry</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link text-white" href="all-Data.php"><i class="fa fa-users"></i> All
                                Data</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link text-white" href="global-Data.php"><i class="fa fa-globe"></i> Global
                                Data</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link text-white" href="duplicate.php"><i class="fa fa-clone"></i>
                                Duplicate
                                Data</a>
                        </li>
                        <li class="nav-item dropdown">
                            <a class="nav-link dropdown-toggle text-white" href="#" id="userList" role="button"
                                aria-expanded="false">
                                Work Status
                            </a>
                            <ul class="dropdown-menu" aria-labelledby="userList">
                                <?php
                                        $tbsql = "SELECT * FROM status WHERE s_by = '$uadmin' ORDER BY s_pos ASC";
                                        $tbrun = mysqli_query($con, $tbsql);
                                        while ( $tbrows = mysqli_fetch_assoc($tbrun) ) {
                                    ?>
                                <li><a class="dropdown-item"
                                        href="status_view.php?<?php echo 'st='.$tbrows['s_id']; ?>"><?php echo $tbrows['s_name']; ?></a>
                                </li>
                                <?php }?>
                            </ul>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link text-white" href="#logoutModal" data-bs-toggle="modal"> LeadX <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">
            <a href="new_entry.php" class="sidebar-link">
                <i class="fa fa-user-plus"></i>
                <span class="sidebar-text">New Entry</span>
            </a>
        </li>
        <li class="sidebar-item">
            <a href="all-Data.php" class="sidebar-link">
                <i class="fa fa-users"></i>
                <span class="sidebar-text">All Candidates</span>
            </a>
        </li>
        <li class="sidebar-item">
            <a href="global-Data.php" class="sidebar-link">
                <i class="fa fa-globe"></i>
                <span class="sidebar-text">Global Candidate</span>
            </a>
        </li>
        <li class="sidebar-item">
            <a href="duplicate.php" class="sidebar-link">
                <i class="fa fa-clone"></i>
                <span class="sidebar-text">Duplicate Candidate</span>
            </a>
        </li>

        <li class="sidebar-item sidebar-dropdown">
            <a href="#" class="sidebar-link">
                <i class="fa fa-list-alt" aria-hidden="true"></i>
                <span class="sidebar-text">Work Status</span>
                <i class="fa fa-chevron-down dropdown-icon ms-auto"></i>
            </a>
            <ul class="sidebar-submenu">
                <?php
                    $sbsql = "SELECT * FROM status WHERE s_by = '$uadmin' ORDER BY s_pos ASC";
                    $sbrun = mysqli_query($con, $sbsql);
                    while ( $sbrows = mysqli_fetch_assoc($sbrun) ) {
                ?>
                <li><a href="status_view.php?<?php echo 'st='.$sbrows['s_id']; ?>"><?php echo $sbrows['s_name']; ?></a>
                </li>
                <?php }?>
            </ul>
        </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 LeadX?</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 leave LeadX.
            </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>