
var modalTemplate = '<div class="modal-dialog modal-lg" role="document">\n' +
'  <div class="modal-content">\n' +
'    <div class="modal-header">\n' +
'      <div class="kv-zoom-actions btn-group">{toggleheader}{fullscreen}{borderless}{close}</div>\n' +
'      <h6 class="modal-title">{heading} <small><span class="kv-zoom-title"></span></small></h6>\n' +
'    </div>\n' +
'    <div class="modal-body">\n' +
'      <div class="floating-buttons btn-group"></div>\n' +
'      <div class="kv-zoom-body file-zoom-content"></div>\n' + '{prev} {next}\n' +
'    </div>\n' +
'  </div>\n' +
'</div>\n';

// Buttons inside zoom modal
var previewZoomButtonClasses = {
toggleheader: 'btn btn-default btn-icon btn-xs btn-header-toggle',
fullscreen: 'btn btn-default btn-icon btn-xs',
borderless: 'btn btn-default btn-icon btn-xs',
close: 'btn btn-default btn-icon btn-xs'
};

// Icons inside zoom modal classes
var previewZoomButtonIcons = {
prev: '<i class="icon-arrow-left32"></i>',
next: '<i class="icon-arrow-right32"></i>',
toggleheader: '<i class="icon-menu-open"></i>',
fullscreen: '<i class="icon-screen-full"></i>',
borderless: '<i class="icon-alignment-unalign"></i>',
close: '<i class="icon-cross3"></i>'
};

// File actions
var fileActionSettings = {
zoomClass: 'btn btn-link btn-xs btn-icon',
zoomIcon: '<i class="icon-zoomin3"></i>',
dragClass: 'btn btn-link btn-xs btn-icon',
dragIcon: '<i class="icon-three-bars"></i>',
removeClass: 'btn btn-link btn-icon btn-xs',
removeIcon: '<i class="icon-trash"></i>',
indicatorNew: '<i class="icon-file-plus text-slate"></i>',
indicatorSuccess: '<i class="icon-checkmark3 file-icon-large text-success"></i>',
indicatorError: '<i class="icon-cross2 text-danger"></i>',
indicatorLoading: '<i class="icon-spinner2 spinner text-muted"></i>'
};

$('.file-input').fileinput({
    browseLabel: 'Browse',
    browseIcon: '<i class="icon-file-plus"></i>',
    uploadLabel: '',
    uploadClass: 'hidden',
    uploadIcon: '',
    removeIcon: '<i class="icon-cross3"></i>',
    layoutTemplates: {
        icon: '<i class="icon-file-check"></i>',
        modal: modalTemplate
    },
    initialCaption: "No file selected",
    previewZoomButtonClasses: previewZoomButtonClasses,
    previewZoomButtonIcons: previewZoomButtonIcons,
    fileActionSettings: fileActionSettings
});

// $('.file-input').fileinput({
//     previewFileType: 'image',
//     browseLabel: 'Select',
//     browseClass: 'btn bg-slate-700',
//     browseIcon: '<i class="icon-image2 position-left"></i> ',
//     removeLabel: 'Remove',
//     removeClass: 'btn btn-danger',
//     removeIcon: '<i class="icon-cancel-square position-left"></i> ',
//     uploadClass: 'btn bg-teal-400 btn-upload',
//     uploadIcon: '<i class="icon-file-upload position-left"></i> ',
//     layoutTemplates: {
//         icon: '<i class="icon-file-check"></i>',
//         modal: modalTemplate
//     },
//     initialCaption: "Please select image",
//     mainClass: 'input-group',
//     previewZoomButtonClasses: previewZoomButtonClasses,
//     previewZoomButtonIcons: previewZoomButtonIcons,
//     fileActionSettings: fileActionSettings

//     });

// upload function

$(".btn-save").on("click", function(){
    saveFileMhs2(nim, role);
})

$(".btn-update").on("click", function(){
    updateFileMhs2(nim, role);
});

$(".btn-upload").on("click", function(e){
    e.preventDefault()
    // console.log(nism);
    saveFileMhs(nim);
})

