window.onload = function carga() {
    
    clearQtyPupils();
    
    //Abrir slideMenu
    document.getElementById('slideMenu').addEventListener("click", function(){
        openNav();
    });
    
    //Logout
    document.getElementById('logOut').addEventListener("click", function(){
        gotoPage('login','main',{});
    });
    
    var params = {
        ssid: getSessionId(),  
    };
    
    loadScreen()
    callWS("GET", "schools/entry", params, respx);
    
    function respx(status, respText){
        console.log(JSON.parse(respText));
        var json = JSON.parse(respText);
        let imageDina = document.getElementsByClassName("logoButton")[0];
        if (status == 200){
            
            document.getElementsByClassName('userName')[0].innerHTML = json.contact;
            document.getElementById('schoolName').innerHTML = json.name;
            json.editlocked ? disabledButton() : changeButton();
            
            showLabelPrivate(json.privat);
            
            if(!json.privat){
                document.getElementById('registerPay').style.opacity = 0.5;
                document.getElementById('paymentsLogo').style.opacity = 0.5;
                document.getElementById('registerPay').setAttribute('disabled','');
                document.getElementById('registerPay').style.cursor = "default";
                document.getElementById('paymentsButton').style.backgroundColor = "#DDD";
            }
            
            disabledEstimatedBtn();
        }
        closeLoad();
    }
}

function disabledEstimatedBtn () {
    let params = {
        id: 7
    }
    loadScreen();
    callWS("GET", "settings/entryregister", params, respx);
    
    //Obtenemos los datos del servicio
    function respx(status, respText){
        var json = JSON.parse(respText);
        console.log(json)
        closeLoad();
        if (status == 200){
            if (json.entry.content == 0) {
                document.getElementById('estimatedPupils').style.opacity = 0.5;
                document.getElementById('schoolsLogo').style.opacity = 0.5;
                document.getElementById('estimatedPupils').setAttribute('disabled','');
                document.getElementById('estimatedPupils').style.cursor = "default";
                document.getElementById('estimatedButton').style.backgroundColor = "#DDD";
                
                document.getElementById('registerPay').style.opacity = 0.5;
                document.getElementById('paymentsLogo').style.opacity = 0.5;
                document.getElementById('registerPay').setAttribute('disabled','');
                document.getElementById('registerPay').style.cursor = "default";
                document.getElementById('paymentsButton').style.backgroundColor = "#DDD";
                
                showLabelEstimated(json.entry.content)
            }
        }else{
            console.log(json.msg);
        }
    }
}

function changeButton(){
    var link = document.getElementById('studentLink');
    var buttonRegister = document.getElementById("registerStudent");
    buttonRegister.setAttribute('onclick',`navigationMenu('searchStudent')`);
    link.setAttribute('href',`javascript:navigationMenu('searchStudent')`);
}

function disabledButton(){
    document.getElementById('registerStudent').style.opacity = 0.5;
    document.getElementById('imagePupils').style.opacity = 0.5;
    document.getElementById('registerStudent').setAttribute('disabled','');
    document.getElementById('registerStudent').style.cursor = "default";
    document.getElementById('registerPupils').style.backgroundColor = "#DDD";
    
    document.getElementsByClassName('registerStudents')[0].style.opacity = 0.5;
    document.getElementById('studentLink').style.cursor = "default";
    document.getElementById('studentLink').setAttribute('href','javascript:void(0)');
    
    document.getElementById('linkPupils').style.opacity = 0.5;
    document.getElementById('linkPupils').style.cursor = "default";
    document.getElementById('linkPupils').setAttribute('href','javascript:void(0)');
}

function navigatePay(){
    
    var params ={
        show: 1,
        nvg: 1,
    };
    
    gotoPage('home', 'registerPay', params);
}

function gotoTop(){
    document.body.scrollTop = 0;
    document.documentElement.scrollTop = 0;
}

function loadScreen() {
    var popup = document.getElementsByClassName("popupLoad")[0];
    
    var style = document.createElement('style');
    document.head.appendChild(style);
    gotoTop();
    style.sheet.insertRule(`.popupLoad {position: fixed;
        top: 0;
        left: 0;
        width: 100%;
        height: ${window.screen.height <= 635 ? "auto" : '100%'};
        background-color: rgba(0,0,0,0.5); display:flex; align-items: center;
        ${window.screen.height <= 635 ? 'padding: 10px 0px;' : null}}`);
    
    document.body.style.overflowY = window.screen.height <= 635 ? "scroll" : "hidden";
        
    popup.innerHTML = `
    <div class="loaded">
        <div class="loaderWrapper">
            <div class="loader"></div>
        </div>
    </div>`;
}

function closeLoad() {
    var popup = document.getElementsByClassName("popupLoad")[0];
    var style = document.createElement('style');
    document.head.appendChild(style);
    
    style.sheet.insertRule(`.popupLoad {display: none}`);
        document.body.style.overflowY = "scroll";
}