window.onload = function(){
   initApp();
  // init();
    //Close onLoad
};


function initApp(){
    var userName = getParameterByName("userName");
    var labelUserName  = document.getElementById("userName");
    labelUserName.innerHTML= userName;   
    welcomeApp(userName);  
    
    var closeMenu = document.getElementById("closeMenu");
    var openMenu = document.getElementById("openMenu");
    var sidebar = document.getElementById("sidebar");
    var sidebarOnlyIcon = document.getElementById("sidebarOnlyIcon");
    if(closeMenu){
        closeMenu.addEventListener("click", function(){
            if(sidebar){
                /*sidebar.style.visibility = "hidden";
                sidebar.style.position = "absolute";
                sidebarOnlyIcon.style.visibility ="visible";
                sidebarOnlyIcon.style.position = "relative";*/
                sidebarOnlyIcon.style.display= "block"
                sidebar.style.display ="none";
                document.getElementById("content").style.marginLeft = "65px";
                if(document.getElementsByClassName("cellContainer")){
                    document.getElementsByClassName("cellContainer")[0].style.width="50%"    
                }
                
            }
        });
    }
    
    if (openMenu){
        openMenu.addEventListener("click", function(){
            if(sidebarOnlyIcon){/*
                sidebarOnlyIcon.style.visibility = "hidden";
                sidebarOnlyIcon.style.position = "absolute";
                sidebar.style.visibility = "visible";
                sidebar.style.position = "relative";*/
                sidebarOnlyIcon.style.display= "none"
                sidebar.style.display ="block";
                document.getElementById("content").style.marginLeft = "229px";
                document.getElementsByClassName("cellContainer")[0].style.width="60%"
            }
        });
    }
    
     var menuLogout = document.getElementById("iconLogout");
     menuLogout.addEventListener("click", function(){
         myLogout();
     });
     
     
}

function welcomeApp(userName){
    var par = {};
        par.sessionid = sessionStorage.getItem("sessionId");
        callWS("GET", "menus/list", par, respuesta);
        function respuesta(status, rsp){
            switch(status){
                case 200:
                   let menuList = JSON.parse(rsp);
                   createMenu(menuList, {"userName": userName });
                    break;
                case 400:
                    /*gotoPage("error","main","");*/
                   console.log(JSON.parse(rsp)); 
                    break;
                case 401:
                    gotoPage("login","main","");
                    console.log(JSON.parse(rsp));
                    break;
                case 500:
                  console.log(JSON.parse(rsp));
                    break;
                default:
                   // console.log(rsp);
            }
        }
}