function saveFileMhs(nim){
    // loaderPage(true);
    // console.log(nim);
    let nimMhs = nim;
    const fileupload1 = $('#file1').prop('files')[0];
    const fileupload2 = $('#file2').prop('files')[0];
    const fileupload3 = $('#file3').prop('files')[0];
    const fileupload4 = $('#file4').prop('files')[0];
    
    if(document.getElementById("file1").files.length == 0 || document.getElementById("file2").files.length == 0 || document.getElementById("file3").files.length == 0 || document.getElementById("file4").files.length == 0     ){
        swal({
            title: "Alert",
            text:  "File Tidak Boleh Kosong",
            confirmButtonColor: "#2196F3",
            type: "warning"
        });  
        return false;
    } else {
        var regexpattern = /[\/.](pdf)$/i;
        let nimMhs = nim;
        let nameF1 = fileupload1.name;
        let nameF2 = fileupload2.name;
        let nameF3 = fileupload3.name;
        let nameF4 = fileupload4.name;

        if(!regexpattern.test(nameF1)){
            swal({
                title: "Alert",
                text:  "File Harus Berupa PDF!",
                confirmButtonColor: "#2196F3",
                type: "warning"
            });  
            return false;
        } 
    
        if(!regexpattern.test(nameF2)){
            swal({
                title: "Alert",
                text:  "File Harus Berupa PDF!",
                confirmButtonColor: "#2196F3",
                type: "warning"
            });  
            return false;
        } 
    
        if(!regexpattern.test(nameF3)){
            swal({
                title: "Alert",
                text:  "File Harus Berupa PDF!",
                confirmButtonColor: "#2196F3",
                type: "warning"
            });  
            return false;
        } 
    
        if(!regexpattern.test(nameF4)){
            swal({
                title: "Alert",
                text:  "File Harus Berupa PDF!",
                confirmButtonColor: "#2196F3",
                type: "warning"
            });  
            return false;
        } 
    }

    



    // if(!regexpattern.test(nameF1)){
    //     swal({
    //         title: "Alert",
    //         text:  "File Harus Berupa PDF!",
    //         confirmButtonColor: "#2196F3",
    //         type: "warning"
    //     });  
    //     return false;
    // } 

    // if(!regexpattern.test(nameF2)){
    //     swal({
    //         title: "Alert",
    //         text:  "File Harus Berupa PDF!",
    //         confirmButtonColor: "#2196F3",
    //         type: "warning"
    //     });  
    //     return false;
    // } 

    // if(!regexpattern.test(nameF3)){
    //     swal({
    //         title: "Alert",
    //         text:  "File Harus Berupa PDF!",
    //         confirmButtonColor: "#2196F3",
    //         type: "warning"
    //     });  
    //     return false;
    // } 

    // if(!regexpattern.test(nameF4)){
    //     swal({
    //         title: "Alert",
    //         text:  "File Harus Berupa PDF!",
    //         confirmButtonColor: "#2196F3",
    //         type: "warning"
    //     });  
    //     return false;
    // } 
    


    let formData = new FormData();

    formData.append('fileupload1', fileupload1); // set file ke tipe data binary
    formData.append('fileupload2', fileupload2); // set file ke tipe data binary
    formData.append('fileupload3', fileupload3); // set file ke tipe data binary
    formData.append('fileupload4', fileupload4); // set file ke tipe data binary
    formData.append('nimMhs', nimMhs); 
    console.log(formData.nimMhs);
   
    $.ajax({
        type        : 'POST',
        url         : baseURL + '/jsondata/saveberkas',
        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: "Berkas mahasiswa telah ditambahkan ",
                    confirmButtonColor: "#66BB6A",
                    type: "success"
                },function(){ 
                    // $("#file1").prop("disabled", true);
                    // $("#file1").prop("disabled", true);
                    // $("#file1").prop("disabled", true);
                    // $("#file1").prop("disabled", true);
                    window.location = baseURL+"/admin/mahasiswalist"
                });

            }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"
                });                        
            }
        }
    });
    console.log(fileupload2);
    console.log(fileupload3);
    console.log(fileupload4);
}

