$(document).ready(function(){
    // var $iduser = $('#id_user').val();
    // console.log($iduser);
   
   $("#btnselect").on("click", function(e){
        e.preventDefault()   
        console.log("button select web");

        var element     = document.getElementById("selectWeb");
        var selectVal   = element.options[element.selectedIndex].value; 
        var token       = $("#tokenvalidate").val();
        console.log(selectVal);
        console.log(token);
        isTokenMatches();
        //window.location.href = baseURL+"/Useranalytics/dashboard?webname="+selectVal+"&token="+token;
   })

   
   function isTokenMatches(){
    var element     = document.getElementById("selectWeb");
    var selectVal   = element.options[element.selectedIndex].value; 
    var token       = $("#tokenvalidate").val().substr(0, 128);

    if(token == null || token == ""){
        $("#wrongfeedback").html("TOKEN IS NULL, INSERT TOKEN!")
        return false
    }

    console.log()
    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: baseURL + '/api/websiteRowMatches',
        async: false,
        data: {
            websiteName     : selectVal,
            token           : token,
        },
        success: function(result){
            
            let data = result.data;
            console.log(data[0].count)

            if(data[0].count > 0){
                window.location.href = baseURL+"/useranalytics/dashboard?webname="+selectVal+"&token="+token;
            } else {
                $("#wrongfeedback").html("WEBSITE NAME AND TOKEN DOES NOT MATCH!")
            }
        },
        error: function(xhr){
            alert(xhr.status+'-'+xhr.statusText);
        }
    })

   }


    tokenValidate();
    function tokenValidate(){
        //console.log("cek fungsi token");
        let token = $('#tokenvalidate').val();
        //console.log(token);
        $.ajax({
            type: 'POST',
            dataType: 'json',
            url: baseURL + '/api/websitelist',
            async: false,
            data: {
                id_user     : null
            },
            success: function(result){
                //console.log(result.data);
                let data = result.data;

                for(tkn in data){
                    var option = "";
                    
                    //console.log(data[tkn].token);
                }
                // var $select = $("#selectWeb option:selected").text();
                // console.log($select);
                
            },
            error: function(xhr){
                alert(xhr.status+'-'+xhr.statusText);
            }
        })
    }


    loadwebsitelist();
    var $select = $("#selectWeb option:selected").text();
    //console.log($select);

    function loadwebsitelist(){
        let $iduser = $('#id_user').val();
        //console.log($iduser);

        $.ajax({
            type: 'POST',
            dataType: 'json',
            url: baseURL + '/api/websitelist',
            async: false,
            data: {
                id_user         : $iduser
            },
            success: function(result){
                //console.log(result.data);
                let data = result.data;

                for(web in data){
                    var option = "";

                    option += '<option value="'+data[web].website_name+'">'+data[web].website_name+'</option>';
                    $('#selectWeb').append(option);
                }
                // var $select = $("#selectWeb option:selected").text();
                // console.log($select);
                
            },
            error: function(xhr){
                alert(xhr.status+'-'+xhr.statusText);
            }
        });
    }
});