
document.addEventListener('DOMContentLoaded', function () {
    const sidebar = document.getElementById('adminSidebar');
    const openButton = document.getElementById('adminMobileMenuBtn');
    const closeButton = document.getElementById('adminSidebarClose');
    const overlay = document.getElementById('adminSidebarOverlay');

    function openAdminSidebar() {
        document.body.classList.add('admin-sidebar-open');
    }

    function closeAdminSidebar() {
        document.body.classList.remove('admin-sidebar-open');
    }

    if (openButton) {
        openButton.addEventListener('click', openAdminSidebar);
    }

    if (closeButton) {
        closeButton.addEventListener('click', closeAdminSidebar);
    }

    if (overlay) {
        overlay.addEventListener('click', closeAdminSidebar);
    }

    document.addEventListener('keydown', function (event) {
        if (event.key === 'Escape') {
            closeAdminSidebar();
        }
    });
});


document.addEventListener('DOMContentLoaded', function () {
    const openButton = document.getElementById('adminMobileMenuBtn');
    const closeButton = document.getElementById('adminSidebarClose');
    const overlay = document.getElementById('adminSidebarOverlay');
    const sidebarLinks = document.querySelectorAll('.admin-menu-link');

    function openAdminSidebar() {
        document.body.classList.add('admin-sidebar-open');
    }

    function closeAdminSidebar() {
        document.body.classList.remove('admin-sidebar-open');
    }

    if (openButton) {
        openButton.addEventListener('click', openAdminSidebar);
    }

    if (closeButton) {
        closeButton.addEventListener('click', closeAdminSidebar);
    }

    if (overlay) {
        overlay.addEventListener('click', closeAdminSidebar);
    }

    sidebarLinks.forEach(function (link) {
        link.addEventListener('click', function () {
            if (window.innerWidth <= 980) {
                closeAdminSidebar();
            }
        });
    });

    document.addEventListener('keydown', function (event) {
        if (event.key === 'Escape') {
            closeAdminSidebar();
        }
    });

    const tables = document.querySelectorAll('.admin-table');

    tables.forEach(function (table) {
        if (!table.parentElement.classList.contains('admin-table-wrap')) {
            const wrapper = document.createElement('div');
            wrapper.className = 'admin-table-wrap';
            table.parentNode.insertBefore(wrapper, table);
            wrapper.appendChild(table);
        }
    });
});

document.addEventListener('DOMContentLoaded', function () {
    const openButton = document.getElementById('adminMobileMenuBtn');
    const closeButton = document.getElementById('adminSidebarClose');
    const overlay = document.getElementById('adminSidebarOverlay');
    const sidebarLinks = document.querySelectorAll('.admin-menu-link');

    function openAdminSidebar() {
        document.body.classList.add('admin-sidebar-open');
    }

    function closeAdminSidebar() {
        document.body.classList.remove('admin-sidebar-open');
    }

    if (openButton) {
        openButton.addEventListener('click', openAdminSidebar);
    }

    if (closeButton) {
        closeButton.addEventListener('click', closeAdminSidebar);
    }

    if (overlay) {
        overlay.addEventListener('click', closeAdminSidebar);
    }

    sidebarLinks.forEach(function (link) {
        link.addEventListener('click', function () {
            if (window.innerWidth <= 980) {
                closeAdminSidebar();
            }
        });
    });

    document.addEventListener('keydown', function (event) {
        if (event.key === 'Escape') {
            closeAdminSidebar();
        }
    });

    document.querySelectorAll('.admin-table').forEach(function (table) {
        if (!table.parentElement.classList.contains('admin-table-wrap')) {
            const wrapper = document.createElement('div');
            wrapper.className = 'admin-table-wrap';

            table.parentNode.insertBefore(wrapper, table);
            wrapper.appendChild(table);
        }
    });

    document.querySelectorAll('a[href*="delete="], a[href*="delete_variation="]').forEach(function (link) {
        if (link.hasAttribute('onclick')) {
            return;
        }

        link.addEventListener('click', function (event) {
            const confirmed = confirm('Are you sure you want to delete this item?');

            if (!confirmed) {
                event.preventDefault();
            }
        });
    });

    document.querySelectorAll('input[type="file"]').forEach(function (input) {
        input.addEventListener('change', function () {
            const existingNote = input.parentElement.querySelector('.admin-file-note');

            if (existingNote) {
                existingNote.remove();
            }

            if (!input.files || input.files.length === 0) {
                return;
            }

            const note = document.createElement('small');
            note.className = 'admin-file-note';
            note.textContent = input.files.length + ' file' + (input.files.length === 1 ? '' : 's') + ' selected';

            input.parentElement.appendChild(note);
        });
    });
});