// funcion para construir el menu
function createMenu( menu, userName ){
    var listMenu = menu.options;
    //console.log("menu", menu.options);
    var actpag = getParameterByName("sid");
    for(let i=0; i < listMenu.length; i++){
        var li = document.createElement("li");
        var liBarra = document.createElement("li");
        
        if (listMenu[i].submenu.length === 0){
            var a = document.createElement("a");
            var span = document.createElement("span");
            var textspan = document.createTextNode( listMenu[i].dsc );
            //a.href="www.google.com";
            span.appendChild(textspan);
            a.innerHTML= "<i class= 'fas "+listMenu[i].icon +" '> </i>";
            a.appendChild(span);
            li.appendChild(a);
            li.setAttribute("id", listMenu[i].content);
            /* Active li */
            var sid = getParameterByName("sid");
            if(sid ===  listMenu[i].content){
                li.classList.add("liAct");    
                input.checked = true;
            }
            li.classList.add("noSubmenu");
           if(listMenu[i].content == 'preferences' || listMenu[i].content == 'accounts' || listMenu[i].content == 'currencies' || listMenu[i].content == 'paymentmethods' || listMenu[i].content == 'users' || listMenu[i].content == 'operations' || listMenu[i].content == 'sessions' || listMenu[i].content == 'orders' || listMenu[i].content == 'exchanges' || listMenu[i].content == 'countries'  || listMenu[i].content == 'states' || listMenu[i].content == 'cities' || listMenu[i].content == 'balancesheet' || listMenu[i].content == 'userbalance' || listMenu[i].content == 'transactions' || listMenu[i].content == 'audit'){
                li.addEventListener("click", function(){
                    let parameterUserName = getParameterByName("userName");                     ;
                     gotoPage("menu",listMenu[i].content, {"userName": parameterUserName});
                 });    
            } else {
                if(listMenu[i].dsc != "Salir"){
                    li.style.opacity = "60%";  
                    li.getElementsByTagName("a")[0].style.color= "#7d7d7d";
                    li.style.pointerEvents="none";
                }
            }
            document.getElementById("ulSidebar").appendChild(li);
            
            var aOnlyIcon = document.createElement("a");
            aOnlyIcon.innerHTML= "<i class= 'fas "+listMenu[i].icon +" '> </i>";
            liBarra.appendChild(aOnlyIcon);
            document.getElementById("barraUlSidebar").appendChild(liBarra);
            if(listMenu[i].content == 'preferences' || listMenu[i].content == 'accounts' || listMenu[i].content == 'currencies' || listMenu[i].content == 'paymentmethods' || listMenu[i].content == 'users' || listMenu[i].content == 'operations' || listMenu[i].content == 'sessions' || listMenu[i].content == 'orders' || listMenu[i].content == 'exchanges' || listMenu[i].content == 'countries'  || listMenu[i].content == 'states'  || listMenu[i].content == 'cities' || listMenu[i].content == 'balancesheet' || 
            listMenu[i].content == 'userbalance' || listMenu[i].content == 'transactions' || listMenu[i].content == 'audit' || listMenu[i].content == 'ocupations'  || listMenu[i].content == 'pairs' || listMenu[i].content == 'economicact'){
                li.addEventListener("click", function(){
                    let parameterUserName = getParameterByName("userName");
                     gotoPage("menu",listMenu[i].content, {"userName": parameterUserName});
                 });    
            } else {
               if(listMenu[i].dsc != "Salir"){
                    li.style.opacity = "60%";
                }
            }
         
        } else {
            var input = document.createElement("input");
            var label = document.createElement("label");
            input.type="checkbox";
            input.setAttribute("id", "list-item-"+i);
            label.htmlFor = "list-item-"+i;
            label.classList.add("first");
            label.innerHTML= "<i class= 'fas "+listMenu[i].icon +" '> </i> "+ listMenu[i].dsc;
            var ulSub = document.createElement("ul");
            var submenu = listMenu[i].submenu;
            li.appendChild(input);
            li.appendChild(label);
            
            var aOnlyIcon = document.createElement("a");
            aOnlyIcon.innerHTML= "<i class= 'fas "+listMenu[i].icon +" '> </i>";
            //aOnlyIcon.dataset.idCheckAsociate = "list-item-"+i;
            aOnlyIcon.addEventListener("click", function(){
                sidebarOnlyIcon.style.display= "none"
                sidebar.style.display ="block"
                document.getElementById("list-item-"+i).checked = true;
            });
            
            liBarra.appendChild(aOnlyIcon);
            document.getElementById("barraUlSidebar").appendChild(liBarra);
         
            for (let j=0; j < submenu.length; j++){
                var liSub = document.createElement("li");
                liSub.innerHTML = submenu[j].dsc;
                liSub.setAttribute("id", submenu[j].content);
                liSub.classList.add("submenu");
                const subUrl= submenu[j].content;
                /* Active li */
                var sid = getParameterByName("sid");
                if(sid ===  submenu[j].content){
                    liSub.classList.add("liAct");
                    input.checked = true;
                }
                if(submenu[j].content == 'accounts' || submenu[j].content == 'currencies' || submenu[j].content == 'paymentmethods' || submenu[j].content == 'users' || submenu[j].content == 'operations' || submenu[j].content == 'sessions' || submenu[j].content == 'orders' || submenu[j].content == 'exchanges' || submenu[j].content == 'countries'  || 
                submenu[j].content == 'states'  || submenu[j].content == 'cities' || submenu[j].content == 'balancesheet' || submenu[j].content == 'userbalance' || submenu[j].content == 'transactions' || submenu[j].content == 'audit' ||
                submenu[j].content == 'ocupations'  || submenu[j].content == 'pairs' || submenu[j].content == 'economicact'){
                    liSub.addEventListener("click", function(){
                        let parameterUserName = getParameterByName("userName");
                        gotoPage("menu",subUrl, {"userName": parameterUserName});
                    });   
                } else {
                    liSub.style.opacity = "60%";
                    liSub.style.marginLeft= "0";
                    liSub.style.paddingLeft= "40px";
                    liSub.style.borderBottom= "none";
                    liSub.style.color= "rgba(125, 125, 125, 0.5)";
                    liSub.style.pointerEvents="none";
                }
                ulSub.appendChild(liSub);
                li.appendChild(ulSub);
                
            }
            li.classList.add("yesSubmenu");
            document.getElementById("ulSidebar").appendChild(li);  
        }
    }
    
    if(li){
        li.lastChild.addEventListener("click", function(){
             myLogout();
        });
    }
     
     
    if(liBarra){
        liBarra.lastChild.addEventListener("click", function(){
            myLogout();
        });
    }
}