var globaldata;

$("#btn-add").on('click', function(){
    $("#modal-conf").modal('show')
})

$("#btn-add-group").on('click', function(){
    $("#is_group").val(1);
    $("#modal-conf").modal('show');
})

$("#submitconf").on('click', function(e){
    e.preventDefault();
    $("#modal-conf").modal('hide')
    submitfilebimbingan();
})

$(document).ready(function(){
    loaddatakp(nim);
})

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)
                $('#nim').val(datas.nim);
                $('#idkp').val(datas.id_kp);
                $('#nopeg').val(datas.no_peg);
                
                ////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"
                });                        
            }
        }
    });

}
loadfilebimbingan()
function loadfilebimbingan(){
    var isObject = {};
    isObject.nim = nim;
    isObject.nopeg = $("#nopeg").val();

    
    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: baseURL + '/jsondata/loadfilebimbingan',
        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(data)
                $res = '';
                $keterangan= '';
                for(x in data){
                    $y = parseInt(x) + 1;

                    if(data[x].uploaded== 200){
                        var identitas = 'Dokumen Mahasiswa';
                        var label     = 'success';
                        $keterangan = `         
                                                <div class="col-md-12">
                                                    <div class="form-group">
                                                        <label class="control-label col-lg-2">Keterangan Bimbingan : </label>
                                                        <div class="col-lg-10">
                                                            <textarea rows="5" cols="5" class="form-control" readonly>`+data[x].keterangan_bimbingan+`</textarea>
                                                        </div>
                                                    </div>
                                                </div>
                                                <hr>
                                        `;
                    } else {
                        var label     = 'info';
                        var identitas = 'Dokumen Pembimbing';
                        $keterangan = `         
                                                <div class="col-md-12">
                                                    <div class="form-group">
                                                        <label class="control-label col-lg-2">Keterangan Bimbingan : </label>
                                                        <div class="col-lg-10">
                                                            <textarea rows="5" cols="5" class="form-control" readonly>`+data[x].keterangan_bimbingan+`</textarea>
                                                        </div>
                                                    </div>
                                                </div>
                                                <hr>
                                        `;
                    }
                    // alert(typeof data[x].jenis_bimbingan);
                    // alert(data[x].jenis_bimbingan);
                    if(data[x].jenis_bimbingan == 1){
                        var label_jenis = 'warning';
                        var jenis       = 'Supervisi';
                    }else{
                        var label_jenis = 'warning';
                        var jenis       = 'Bimbingan Laporan';
                    }

                    var regexpattern = /[\/.](pdf)$/i;
    // let nimMhs = nim;
                    let namefile = data[x].file_bimbingan;
                    let ext = "";
                    if(regexpattern.test(namefile)){
                        ext = `<embed src="`+baseURL+data[x].dir_file+data[x].file_bimbingan+`" type="application/pdf"   style="width: 100%; height: 100%; min-height: 877px; min-width: 670px ">`;
                    }else{
                        ext = `<img src="`+baseURL+data[x].dir_file+data[x].file_bimbingan+`" style="max-width: 81vw;">`;
                    }

                    $res += `
                            <div class="panel panel-white">
                                <div class="panel-heading">
                                    <h6 class="panel-title">
                                        <a data-toggle="collapse" data-parent="#accordion-control-right" href="#accordion-control-right-group`+$y+`" aria-expanded="true" class="collapsed" >Bimbingan #`+$y+`  </a>
                                        <div class="label label-primary">`+data[x].tanggal_bimbingan+`</div>
                                        <div class="label label-`+label+`">`+identitas+`</div>
                                        <div class="label label-`+label_jenis+`">`+jenis+`</div>
                                    </h6>
                                </div>
                                <div id="accordion-control-right-group`+$y+`" class="panel-collapse collapse" aria-expanded="false" style="height: 0px;">
                                    <div class="panel-body"  >
                                        
                                        <div class="row">
                                            `+$keterangan+`
                                        </div>
                                        
                                        <a class="btn btn-primary btn-lg" style="margin:5px" href="`+baseURL+data[x].dir_file+data[x].file_bimbingan+`" download="bimbingan`+$y+`-`+data[x].nim+`.pdf"><i class=" icon-download4"></i> Download Dokumen</a>
                                        <br>`+ext+`
                                    </div>
                                </div>
                            </div>
                        `
                        $keterangan= '';
                }
                $('#accordion-control-right').empty();
                $('#accordion-control-right').html($res);
                
                
                

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

function submitfilebimbingan(){
    loaderPage(true)
    const file = $('#file').prop('files')[0];
    var isObject = {};
    let formdata = new FormData();
    
    isObject.nim    = $("#nim").val();
    isObject.idkp   = $("#idkp").val();
    isObject.nopeg  = $("#nopeg").val();
    isObject.bukti  = file;
    isObject.jenis  = $('input[name="jenisbimbingan"]:checked').val();
    isObject.keterangan = $("#keterangan").val();
    isObject.isGroup = $("#is_group").val();
    // console.log(isObject);
    // return false;
 
    var regexpattern = /[\/.](pdf)$/i;
    // let nimMhs = nim;
    let namefile = isObject.bukti.name;
    // console.log(namefile)
    // if(!regexpattern.test(namefile)){
    //     wrongvalidation('File Harus Berupa PDF!');
    //     return false;
    // } 
    

    formdata.append('file', isObject.bukti);
    formdata.append('idkp', isObject.idkp);
    formdata.append('nopeg', isObject.nopeg);
    formdata.append('nim', isObject.nim);
    formdata.append('keterangan', isObject.keterangan);
    formdata.append('jenis', isObject.jenis);
    formdata.append('is_group', isObject.isGroup);
    formdata.append('data', JSON.stringify(globaldata));
  

    $.ajax({
        type        : 'POST',
        url         : baseURL + '/jsondata/submitfilebimbingan',
        data        : formdata,
        cache       : false,
        processData : false,
        contentType : false,
        success: function (response) {

            if(response.code == CODE_SUCCESS){
                loaderPage(false)
                // document.getElementById("form-data").reset();

                swal({
                    title:"Alert",
                    text: "Dokumen laporan telah di upload, silahkan hubungi pembimbing",
                    confirmButtonColor: "#66BB6A",
                    type: "success"
                },function(){ 
                    // $("#file1").prop("disabled", true);
                    // $("#file1").prop("disabled", true);
                    // $("#file1").prop("disabled", true);
                    // $("#file1").prop("disabled", true);
                    window.location.reload();
                });

            }else{
                loaderPage(false)
                swal({
                    title: "Alert",
                    text:  response.info+ " Silahkan coba kembali :)",
                    confirmButtonColor: "#2196F3",
                    type: "error"
                });  
            };

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


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

                

                if(data.length < 2){
                    $(".btn-save").prop('disabled',true);
                    $("#pembinfo").html('Anda belum memiliki <code>Pembimbing</code>, silahkan hubungi TU untuk mendapatkan pembimbing')
                    $("#status_awal").html('Anda Belum Memiliki Pembimbing, silahkan hubungi TU')
                    $statbimbingan = "ANDA BELUM MEMILIKI PEMBIMBING"
                }else{
                        var dosen;
                        if(data[1].gelar_dpn != null && data[1].gelar_blk != null){
                            dosen = data[1].gelar_dpn+" "+data[1].nama+", "+data[1].gelar_blk;
                            // cols += '<option value="'+data[i].NOPEG+'">'+data[i].gelar_dpn+" "+data[i].NAMA+", "+data[i].gelar_dpn+'</option> ';
                        }else if(data[1].gelar_dpn == null && data[1].gelar_blk != null){
                            dosen = data[1].nama+", "+data[1].gelar_blk;
                            // cols += '<option value="'+data[1].NOPEG+'">'+data[1].NAMA+", "+data[i].gelar_dpn+'</option> ';
                        }else if(data[1].gelar_dpn != null && data[1].gelar_blk == null){
                            dosen = data[1].gelar_dpn+" "+data[1].nama;
                            // cols += '<option value="'+data[1].NOPEG+'">'+data[1].gelar_dpn+" "+data[i].NAMA+'</option> ';
                        } else if(data[1].gelar_dpn == null && data[1].gelar_blk == null){
                            dosen = data[1].nama;
                            // cols += '<option value="'+data[i].NOPEG+'">'+data[i].NAMA+'</option> ';
                        }
                    
                    $url = baseURL+"/data/profilepicture/"+data[1].file_foto;
                    if(fileExists($url)){
                        $("#img-mhsw").attr('src', baseURL+"/data/profilepicture/"+data[1].file_foto);
                    } else {
                        $("#img-mhsw").attr('src', baseURL+"/data/profilepicture/defaultpics.png");
                    }
                    
                    
                    $("#name").html(dosen+" <i class='icon-checkmark4'></i>");
                    $("#nip").html(data[1].nip);
                    $("#initial").html(data[1].inisial);
                    $("#email").html(data[1].email)
                    
                    
                    if(data[1].approve_proses_bimbingan == null || data[1].approve_proses_bimbingan == ""){
                        $("#status_awal").html('Anda Belum Mengupload File Laporan Untuk Bimbingan');
                        $statbimbingan = "BELUM SELESAI";
                    } else if(data[1].approve_proses_bimbingan == 1){
                        $statbimbingan = "SELESAI"; 
                        $("#btn-add").attr("disabled", true);
                    }
                }
                $("#stat-bimbingan").html("Status Bimbingan: "+$statbimbingan)
            } 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"
                });                        
            }
        }
    });
    // return status;   
}

