document.querySelectorAll('.no_follow').forEach(link => {
    link.addEventListener('click', (event) => {
        event.preventDefault();
    });
});

let responseData = null;

document.addEventListener('DOMContentLoaded', async function() {
    const loadingElement = document.getElementById('loading');
    const errorMessageElement = document.getElementById('error-message');
    const contentElement = document.getElementById('content');

    if (loadingElement) {
        loadingElement.style.display = 'block';
        loadingElement.innerHTML = 'در حال بارگزاری...';
    }

    try {
        const input_data = JSON.parse(window.Kodular.getWebViewString());
        const base_url = input_data.base_url;
        const access_token = input_data.access_token;
        const page_name = input_data.page_name;
        const other_value = input_data.other_value || ''; 

        const response = await fetch(`${base_url}?page_name=${page_name}${other_value}`, {
            method: 'GET',
            headers: {
                'Authorization': `Bearer ${access_token}`,
                'Content-Type': 'application/json',
            }
        });

        if (response.ok) {
            responseData = await response.json();
            handleResponseData();
            if (loadingElement) loadingElement.style.display = 'none';
            if (errorMessageElement) errorMessageElement.style.display = 'none';
            if (contentElement) contentElement.style.display = 'block';
        } else {
            if (errorMessageElement) {
                errorMessageElement.innerHTML = `خطایی رخ داده است <br>${response.status}<br><br><button id="back_to_start_page" class="btn btn-food view-cart-button">بازگشت به صفحه ورود<i class="ri-arrow-left-line"></i></button>`;
                errorMessageElement.style.display = 'block';
            }
            throw new Error('Network response was not ok');
        }

    } catch (error) {
        if (loadingElement) loadingElement.style.display = 'none';
        if (errorMessageElement) errorMessageElement.style.display = 'block';
        console.error(error);
    }

    // مدیریت رویداد کلیک دکمه بازگشت به صفحه ورود
    const backToStartPageButton = document.getElementById('back_to_start_page');
    if (backToStartPageButton) {
        backToStartPageButton.addEventListener('click', function() {
            window.Kodular.setWebViewString('back_to_start_page');
        });
    }
});

function handleResponseData() {
    if (responseData) {
        window.user_lat = responseData.user_location.lat;
        window.user_lng = responseData.user_location.lng;

    } else {
        console.log('No data available');
    }
}


function delay(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
}
const backToHome = document.getElementById('back_to_home');
if (backToHome) {
    backToHome.addEventListener('click', async (event) => {
        event.preventDefault();
        window.Kodular.setWebViewString('home');
        await delay(1);
        const url = backToHome.getAttribute('href');
        if (url) {
            window.location.href = url;
        } else {
            console.error('No href found for back_to_home');
        }
    });
}