$("#identitas").val(identitas);

$(".subnilai").on("click", function(e){
    e.preventDefault();
    var nilai       = document.querySelectorAll('[id^="nilai"]');
    var nama_nilai  = document.querySelectorAll('[id^="nama_nilai"]');
    var isObject    = {};
    
    for(var i = 0; i < nilai.length; i++){
        // if(i == 0){
        //     isObject['id_kp'] = $("#idkp")
        // }
        isObject[nama_nilai[i].innerHTML] = nilai[i].value;
    }

    console.log(isObject);

    
    insertnilai(isObject);
})

function insertnilai(obj){
    var isObject = obj;
    var isObject2 = {};

    // var nopegawai = nopeg;
    for(i of Object.keys(isObject)){
        console.log(i+" -> "+isObject[i])
        if(isObject[i] == 0 || isObject[i] < 0 || isObject[i] > 100){
            swal({
                title: "Alert",
                text:  "Nilai harus berisi angka 0 - 100",
                confirmButtonColor: "#2196F3",
                type: "error"
            }); 
            return false
        }
        
    }

    isObject2.idkp          = $("#idkp").val();
    isObject2.identitas     = $("#identitas").val();
    isObject2.ididentitas   = $("#id_identitas").val(); 
    console.log(isObject2.identitas);

    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: baseURL + '/jsondata/submitnilaipenguji',
        async: false,
        data: {
            iparam	    : cryptoEncrypt(PHRASE, isObject),
            iparam2	    : cryptoEncrypt(PHRASE, isObject2),
        },
        success: function(response){
            
            if(response.code == CODE_SUCCESS){
                // let result  = cryptoDecrypt(PHRASE, response.data);
                // console.log(result.data);
                // let data    = result.data;
                // let counter = 1;
                // console.log(data);
                swal({
                    title: "Alert",
                    text: "Nilai Pengujii di Input",
                    confirmButtonColor: "#66BB6A",
                    type: "success"
                }, function(){
                    // $("tbody").empty();
                    // loadUser();  
                    // loadbobot();
                    // if(nopegawai){
                    //     window.location = baseURL + "/dosen/penilaianpenguji" 
                    // }else {
                        window.location = baseURL + "/penilaian/penilaianpenguji" 
                    // }
                    

                });
                
                
                
            } else {
                swal({
                    title: "Alert",
                    text:  response.info+ " Silahkan coba kembali :)",
                    confirmButtonColor: "#2196F3",
                    type: "error"
                });  
            }
            
        },
        error: function(xhr) {
            loaderPage(false);
            if(xhr.status != 200){
                swal({
                    title: "Galat",
                    text:  xhr.status+"-"+xhr.statusText+"<br> Silahkan coba kembali :)",
                    confirmButtonColor: "#2196F3",
                    type: "error"
                });                        
            }
        }
    });
}

const param = 82
loadparambobot(param);
function loadparambobot(param){
    var isObject = {}
    isObject.code = param;

    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: baseURL + '/jsondata/loadparambobot',
        // async: false,
        data: {
            iparam	    : cryptoEncrypt(PHRASE, isObject),
        },
        success: function(response){
            // console.log(response.code)
            // $("tbody").empty();
            
            if(response.code == CODE_SUCCESS){
                let result  = cryptoDecrypt(PHRASE, response.data);
                // console.log(result.data);
                let data    = result.data;
                console.log(data);
                $res = '';
                $totalbobot = 0;
                for(i in data){
                    $res += `<div class="form-group" >
                                <label id="nama_nilai`+data[i].id_bobot+`" class="col-lg-3 control-label text-semibold">`+data[i].nama_bobot+`</label>
                                <div class="col-lg-9">
                                    <div class="input-group">
                                        <span class="input-group-addon"><i class="icon-home4"></i></span>
                                        <input value="" id="nilai`+data[i].id_bobot+`" type="number" class="form-control" placeholder="`+data[i].nama_bobot+`&hellip;">
                                    </div>
                                    <span class="help-block">maksimal : 100</span>
                                </div>
                            </div> `;
                    $totalbobot += parseFloat(data[i].bobot);
                }
                $("#forms").html($res);
                console.log($totalbobot);

                
            } else {
                swal({
                    title: "Alert",
                    text:  response.info+ " Silahkan coba kembali :)",
                    confirmButtonColor: "#2196F3",
                    type: "error"
                });  
            }
            
        },
        error: function(xhr) {
            loaderPage(false);
            if(xhr.status != 200){
                swal({
                    title: "Galat",
                    text:  xhr.status+"-"+xhr.statusText+"<br> Silahkan coba kembali :)",
                    confirmButtonColor: "#2196F3",
                    type: "error"
                });                        
            }
        }
    });
}

