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

// alert(penguji_ke);
if(identitas == "pembimbing"){
    $("#title").html('<i class="icon-arrow-left52 position-left"></i> <span class="text-semibold">Dosen</span> - Input Penilaian Pembimbing');
    $("#titles").html('Input Nilai Pembimbing');
}

$(".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++){
        isObject[nama_nilai[i].innerHTML] = nilai[i].value;
    }

    console.log(isObject);

    
    insertnilai(isObject);
})

function insertnilai(obj){
    $(".subnilai").attr("disabled", true);
    var isObject = obj;
    var isObject2 = {};

    // var nopegawai = nopeg;

    isObject2.idkp          = $("#idkp").val();
    isObject2.identitas     = $("#identitas").val();
    isObject2.ididentitas   = $("#id_identitas").val(); 
    isObject2.penguji_ke    = penguji_ke;
    // console.log(isObject2.ididentitas);
// return false;
    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
        }
        
    }

    $.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){
                swal({
                    title: "Alert",
                    text: "Nilai Pengujii di Input",
                    confirmButtonColor: "#66BB6A",
                    type: "success"
                }, function(){
                    window.location = baseURL + "/dosen/penilaianpenguji"
                        // if(isObject.identitas == 'penguji'){
                        //     window.location = baseURL + "/dosen/penilaianpenguji"
                        // }else {
                        //     window.location = baseURL + "/dosen/penilaianpembimbing" 
                        // }
                });
                
                
                
            } else {
                $(".subnilai").attr("disabled", false)
                swal({
                    title: "Alert",
                    text:  response.info+ " Silahkan coba kembali :)",
                    confirmButtonColor: "#2196F3",
                    type: "error"
                });  
            }
            
        },
        error: function(xhr) {
            $(".subnilai").attr("disabled", false)
            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 = '';
                $ket = '';
                for(i in data){
                    if(data[i].nama_bobot == "Presentasi Rata-rata"){
                        $ket = `80 : Lancar, tidak membaca slide<br>
                                75 : Lancar, sesekali membaca slide<br>
                                70 : Tidak lancar<br>
                                65 : Hanya membaca slide<br>
                                60 : Tidak mengerti isi slide<br>`;
                    } else if(data[i].nama_bobot == "Sistematika Laporan"){
                        $ket = `Ada 5 poin yang bisa didapat:<br>
                                • tata bahasa baik, <br>
                                • gambar, tabel, grafik, dll dideskripsikan secara naratif dalam paragraf.<br>
                                • sedikit salah ketik, <br>
                                • runtun, <br>
                                • layout rapi: tidak banyak space kosong,<br>
                                Nilai: 80 = 5 poin; 75 = 4 poin; 70 = 3 poin; 65 = 2 poin; 60 = 1 poin<br>`
                    } else if(data[i].nama_bobot == "Konten Laporan"){
                        $ket = `Ada 5 poin yang bisa didapat:<br>
                                • memaparkan hal yang dilakukan, <br>
                                • perhitungan/proses benar,<br>
                                • ada studi kasus, <br>
                                • kesimpulan benar<br>
                                • data benar,<br>
                                Nilai: 80 = 5 poin; 75 = 4 poin; 70 = 3 poin; 65 = 2 poin; 60 = 1 poin`
                    } else if(data[i].nama_bobot == "Pemahaman / Tanya Jawab Rata-rata"){
                        $ket = `maks nilai : 100`;
                    }
                    
                    $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"><code>`+$ket+`</code></span>
                                </div>
                            </div> `;
                }
                $("#forms").html($res);
                
            } 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){
    loaderPage(true)
    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)
                $('#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)
                $url = '..'+datas.file_dir+datas.file_name;
                $("#formsembed").html(`<embed src="`+baseURL+datas.file_dir+datas.file_name+`" type="application/pdf"   style="width: 100%; height: 100%; min-height: 480px; ">`)
                $("#links").html(datas.ruang)
                // $('#img-mhsw').attr('src', '/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");
                }

                //disabling submit button
                // if(asdingding(datas.tanggal, datas.jam) > 0){
                //     $(".subnilai").attr('disabled', true)
                // }

                if(identitas == 'pembimbing'){
                    $("#id_identitas").val(datas.no_peg);
                } else if(identitas == 'penguji'){
                    if(penguji_ke == 'penguji_1'){
                        $("#id_identitas").val(datas.penguji_1);
                    }else{
                        $("#id_identitas").val(datas.penguji_2);
                    }
                    
                }
                
                ////buat disable kalo udah upload berkas
                // if(datas.id_berkas != null || datas.id_berkas == ''){
                //     $("#btn-add").attr('disabled', true)
                // }

                
            } else {
                loaderPage(false)
                $("#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"
                });                        
            }
        }
    });

}

// function newTab(url) { 
//     window.open("google.com"); 
// } 

function myFunction(element) {
    /* Get the text field */
    // var copyText = document.getElementById("links");
  
    // /* Select the text field */
    // copyText[0].select();
    // // copyText.setSelectionRange(0, 99999); /* For mobile devices */
  
    // /* Copy the text inside the text field */
    // document.execCommand("copy");
  
    // /* Alert the copied text */
    // alert("Copied the text: " + copyText.value);
    swal({
        title: "Pemberitahuan",
        text:  "Link berhasil di copy!",
        confirmButtonColor: "#2196F3",
        type: "success"
    }); 

    var $temp = $("<input>");
    $("body").append($temp);
    $temp.val($(element).text()).select();
    document.execCommand("copy");
    $temp.remove();
  }

  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(); 
}); 