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

$("#btn-approve").on('click', function(){
    isapproveseminar();
})

function isapproveseminar(){
    event.preventDefault();
    swal({
        title: "Approval Seminar",
        text: "Setujui Mahasiswa Untuk Lanjut Ke Seminar KP?",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "Ya",
        cancelButtonText: "Tidak",
        cancelButtonColor: "#DD6B55",
        closeOnConfirm: false,
        closeOnCancel: false
      },
      function(isConfirm){
        if (isConfirm) {
            approvalseminar()
            // console.log(uname);
            // // submitting the form when user press yes
        } else {
          swal("Approval Dibatalkan", "", "error");
        }
    });
}

function approvalseminar(){
    var isObject = {}
    isObject.idkp = $("#idkp").val();
    isObject.nim  = $("#nim").val();

    // alert(isObject.nim);

    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: baseURL + '/jsondata/approvalseminar',
        async: false,
        data: {
            iparam	    : cryptoEncrypt(PHRASE, isObject),
        },
        success: function (response) {

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

                swal({
                    title:"Alert",
                    text: "Approval Berhasil!",
                    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{
                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"
                });                        
            }
        }
    });
    
}


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

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.ket    = $("#keterangan").val();
    isObject.jenis  = $('input[name="jenisbimbingan"]:checked').val();
    isObject.bukti  = file;
    isObject.data   = globaldata;
 
    var regexpattern = /[\/.](pdf)$/i;
    // let nimMhs = nim;
    let namefile = isObject.bukti.name;
    console.log(namefile)
    // if(!regexpattern.test(namefile)){
    //     loaderPage(false)
    //     wrongvalidation('File Harus Berupa PDF!');
    //     return false;
    // } 
    

    formdata.append('file', isObject.bukti);
    formdata.append('idkp', globaldata[0].idkp);
    formdata.append('nopeg', globaldata[0].nopeg);
    // formdata.append('nim', isObject.nim);
    formdata.append('keterangan', isObject.ket);
    formdata.append('jenis', isObject.jenis);
    formdata.append('data', JSON.stringify(globaldata));
    // console.log(globaldata)
    // for ( var key in globaldata ) {
    //     // formdata.append(key, globaldata[key]);
    //     console.log(key);
    // }
    
  

    $.ajax({
        type        : 'POST',
        url         : baseURL + '/jsondata/submitfilebimbingandsngroup',
        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 telah di upload, silahkan hubungi mahasiswa",
                    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"
                });                        
            }
        }
    });
    
}


function loadfilebimbingan(){
    var isObject = {};

    isObject.nim   = nim;
    isObject.nopeg = nopeg;
    isObject.data  = globaldata;

    
    $.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="row" >
                                                <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>
                                            </div>
                                            <hr>
                                        `;
                    } else {
                        var label     = 'info';
                        var identitas = data[x].name;
                        $keterangan = `     <div class="row" >
                                                <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>
                                            </div>
                                            <hr>
                                        `
                    }

                    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" >
                                        
                                            `+$keterangan+`
                                        
                                        
                                        <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)
                $("#btn-approve").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"
                });                        
            }
        }
    });
}

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);
                $('#name').html(datas.nama);
                $('#nimMhs').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="`+datas.file_dir+datas.file_name+`" type="application/pdf"   style="width: 100%; height: 100%; min-height: 480px; ">`)
                $("#links").html(datas.ruang)
                console.log(datas.file_foto);
                // if(datas.file_foto != null || datas.file_foto != ""){
                //     $('#img-mhsw').attr('src', baseURL+'/data/profilepicture/'+datas.file_foto);
                // }
                var url = baseURL+'/data/profilepicture/'+datas.file_foto;
                if(fileExists(url)){
                    $('#img-mhsw').attr('src', baseURL+'/data/profilepicture/'+datas.file_foto);
                }else{
                    $('#img-mhsw').attr('src', baseURL+'/data/profilepicture/defaultpics.png');
                }
                
                if(datas.approve_proses_bimbingan == 1){
                    $statbim = "SELESAI";   
                    $("#btn-approve").attr("disabled", true);
                    $("#btn-add").attr("disabled", true);
                }else {
                    $statbim = "BELUM SELESAI";
                    $("#btn-approve").attr("disabled", false);
                    $("#btn-add").attr("disabled", false);
                }

                $("#status-bimbingan").html("Status Bimbingan: "+$statbim)
                ////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"
                });                        
            }
        }
    });

}

var globaldata;


loadmahasiswabimbingan(nopeg);
function loadmahasiswabimbingan(nopeg){
    let isObject = {};

    isObject.nopeg = nopeg;
    isObject.nim   = null;

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