// alert(PHRASE);

loadElementspembimbing(kodeprodi);
function loadElementspembimbing(kodeprodi){
    loaderPage(true)
    let isObject     = {}; 
    isObject.ipoly   = 1;
    isObject.kodeprodi = kodeprodi;

    var dtpr = $('#list_approval').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/listapproval',
            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);
                    
                    var data = response.data.data;
                    
                    console.log(data)
                    return data;
                }else{
                    return response;
                }
               
            }
        },	
        columns: [
            { 'data': 'id_kp', 'sClass':'','sWidth':'10px'},
            { 'data': 'nim', 'sClass':''},
            { 'data': 'nama', 'sClass':''},
            { 'data': 'nama_prodi', 'sClass':''},
            { 'data': 'nama_dosen', 'sClass':''},
            { 'data': 'approve_proses_bimbingan', '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;">';
                    var $rowData = ``;
                    console.log(row.nama_dosen);
                    if(row.nama_dosen == null || row.nama_dosen == ""){
                        $rowData += '<span class="text-center">-</span>';
                    } else {
                        $rowData += row.nama_dosen;
                    }

                    return $rowData;
                },
                visible: true,
                targets: 4,
            },  
            {   
                render: function (data, type, row){
                    // console.log(row);
                    var $rowData = ``;
                    
                    // console.log(row.status_pembimbing)
                    
                    
                    if(row.approve_maju_seminar == 1){

                        $rowData = '<span style="min-width: 70px" class="label label-success">Disetujui</span>';
                    }else{
                        $rowData = '<span style="min-width: 70px" class="label label-warning">Belum Disetujui</span>';
                    }
                    console.log(row)
                    return $rowData;
                    // return $rowData;
                },
                visible: true,
                targets: 5,
            }, 
            {   
                render: function (data, type, row){
                    // console.log(row);
                    var $rowData = ``;

                    if(row.approve_maju_seminar == 1){
                        $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  onclick="switchapproval(true, `+row.id_kp+`)"class=""><a><i class=" icon-file-eye2"></i>Batalkan Approval</a></li>
                                            </ul>
                                        </li>
                                    </ul>`;
                    }else{
                        $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  onclick="switchapproval(false, `+row.id_kp+`)"class=""><a><i class=" icon-file-eye2"></i>Approve</a></li>
                                            </ul>
                                        </li>
                                    </ul>`;
                    }

                    return $rowData;
                },
                visible: true,
                targets: 6,
                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();

        }
    });

}

function switchapproval(isapprove, idkp){
    let isObject = {};

    isObject.idkp       = idkp;
    isObject.isapprove  = isapprove; 

    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: baseURL + '/jsondata/approvalseminarkoor',
        async: false,
        data: {
            iparam	    : cryptoEncrypt(PHRASE, isObject),
        },
        success: function(response){
            // console.log(response.code)
            // $("tbody").empty();
            
            if(response.code == CODE_SUCCESS){
                let result  = response.data;
               
                if(isapprove == false){
                    swal({
                        title: "Alert",
                        text:  " Approval Success!",
                        confirmButtonColor: "#66BB6A",
                        type: "success"
                    }, function(){
                        location.reload();
                    }); 
                } else {
                    swal({
                        title: "Alert",
                        text: " Pembatalan Approval Success!",
                        confirmButtonColor: "#66BB6A",
                        type: "success"
                    }, function(){
                        location.reload();
                    }); 
                }
            


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