// $('.fullcalendar-languages').fullCalendar({
//     header: {
//         left: 'prev,next today',
//         center: 'title',
//         right: 'month,listMonth'
//     },
//     defaultDate: Date.now(),
//     locale: 'in',
//     buttonIcons: false, // show the prev/next text
//     weekNumbers: true,
//     editable: false,
//     events: null,
// });

loadElementsMhs()
function loadElementsMhs(clicksc){
    loaderPage(true)
    let isObject     = {}; 
    isObject.ipoly   = 1;

    var dtpr = $('#jadwallist').DataTable({
        serverSide	: true,
        destroy		: true,
        bFilter		: true,
        responsive	: true,
        searching   : true,
        pagingType  : 'full',
        lengthMenu  : [[1, 25, 50, -1], [1, 25, 50, "All"]],
        pageLength  : 25,
        ajax		: {
            url: baseURL + '/jsondata/jadwallist',
            type: 'POST',
            // async: false,
            data : function (d) {         
                return $.extend( {}, d, {
                    iparam	    : cryptoEncrypt(PHRASE, isObject),
                });
            },
            dataSrc: function(response) {
                
                if(response.code == CODE_SUCCESS){
                    response = cryptoDecrypt(PHRASE, response.data);
                    // console.log(response.data.data);
                    var data = response.data.data;
                    // var datas;
                    // var dataada=[];
                    // // datas.push(data[0])
                    // // console.log(datas);
                    // for(i in data){
                    //     datas = {
                    //         "id_mhs": data[i].id_mhs,
                    //         "nim": data[i].nim,
                    //         "nama": data[i].nama,
                    //         "jurusan": data[i].jurusan,
                    //         "jeniskelamin": data[i].jeniskelamin,
                    //         "fakultas": data[i].fakultas,
                    //         "create_date": data[i].create_date,
                    //         // "status": statusberkas(data[i].nim)
                    //     }
                    //     dataada.push(datas)
                    // }
                    console.log(data)
                    callendars(data);
                    return data;
                }else{
                    return response;
                }
               
            }
        },	
        columns: [
            { 'data': 'nim', 'sClass':'','sWidth':'10px'},
            { 'data': 'nama', 'sClass':''},
            // { 'data': 'jurusan', 'sClass':''},
            { 'data': 'namapenguji1', 'sClass':''},
            { 'data': 'namapenguji2', 'sClass':''},
            { 'data': 'dosen_pembimbing', 'sClass':''},
            { 'data': 'tanggal', 'sClass':''},
            { 'data': 'jam', 'sClass':''},
            { 'data': 'semester', 'sClass':''},
            { 'data': 'ruang', 'sClass':'text-center'},
            { 'data': '', 'sClass':'text-center','sWidth':'10px'}
        ],
        // buttons: {            
        //     buttons: [
        //         {
        //             extend: 'colvis',
        //             className: 'btn btn-default'
        //         },
        //     ]
        // },
        processing: true,
        stateSave: false,
        autoWidth: false,
        dom: '<"datatable-header"fl><"datatable-scroll-wrap"t><"datatable-footer"ip>',
        language: {
            search: '<span>Filter:</span> _INPUT_',
            lengthMenu: '<span>Show:</span> _MENU_',
            processing: "<div class='table-loading'><img src='../../img/loadingtwo.gif' /></div>",

            paginate: { 'first': 'First', 'last': 'Last', 'next': '&rarr;', 'previous': '&larr;' }
        },
        columnDefs: [
            {
                // width: '20px',
                // targets: 0,
                // visible: true
            },
            {   


                // render: function (data, type, row){
                //     // var $rowData = '<img src="/distro/assets/images/placeholder.jpg" alt="" class="img-rounded img-preview showAtt" style="cursor: pointer;">';
                //     let isIcon = ' icon-file-download2';

                //     var $rowData = '<div class="thumb img-rounded img-preview showAtt" style="width: 70px;">'+
                //         '<img src="/distro/assets/images/placeholder.jpg" alt="" class="img-rounded img-preview showAtt">'+
                //         '<div class="caption-overflow">'+
                //             '<span>'+
                //                 '<p  class="btn showAtt bg-success-400 btn-icon btn-lg" style="font-variant: all-petite-caps;background-color: #0e0e0e33 !important;border-color: #25252591 !important;">'+
                //                     '<i class="'+isIcon+'"></i> '
                //                 '</p>'+
                //             '</span>'+
                //         '</div>'+
                //     '</div>';

                //     return $rowData;
                // },
                // visible: true,
                // targets: 1,
            },  
            {   
                render: function (data, type, row){
                    // console.log(row);
                    var $rowData = ``;
                    var sem      = '';
                    if(row.semester == 1){
                        sem = "GANJIL";
                    }else{
                        sem = "GENAP"
                    }

                    $rowData = sem+" "+row.tahun_ajaran;
                    
                    return $rowData;
                },
                visible: true,
                targets: 7,
            }, 
            {   
                render: function (data, type, row){
                    var $rowData = '';
                    
                    $rowData = `<ul class="icons-list">
                                         <li class="dropdown">
                                             <a href="#" class="dropdown-toggle" data-toggle="dropdown">
                                                 <i class="icon-menu9"></i>
                                             </a>
                                             <ul class="dropdown-menu dropdown-menu-right">
                                                <li class=""><a href="`+baseURL+`/admin/updatejadwal?nim=`+encodeURIComponent(btoa(row.nim))+`"class=""><i class=" icon-users"></i> Update Jadwal</a></li>
                                                <li><a href="/data/pengumuman/`+row.file_jadwal+`" download><i class="icon-file-download2"></i> Download jadwal</a></li>
                                                
                                            </ul>

                                             
                                         </li>
                                     </ul>`

                    return $rowData;
                },
                visible: true,
                targets: 9,
                className: 'dt-center'
            }, 
        ],
        fnRowCallback: function(nRow, aData, iDisplayIndex, iDisplayIndexFull){ 
            var index = iDisplayIndexFull + 1; 
            $('td:eq(0)', nRow).html('#'+index); 
            return  index;
        },
        drawCallback: function (settings) {
            // console.log(settings.json);
            
            var api  = this.api();
            var rows = api.rows({page:'current'}).nodes();
            var last = null;

            // console.log(rows)
            // Reverse last 3 dropdowns orientation
            $(this).find('tbody tr').slice(-3).find('.dropdown, .btn-group').addClass('dropup');

            var that = this;
            var td ;
            var tr ;

            this.$('td').click( function () {
                td = this;
            });
            this.$('tr').click( function () {
                tr = this;
            });

            this.$('.showAtt').on('click', function(){

                var tr      = $(this).parents('tr')
                var aData   = dtpr.row( tr ).data();

                // loadAttachments(aData);

            });

            
        },
        fnInitComplete: function (oSettings, json) {
            var that = this;
            var td ;
            var tr ;

            this.$('td').click( function () {
                td = this;
            });
            this.$('tr').click( function () {
                tr = this;
            });

            

            $('#mahasiswalist input').bind('keyup', function (e) {
                return this.value;
            });

            setIFrameSize();

        }
    });

}

