
$(".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;
    }

    
    insertnilai(isObject);
})

function insertnilai(obj){
    var isObject = obj;
    var isObject2 = {};
    loaderPage(true)
    isObject2.idkp = $("#idkp").val();
    console.log(isObject2.idkp)
    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/submitnilaiperusahaan',
        // 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 Perusahaan di Input",
                    confirmButtonColor: "#66BB6A",
                    type: "success"
                }, function(){
                    // $("tbody").empty();
                    // loadUser();  
                    // loadbobot();
                    window.location = baseURL + "/penilaian/penilaianperusahaan" 
                });
                
                
                
            } else {
                loaderPage(false);
                swal({
                    title: "Alert",
                    text:  response.info+ " Silahkan coba kembali :)",
                    confirmButtonColor: "#2196F3",
                    type: "error"
                });  
            }
            loaderPage(false);
        },
        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 = 81
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 == "Kehadiran KP"){
                        $ket = ` Kehadiran ( ≥22 hari = 100, 17-22 hari = 80, 12-17 hari = 60, <12 hari = 40 )`;
                    } else if(data[i].nama_bobot == "Sikap"){
                        $ket = `Sikap (A=93, AB=80, B=72, BC=66, C=61)`;
                    } else if(data[i].nama_bobot == "Kinerja"){
                        $ket = `Kinerja (A=93, AB=80, B=72, BC=66, C=61)`;
                    }
                    $res += `<div class="form-group" >
                                <label id="nama_nilai`+data[i].id_bobot+`" class="col-lg-2 control-label text-semibold">`+data[i].nama_bobot+`</label>
                                <div class="col-lg-10">
                                    <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"
                });                        
            }
        }
    });
}

loaddatakp(nim)
function loaddatakp(nim){
    
    var isObject = {};
    isObject.nim = nim;
    console.log(isObject.nim) 
    var status;
    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: baseURL + '/jsondata/loaddatakp',
        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)
                // $('#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");
                }

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

}

/* 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');
   
}

$("#indexsetter").on("change", function(){
    let value = $(this).val();
    // alert(typeof value);
    switch(value) {
        case '1':
          $("#forms").find("input").val(93);
          break;
        case '2':
            $("#forms").find("input").val(80);
          break;
        case '3':
            $("#forms").find("input").val(72);
            break;
        case '4':
            $("#forms").find("input").val(66);
            break; 
        case '5':
            $("#forms").find("input").val(61);
            break; 
        default:
            $("#forms").find("input").val(61);
            break;
    }
});