function saveFileMhs2(nim, role){
    // loaderPage(true);
    let roles   = role;
    let nimMhs  = nim;
    let judul   = $("#laporan").val();
    
    
    const fileupload1 = $('#file1').prop('files')[0];
    const fileupload2 = $('#file2').prop('files')[0];
    const fileupload3 = $('#file3').prop('files')[0];
    
    if(document.getElementById("file1").files.length == 0 || document.getElementById("file2").files.length == 0 || document.getElementById("file3").files.length == 0 ){
        swal({
            title: "Alert",
            text:  "File Tidak Boleh Kosong",
            confirmButtonColor: "#2196F3",
            type: "warning"
        });  loaderPage(false);
        return false;
    } else {
        var regexpattern = /[\/.](pdf)$/i;
        let nameF1 = fileupload1.name;
        let nameF2 = fileupload2.name;
        let nameF3 = fileupload3.name;
        

        if(!regexpattern.test(nameF1) || !regexpattern.test(nameF2) || !regexpattern.test(nameF3)){
            swal({
                title: "Alert",
                text:  "File Harus Berupa PDF!",
                confirmButtonColor: "#2196F3",
                type: "warning"
            });  loaderPage(false);
            return false;
        } 
        loaderPage(false);
    }

    
    loaderPage(false);
    // arrayOfFiles.push(fileupload1);
    // arrayOfFiles.push(fileupload2);
    // arrayOfFiles.push(fileupload3);
    let formData = new FormData();

    formData.append('fileupload1', fileupload1); // set file ke tipe data binary
    formData.append('fileupload2', fileupload2);
    formData.append('fileupload3', fileupload3);
    formData.append('nimMhs', nimMhs); 
    formData.append('judul', judul);
    formData.append('role', roles);
   
    $.ajax({
        type        : 'POST',
        url         : baseURL + '/jsondata/saveberkasmahasiswaa',
        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: "Berkas mahasiswa telah ditambahkan ",
                    confirmButtonColor: "#66BB6A",
                    type: "success"
                },function(){ 
                    // $("#file1").prop("disabled", true);
                    // $("#file1").prop("disabled", true);
                    // $("#file1").prop("disabled", true);
                    // $("#file1").prop("disabled", true);
                    window.location = baseURL+"/admin/mahasiswalist"
                });

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

function updateFileMhs2(nim, role){
    let roles  = role;
    let nimMhs = nim;
    let judul  = $("#laporan").val();
    
    const fileupload1 = $('#file1upd').prop('files')[0];
    const fileupload2 = $('#file2upd').prop('files')[0];
    const fileupload3 = $('#file3upd').prop('files')[0];

    let formData = new FormData();

    if(!document.getElementById("file1upd").files.length == 0 ){
        var regexpattern = /[\/.](pdf)$/i;
        let nameF1 = fileupload1.name;
        

        if(!regexpattern.test(nameF1)){
            swal({
                title: "Alert",
                text:  "File Harus Berupa PDF!",
                confirmButtonColor: "#2196F3",
                type: "warning"
            });  
            return false;
        }else{
            formData.append('fileupload1', fileupload1);
        }
    } 

    if(!document.getElementById("file2upd").files.length == 0 ){
        var regexpattern = /[\/.](pdf)$/i;
        let nameF1 = fileupload2.name;
        

        if(!regexpattern.test(nameF1)){
            swal({
                title: "Alert",
                text:  "File Harus Berupa PDF!",
                confirmButtonColor: "#2196F3",
                type: "warning"
            });  
            return false;
        }else{
            formData.append('fileupload2', fileupload2);
        }
    } 

    if(!document.getElementById("file3upd").files.length == 0 ){
        var regexpattern = /[\/.](pdf)$/i;
        let nameF1 = fileupload3.name;
        

        if(!regexpattern.test(nameF1)){
            swal({
                title: "Alert",
                text:  "File Harus Berupa PDF!",
                confirmButtonColor: "#2196F3",
                type: "warning"
            });  
            return false;
        }else{
            formData.append('fileupload3', fileupload3);
        }
    } 

    formData.append('nimMhs', nimMhs); 
    // formData.append('idberkas', idberkas);
    formData.append('role', roles)
   
    $.ajax({
        type        : 'POST',
        url         : baseURL + '/jsondata/updateberkasmahasiswa',
        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: "Berkas mahasiswa telah di update ",
                    confirmButtonColor: "#66BB6A",
                    type: "success"
                },function(){ 
                    // $("#file1").prop("disabled", true);
                    // $("#file1").prop("disabled", true);
                    // $("#file1").prop("disabled", true);
                    // $("#file1").prop("disabled", true);
                    window.location = baseURL+"/admin/mahasiswalist"
                });

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

loadMahasiswa(nim);
function loadMahasiswa(nim){
    let isObject = {};
    isObject.param = nim;
    // console.log(isObject)

    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: baseURL + '/jsondata/loadmahasiswa',
        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[0];
                // console.log(data)
                // let counter = 1;
                let nama_prodi = data.nama_prodi;
                
                $("#jurusanMhs").html(nama_prodi.toUpperCase());
                $("#nimMhs").html(data.nama);
                $("#namaMhs").html(data.nim);
                $("#laporanupd").val(data.judul_laporan);

                // untuk select mhs utama
                var opts = "<option value='"+data.nim+"'>"+data.nama+"</option>";
                $("#nimselect").append(opts)
                $("#isprodi").html(nama_prodi.toUpperCase())
                
                var url =  baseURL+'/data/profilepicture/'+data.file_foto;
                if(fileExists(url)){
                    $(".improfmhs").attr('src', url);
                } else {
                    $(".improfmhs").attr('src', baseURL+'/data/profilepicture/defaultpics.png');
                }
                
             
            } 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"
                });                        
            }
        }
    });
}