callendars();
function callendars(){

    let isObject = {};
    isObject.param = iduser;
    // console.log(isObject)

    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: baseURL + '/jsondata/loadcalendar',
        // 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;
                let counter = 1;
                console.log(data);
                var eventsCall = [];
                for(i in data){
                    var datajam = addMinutes(data[i].jam, 45);
                    var addhours = new Date(data[i].tanggal);
                    console.log(datajam);
                    eventsCall.push({
                        title: data[i].nim_mhs,
                        start: data[i].tanggal+"T"+data[i].jam,
                        // end: data[i].tanggal+"T"+addHours(data[i].jam)
                    });
                   
                }
            
                
                // var idate = idate.split("-");
                // idate = new Date(idate[2], idate[1] - 1, idate[0]).getTime();

                
                // $('.fullcalendar-languages').empty();
                console.log(eventsCall);
                var initialLocaleCode = 'in';
                var date = Date.now();
                $('.fullcalendar-languages').fullCalendar({
                    header: {
                        left: 'prev,next today',
                        center: 'title',
                        right: 'month,listMonth'
                    },
                    defaultDate: date,
                    locale: initialLocaleCode,
                    buttonIcons: false, // show the prev/next text
                    weekNumbers: true,
                    editable: false,
                    events: eventsCall,
                });
                
            } 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"
                });                        
            }
        }
    });

    // We're using Select2 for language select
    $('.select').select2({
        width: 100,
        minimumResultsForSearch: Infinity,
        containerCssClass: 'bg-slate-700',
        dropdownCssClass: 'bg-slate-700'
    });
}   

function addMinutes(time, minsToAdd) {

    function D(J){ return (J<10? '0':'') + J;};
    var piece = time.split(':');
    var mins = piece[0]*60 +piece[1] +minsToAdd + piece;
    console.log(mins);
    return D(mins%(24*60)/60 | 0) + ':' + D(mins%60);  
}

function addHours(time){
    var piece = time.split(':');
    var hours = parseInt(piece[0]);
    var inthours = hours + 1;
    // console.log(inthours);
    return (parseInt(piece[0])+1)+ ":" + piece[1] + ":" + piece[2];
}

// alert(addingwife('00:30:00', '40'))
// function addingwife(hours, minutes){
//     var splithours = hours.split(':')
//     var date = new Date();
//     date.setHours(splithours[0]);
//     date.setMinutes(parseInt(splithours[1]*60000 + parseInt(minutes)*60000));

//     return date.getHours()+':'+date.getMinutes();
// }
// console.log(addHours('14:00:00'))  // '18:35'
  


