// sidebar all js
document.addEventListener("DOMContentLoaded", function () {
    // Sidebar Variables
    const sidebar = document.getElementById("sidebar-wrapper");
    const contentWrapper = document.getElementById("page-content-wrapper");
    const menuToggle = document.getElementById("menu-toggle");
    const sidebarToggle = document.getElementById("sidebarToggle");

    // Overlay for mobile sidebar close
    const overlay = document.createElement("div");
    overlay.classList.add("sidebar-overlay");
    document.body.appendChild(overlay);

    // Initial adjustment for content wrapper based on sidebar state
    function adjustContentWrapper() {
        contentWrapper.style.marginLeft = sidebar.classList.contains("collapsed") ? "80px" : "250px";
    }
    adjustContentWrapper();

    // Toggle sidebar and overlay visibility
    function toggleSidebar() {
        sidebar.classList.toggle("active");
        overlay.classList.toggle("active");
        adjustContentWrapper();
    }

    // Event listener for sidebar toggles
    menuToggle.addEventListener("click", function (event) {
        event.preventDefault();
        toggleSidebar();
    });

    sidebarToggle.addEventListener("click", function () {
        sidebar.classList.toggle("collapsed");
        adjustContentWrapper();
    });

    // Close sidebar and overlay when clicking outside on mobile
    overlay.addEventListener("click", function () {
        sidebar.classList.remove("active");
        overlay.classList.remove("active");
    });

    // Dropdown Menu Functionality
    const dropdowns = document.querySelectorAll('.dropdown-toggle');

    // Function to toggle individual dropdown
    function toggleDropdown(dropdown) {
        const parentLi = dropdown.parentElement;
        const submenu = parentLi.querySelector('ul');

        // Toggle 'menu-open' class and display style
        parentLi.classList.toggle('menu-open');
        submenu.style.display = submenu.style.display === 'block' ? 'none' : 'block';

        // Close other open dropdowns
        dropdowns.forEach(function (otherDropdown) {
            if (otherDropdown !== dropdown) {
                const otherParentLi = otherDropdown.parentElement;
                const otherSubmenu = otherParentLi.querySelector('ul');
                otherParentLi.classList.remove('menu-open');
                otherSubmenu.style.display = 'none';
            }
        });
    }

    // Event listener for dropdown clicks
    dropdowns.forEach(function (dropdown) {
        dropdown.addEventListener('click', function (event) {
            event.preventDefault();
            toggleDropdown(dropdown);
        });
    });

    // Close all dropdowns when clicking outside of the sidebar or toggle button
    // document.addEventListener('click', function (event) {
    //     if (!event.target.closest('#sidebar-wrapper') && !event.target.closest('#menu-toggle')) {
    //         dropdowns.forEach(function (dropdown) {
    //             const parentLi = dropdown.parentElement;
    //             const submenu = parentLi.querySelector('ul');
    //             parentLi.classList.remove('menu-open');
    //             submenu.style.display = 'none';
    //         });
    //     }
    // });

    // Auto-open dropdown if it contains an active link on page load
    document.querySelectorAll('.treeview').forEach(function (treeview) {
        const activeLink = treeview.querySelector('.nav-link.active');
        if (activeLink) {
            treeview.classList.add('menu-open');
            const submenu = treeview.querySelector('ul');
            if (submenu) {
                submenu.style.display = 'block';
            }
        }
    });
});







// Fullscreen js
const fullscreenBtn = document.getElementById('fullscreenBtn');

fullscreenBtn.addEventListener('click', function (e) {
    e.preventDefault();
    if (!document.fullscreenElement) {
        // Request fullscreen
        document.documentElement.requestFullscreen();
        fullscreenBtn.innerHTML = '<i class="fas fa-compress"></i>'; // Change icon to compress
    } else {
        // Exit fullscreen
        if (document.exitFullscreen) {
            document.exitFullscreen();
            fullscreenBtn.innerHTML = '<i class="fas fa-expand"></i>'; // Change icon back to expand
        }
    }
});



/*******image light box********/
document.getElementById("featured_image").addEventListener("change", function (event) {
    var preview = document.getElementById("imagePreview");
    var file = event.target.files[0];
    var reader = new FileReader();

    reader.onload = function (e) {
        preview.src = e.target.result;
        preview.style.display = "block";
    }

    if (file) {
        reader.readAsDataURL(file);
    }
});

// Function to open the lightbox
function openLightbox(src) {
    var lightbox = document.getElementById("lightbox");
    var lightboxImage = document.getElementById("lightboxImage");
    lightbox.style.display = "block";
    lightboxImage.src = src;
}

// Function to close the lightbox
function closeLightbox() {
    document.getElementById("lightbox").style.display = "none";
}






/************ Common Setting Logo Upload ***********/

function previewImage(event) {
    var file = event.target.files[0];
    if (file) {
        var reader = new FileReader();

        reader.onload = function (e) {
            var preview = document.getElementById('logoPreview');
            preview.src = e.target.result;
            preview.style.display = "block";
        }

        reader.readAsDataURL(file);
    }
}