loadmahasiswabimbinganGroup(nopeg, nim);
function loadmahasiswabimbinganGroup(nopeg, nim){
    let isObject = {};
    // console.log("dimana ini gasi")
    isObject.nopeg = $("#nopeg").val();
    isObject.nim   = nim;

    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: baseURL + '/jsondata/loadmgroup',
        // 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;
                globaldata  = data;
                let content = `Pembimbing : `+data[0].nama_pembimbing+` <br><br>`;

                content += `List Mahasiswa: <br>`;
                for(x in data){
                    content += `- `+data[x].nim+` / `+data[x].nama+` <br>`;
                }

                $("#list-here").html(content);
                loadfilebimbinganGroup();
            } else {
                
                // alert("ggl")
            }

            
            
        },
        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 loadfilebimbinganGroup(){
    var isObject = {};
    isObject.nim = nim;
    isObject.nopeg = $("#nopeg").val();

    
    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: baseURL + '/jsondata/loadfilebimbingangroup',
        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(data)
                $res = '';
                $keterangan= '';
                for(x in data){
                    $y = parseInt(x) + 1;

                    if(data[x].uploaded== 200){
                        var identitas = data[x].name;
                        var label     = 'success';
                        $keterangan = `         
                                                <div class="col-md-12">
                                                    <div class="form-group">
                                                        <label class="control-label col-lg-2">Keterangan Bimbingan : </label>
                                                        <div class="col-lg-10">
                                                            <textarea rows="5" cols="5" class="form-control" readonly>`+data[x].keterangan_bimbingan+`</textarea>
                                                        </div>
                                                    </div>
                                                </div>
                                                <hr>
                                        `;
                    } else {
                        var label     = 'info';
                        var identitas = data[x].name;
                        $keterangan = `         
                                                <div class="col-md-12">
                                                    <div class="form-group">
                                                        <label class="control-label col-lg-2">Keterangan Bimbingan : </label>
                                                        <div class="col-lg-10">
                                                            <textarea rows="5" cols="5" class="form-control" readonly>`+data[x].keterangan_bimbingan+`</textarea>
                                                        </div>
                                                    </div>
                                                </div>
                                                <hr>
                                        `;
                    }
                    // alert(typeof data[x].jenis_bimbingan);
                    // alert(data[x].jenis_bimbingan);
                    if(data[x].jenis_bimbingan == 1){
                        var label_jenis = 'warning';
                        var jenis       = 'Supervisi';
                    }else{
                        var label_jenis = 'warning';
                        var jenis       = 'Bimbingan Laporan';
                    }

                    var regexpattern = /[\/.](pdf)$/i;
    // let nimMhs = nim;
                    let namefile = data[x].file_bimbingan;
                    let ext = "";
                    if(regexpattern.test(namefile)){
                        ext = `<embed src="`+baseURL+data[x].dir_file+data[x].file_bimbingan+`" type="application/pdf"   style="width: 100%; height: 100%; min-height: 877px; min-width: 670px ">`;
                    }else{
                        ext = `<img src="`+baseURL+data[x].dir_file+data[x].file_bimbingan+`" style="max-width: 81vw;">`;
                    }

                    $res += `
                            <div class="panel panel-white">
                                <div class="panel-heading">
                                    <h6 class="panel-title">
                                        <a data-toggle="collapse" data-parent="#accordion-control-right-batch" href="#accordion-control-right-group-batch`+$y+`" aria-expanded="true" class="collapsed" >Bimbingan #`+$y+`  </a>
                                        <div class="label label-primary">`+data[x].tanggal_bimbingan+`</div>
                                        <div class="label label-`+label+`">`+identitas+`</div>
                                        <div class="label label-`+label_jenis+`">`+jenis+`</div>
                                    </h6>
                                </div>
                                <div id="accordion-control-right-group-batch`+$y+`" class="panel-collapse collapse" aria-expanded="false" style="height: 0px;">
                                    <div class="panel-body"  >
                                        
                                        <div class="row">
                                            `+$keterangan+`
                                        </div>
                                        
                                        <a class="btn btn-primary btn-lg" style="margin:5px" href="`+baseURL+data[x].dir_file+data[x].file_bimbingan+`" download="bimbingan`+$y+`-`+data[x].nim+`.pdf"><i class=" icon-download4"></i> Download Dokumen</a>
                                        <br>`+ext+`
                                    </div>
                                </div>
                            </div>
                        `
                        $keterangan= '';
                }
                $('#accordion-control-right-batch').empty();
                $('#accordion-control-right-batch').html($res);
                
                
                

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