loadparambobotvalidation();
function loadparambobotvalidation(){
    var isObject = {}
    isObject.code = null;
    // var totalbobot = 0;

    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: baseURL + '/jsondata/loadparambobot',
        async: false,
        data: {
            iparam	    : cryptoEncrypt(PHRASE, isObject),
        },
        success: function(response){
            // console.log(response.code)
            // $("tbody").empty();
            
            if(response.code == CODE_SUCCESS){
                let result  = cryptoDecrypt(PHRASE, response.data);
                // console.log(result.data);
                let data    = result.data;
                console.log(data);
                $totalbobot = 0;
                for(i in data){
                    $totalbobot += parseFloat(data[i].bobot);
                }
                $(".alertno").hide();
                if(parseInt($totalbobot) < 1 || parseInt($totalbobot) > 1){
                    // $(".subnilai").attr('disabled', true);
                    $(".alertno").show();
                }
                
            } else {
                swal({
                    title: "Alert",
                    text:  response.info+ " Silahkan coba kembali :)",
                    confirmButtonColor: "#2196F3",
                    type: "error"
                });  
            }
            
        },
        error: function(xhr) {
            loaderPage(false);
            if(xhr.status != 200){
                swal({
                    title: "Galat",
                    text:  xhr.status+"-"+xhr.statusText+"<br> Silahkan coba kembali :)",
                    confirmButtonColor: "#2196F3",
                    type: "error"
                });                        
            }
        }
    });
}

loaddatakp(nim)
function loaddatakp(nim){
    
    var isObject = {};
    isObject.nim = nim;
    var identitas = $("#identitas").val();
    console.log(isObject.nim) 
    var status;
    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: baseURL + '/jsondata/loaddatakps',
        // async: false,
        data: {
            iparam	    : cryptoEncrypt(PHRASE, isObject),
        },
        success: function(response){
                
            if(response.code == CODE_SUCCESS){
                let result  = cryptoDecrypt(PHRASE, response.data);
                let data    = result.data;
                let datas   = data[0];
                console.log(datas.file_foto)
                $('#name').html(datas.nama);
                $('#nim').html(datas.nim);
                $('#idkp').val(datas.id_kp);
                $('#judul').html(datas.judul_laporan);
                $('#idkp').val(datas.id_kp);
                $('#program-studi').html(datas.nama_prodi)
                // $('#img-mhsw').attr('src', baseURL+'/public/data/profilepicture/'+datas.file_foto);
                $url = baseURL+'/data/profilepicture/'+datas.file_foto;
                // console.log()
                if(fileExists($url)){
                    $('#img-mhsw').attr('src', baseURL+'/data/profilepicture/'+datas.file_foto);
                } else {
                    // ('#img-mhsw').attr('src', baseURL+'/public/data/profilepicture/defaultpics.png');
                    $("#img-mhsw").attr('src', baseURL+"/data/profilepicture/defaultpics.png");
                }
                // console.log(datas.tanggal)
                if(asdingding(datas.tanggal, datas.jam) > 0){
                    $(".subnilai").attr('disabled', true)
                }
                if(identitas == 'pembimbing'){
                    $("#id_identitas").val(datas.no_peg);
                } else if(identitas == 'penguji'){
                    $("#id_identitas").val(datas.penguji_1);
                }
                
                ////buat disable kalo udah upload berkas
                // if(datas.id_berkas != null || datas.id_berkas == ''){
                //     $("#btn-add").attr('disabled', true)
                // }

                
            } else {
                $("#btn-add").attr('disabled', true)
                // $("#updateberkas").hide();
                // swal({
                //     title: "Alert",
                //     text:  response.info+ " Silahkan coba kembali :)",
                //     confirmButtonColor: "#2196F3",
                //     type: "error"
                // });
            }

            
            
        },
        error: function(xhr) {
            loaderPage(false);
            if(xhr.status != 200){
                swal({
                    title: "Galat",
                    text:  xhr.status+"-"+xhr.statusText+"<br> Silahkan coba kembali :)",
                    confirmButtonColor: "#2196F3",
                    type: "error"
                });                        
            }
        }
    });

}

// console.log(asdingding());
function asdingding(tgl, jam){
    var crack = tgl.split('-');
    var strtime = crack[1]+'/'+crack[2]+'/'+crack[0]+" "+jam;
    console.log(strtime);

    const date1 = new Date();
    console.log(jam);
    const date2 = new Date(strtime);
    console.log("skrg: "+date1)
    console.log("jdwl: "+date2)
    const diffTime = date2 - date1;
    const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24)); 
    console.log(diffTime + " milliseconds");
    console.log(diffDays + " days");
    return diffTime;
}

