window.addEventListener('DOMContentLoaded', function () {

    if (getCookie('login') === '') {
        window.location.replace("login.php");
    }

}, true);

function removeAllCookies() {

    document.cookie = 'id=; expires=Thu, 01 Jan 1970 00:00:00 GMT; Max-Age=0;  path=/areye_panel';
    document.cookie = 'name=; expires=Thu, 01 Jan 1970 00:00:00 GMT; Max-Age=0;  path=/areye_panel';
    document.cookie = 'email=; expires=Thu, 01 Jan 1970 00:00:00 GMT; Max-Age=0;  path=/areye_panel';
    document.cookie = 'login=; expires=Thu, 01 Jan 1970 00:00:00 GMT; Max-Age=0;  path=/areye_panel';
    document.cookie = 'securityKey=; expires=Thu, 01 Jan 1970 00:00:00 GMT; Max-Age=0;  path=/areye_panel';
    window.location.replace("login.php");

}

/* logout session endpoint */
function logoutSessionEndpoint(url) {
    return new Promise(function (resolve, reject) {

        const xhttp = new XMLHttpRequest();
        xhttp.onload = function () {

            var status = xhttp.status;
            if (status === 200 || status === 201 || status === 204) {
                resolve({code: status, data: this.responseText});
            } else {
                reject({code: status, data: this.responseText});
            }

        };
        xhttp.open("GET", url, true);
        for (let i = 0; i < listOfHeaders.length; i++) {
            xhttp.setRequestHeader(listOfHeaders[i]['key'], listOfHeaders[i]['value']);
        }

        xhttp.send();

    });
}

if (document.getElementById('btn-logout') != null) {

    document.getElementById('btn-logout')
        .addEventListener('click', function () {

            logoutSessionEndpoint(baseUrl + 'v1/web/restaurant/user/logout')
                .then(response => JSON.parse(response.data))
                .then(data => {
                    removeAllCookies();
                })
                .catch(error => {
                    removeAllCookies();
                });

        });

    document.getElementById('input-greetings').innerText = 'Welcome ' + getCookie('name') + '!';
    document.getElementById('input-profile').innerText = getCookie('name');
    ///document.getElementById('input-role').innerText = getCookie('business');




}

/*if (getCookie('accessTypeId') !== '1'
    && getCookie('accessTypeName') !== 'Admin') {

    document.getElementById('navbar-nav').style.display = 'none';
    document.getElementById('navbar-nav').innerHTML = '';
    document.getElementById('layout-customers').style.setProperty('display', '');
    document.getElementById('layout-orders').style.setProperty('display', '');

    if (getCookie('accessTypeName') === 'Manager'
        && getCookie('accessTypeId') === '5') {

        document.getElementById('layout-home').style.setProperty('display', '');
        document.getElementById('layout-product').style.setProperty('display', '');

    }

}*/



