document.addEventListener('DOMContentLoaded', function() {
    const addressDisplay = document.getElementById('address-display');
    const addressContainer = document.querySelector('.address-container');
    const modal = document.getElementById('address-modal');
    const closeButton = document.querySelector('.close-button');
    const saveAddressButton = document.getElementById('save-address-button');
    const modalAddressInput = document.getElementById('modal-address-input');

    // Load and display saved address
    const savedAddress = localStorage.getItem('deliveryAddress');
    if (savedAddress) {
        addressDisplay.textContent = savedAddress;
    } else {
        addressDisplay.textContent = 'Enter delivery address';
    }

    // Open modal
    if (addressContainer) {
        addressContainer.addEventListener('click', function() {
            if (modal) {
                modal.style.display = 'block';
                modalAddressInput.value = localStorage.getItem('deliveryAddress') || '';
            }
        });
    }

    // Close modal
    if(closeButton) {
        closeButton.addEventListener('click', function() {
            if (modal) modal.style.display = 'none';
        });
    }


    window.addEventListener('click', function(event) {
        if (event.target == modal) {
            modal.style.display = 'none';
        }
    });

    // Save address
    if(saveAddressButton) {
        saveAddressButton.addEventListener('click', function() {
            const newAddress = modalAddressInput.value.trim();
            if (newAddress) {
                localStorage.setItem('deliveryAddress', newAddress);
                addressDisplay.textContent = newAddress;
                modal.style.display = 'none';
            } else {
                alert('Please enter an address.');
            }
        });
    }

    // Search functionality for index.php
    const searchInput = document.getElementById('search-input');
    if (searchInput) {
        const restaurantGrid = document.getElementById('restaurant-grid');
        const restaurantCards = restaurantGrid.querySelectorAll('a.restaurant-card');

        searchInput.addEventListener('keyup', function () {
            const searchTerm = searchInput.value.toLowerCase();

            restaurantCards.forEach(card => {
                const name = card.dataset.name || '';
                const cuisine = card.dataset.cuisine || '';

                if (name.includes(searchTerm) || cuisine.includes(searchTerm)) {
                    card.style.display = 'flex';
                } else {
                    card.style.display = 'none';
                }
            });
        });
    }

    // Search functionality for menu.php
    const menuSearchInput = document.getElementById('menu-search-input');
    if (menuSearchInput) {
        const menuGrid = document.getElementById('menu-grid');
        const menuItems = menuGrid.querySelectorAll('.menu-item-card');

        menuSearchInput.addEventListener('keyup', function () {
            const searchTerm = menuSearchInput.value.toLowerCase();

            menuItems.forEach(card => {
                const name = card.dataset.name || '';
                const description = card.dataset.description || '';

                if (name.includes(searchTerm) || description.includes(searchTerm)) {
                    card.style.display = 'flex';
                } else {
                    card.style.display = 'none';
                }
            });
        });
    }

    // Filter dropdown
    const filterDropdown = document.querySelector('.filter-dropdown');
    if (filterDropdown) {
        const filterButton = filterDropdown.querySelector('.filter-button');
        const filterOptions = filterDropdown.querySelector('.filter-options');

        filterButton.addEventListener('click', () => {
            const isVisible = filterOptions.style.display === 'block';
            filterOptions.style.display = isVisible ? 'none' : 'block';
        });

        window.addEventListener('click', (event) => {
            if (!filterDropdown.contains(event.target)) {
                filterOptions.style.display = 'none';
            }
        });
    }
});