/* disabling scroll input number */
$('.form-group').on('focus', 'input[type=number]', function (e) {
    $(this).off('wheel.disableScroll')
})

$('.form-group').on('blur', 'input[type=number]', function (e) {
    $(this).off('wheel.disableScroll')
})

/* only numbweer */
$(function() {
    $('.form-group').on('keydown', 'input[type=number]', function(e){
        -1!==$
        .inArray(e.keyCode,[46,8,9,27,13,110,190]) || /65|67|86|88/
        .test(e.keyCode) && (!0 === e.ctrlKey || !0 === e.metaKey)
        || 35 <= e.keyCode && 40 >= e.keyCode || (e.shiftKey|| 48 > e.keyCode || 57 < e.keyCode)
        && (96 > e.keyCode || 105 < e.keyCode) && e.preventDefault()
    });
})

$('input[type=number]').on("wheel", function (e) { 
    $(this).blur(); 
}); 

loadstatus(nim);
function loadstatus(nim){
    var isObject = {};
    isObject.nim = nim;
    
    var status;
    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: baseURL + '/jsondata/loadstatusberkasm',
        async: false,
        data: {
            iparam	    : cryptoEncrypt(PHRASE, isObject),
        },
        success: function(response){
                
            if(response.code == CODE_SUCCESS){
                let result  = cryptoDecrypt(PHRASE, response.data);
                let data    = result.data;
                
                $("#idberkas").val(data[0].idberkas);
                if(data[0].isvalidtu == 51 || data[0].isvalidtu == 53 || data[0].isvalidtu == 52){

                    $('.btn-save').prop('disabled', true);
                    var rowdata = '';
                    for(i in data){
                        rowdata += `  <ul class="media-list" style="margin-top:10px">
            
                                        <li class="media" >
                                            <div class="media-left media-middle medhov" onclick="showfile('`+baseURL+data[i].file_dir+`','`+data[i].file_name+`')">
                                                <a href="#">
                                                    <img src="`+baseURL+`/img/pdfp.png" class="img-circle img-md" alt="">
                                                </a>
                                            </div>
                            
                                            <div class="media-body medhov media-bodys" onclick="showfile('`+baseURL+data[i].file_dir+`','`+data[i].file_name+`')">
                                            <div class="media-heading text-semibold">`+data[i].file_name+`</div>
                                                <span class="text-muted __web-inspector-hide-shortcut__">File size : `+data[i].file_size+` Mb</span>
                                            </div>
                            
                                            <div class="media-right media-middle">
                                                
                                            </div>
                                        </li>
                                    </ul>`;
                    }

                    $('#listfile').empty();
                    $('#listfile').append(rowdata);
                    $("#inputberkas").hide();
                } 
                // else{
                   
                //     $("#listberkas").hide();
                // }

                // if(data[0].isvalidtu == 62){
                    
                // }

                // $("#berinfo").html(data[0].param_desc)
                // if(data[0].isvalidtu == 51){
                //     $("#ketinfo").html(data[0].keterangan);
                // } else {
                //     if(data[0].keterangan != null || data[0].keterangan != ''){
                //         $("#ketinfo").html(data[0].keterangan)
                //     }else{
                //         $("#ketinfo").html('-')
                //     }
                // }
                // status = data[0].isvalidtu;
                // $("#laporanset").val(data[0].judul_laporan)
                
                // if(data[0].isvalidtu == 62){
                //     $("#berinfo").html("Anda Belum Upload berkas")
                //     $("#updateberkas").hide();
                //     $("#ketinfo").html('-')
                // }
            } else {
                $("#berinfo").html("Anda Belum Upload berkas")
                $("#updateberkas").hide();
                
                // swal({
                //     title: "Alert",
                //     text:  response.info+ " Silahkan coba kembali :)",
                //     confirmButtonColor: "#2196F3",
                //     type: "error"
                // });
            }

            
            
        },
        error: function(xhr) {
            loaderPage(false);
            if(xhr.status != 200){
                swal({
                    title: "Galat",
                    text:  xhr.status+"-"+xhr.statusText+"<br> Silahkan coba kembali :)",
                    confirmButtonColor: "#2196F3",
                    type: "error"
                });                        
            }
        }
    });
   
}

function showfile(path, name){  
    // var cols = `<input id="nim_mhs" value="`+nim+`" type="hidden" class="form-control" placeholder="Nama Perusahaan..">`;
    $('.modal-body').empty();
    // $('.modal-body').append(cols)
    $('.modal-body').append(`<embed src="`+path+`/`+name+`" type="application/pdf"   style="width: 100%; height: 100%; min-height: 480px; ">`);
    $("#modal-review").modal('show');
   
}