<script type="text/javascript">
    $(document).ready(function () {
        list_data = $('#list_data').DataTable({
                    'searching'   : true,
                    'paging'      : true,
                    'lengthChange': true,
                    'ordering'    : true,
                    'info'        : true,
                    'scrollX'     : true,
                    // 'scrollCollapse': true,
                    // 'fixedColumns': true,
                    'language'    :{
                                    'url'         : '<?=base_url("assets/plugins/datatables/dataTables-language-id.json")?>',
                                    'sEmptyTable' : 'Tidak ada data untuk ditampilkan'
                                },
                    "processing"  : true,
                    "serverSide"  : true,
                    "order"       : [],

                    "ajax"        : {
                                    "url": "<?= route('master.jenisplafon.listdata') ?>",
                                    "type": "GET",
                                    "data": {
                                                id_cabang : function() {
                                                    return $(".filter #id_cabang").val()
                                                },
                                            },
                                    "complete" : function(data){
                                            reconfigure(data);
                                        }
                                    },

                    "aoColumnDefs": [
                                        { "aTargets": [0], "bSortable": false },

                                        <?php
                                            if(in_array($this->level_user, array("1"))) {
                                        ?>
                                                { "aTargets": [-1], "bSortable": false }

                                        <?php } ?>
                                    ],
                    "fnRowCallback": function( nRow, aData, iDisplayIndex, iDisplayIndexFull ) {
                            $('td:eq(0)', nRow).css({'text-align':'center','width':'10px'});
                            $('td:eq(1)', nRow).css({'text-align':'left','width':'300px'});
                            $('td:eq(2)', nRow).css({'text-align':'center','width':'100px'});
                            $('td:eq(3)', nRow).css({'text-align':'left'});
                            $('td:eq(4)', nRow).css({'text-align':'right'});

                            <?php
                                if(in_array($this->level_user, array("1"))) {
                            ?>

                                    $('td:eq(5)', nRow).css({'text-align':'center','width':'100px'});

                            <?php } ?>
                    }

        });

        $(document).on('click','.filter #btn_filter',function(){
            list_data.ajax.reload();
        });


        $('.filter #id_cabang').select2({
            placeholder: 'Semua cabang',
            ajax: {
                // type: 'POST',
                url: '<?= route("dropdown.cabang") ?>',
                dataType: 'json',
                delay: 250,
                processResults: function (data) {
                return {
                        results: data
                    };
                },
                cache: true
            },
            minLength: 3,
        });


        $(document).on('click','.btn_tambah',function(){
            $('#tampil_form').load("<?= route('master.jenisplafon.form.tambah') ?>",function(){
                $('#modal_form').modal('show');
                $(".title").text("Form Tambah Data");
                $(".btn_save").addClass("btn_save btn btn-primary");
                $(".btn_save").append('<i class="fa fa-save"></i> ');
                $(".btn_save").append('SIMPAN');
                $("#btn_new_input").hide();
            });
        });

        $(document).on('click','.btn_edit',function(){
            $('#tampil_form').load("<?= route('master.jenisplafon.form.edit') ?>"+"/"+$(this).attr('id'),function(){
                $('#modal_form').modal('show');
                $(".title").text("Form Edit Data");
                $(".btn_save").addClass("btn_save btn btn-warning");
                $(".btn_save").append('<i class="fa fa-edit (alias)"></i> ');
                $(".btn_save").append('PERBARUI');
                $("#btn_new_input").hide();
            });
        });

        $(document).on('click','.btn_delete',function(){
            $('#tampil_form').load("<?= route('master.jenisplafon.form.hapus') ?>"+"/"+$(this).attr('id'),function(){
                $('#modal_form').modal('show');
                $(".title").text("Apakah Anda ingin menghapus data ini?");
                $('#form_input input[type=text]').prop("disabled",true);
                $("#id_cabang").prop("disabled", true);
                $(".btn_save").append('<i class="fa fa-trash"></i> ');
                $(".btn_save").append('HAPUS');
                $(".btn_save").removeClass("btn_save").addClass("btn_hapus btn btn-danger");
            });
        });

        $(document).on('click','#btn_new_input', function(){
            $('#form_input input[type=text]').prop("disabled",false);
            $("#form_input input[type=text]").val('');
            $('#form_input #id_cabang').prop('disabled', false);
            $("#form_input #id_cabang").select2("val","0");

            $('.btn_save').show();
            $('#btn_new_input').hide();
            $('#message').hide();
        });

        $(document).on("click",".btn_save",function(e){
            e.preventDefault();
            $('#message').html("");

            var loading = Ladda.create(this);

            if($("#id_cabang").val()=="0"){
                $('#message').html("<font style='color:Crimson'><i class='fa fa-exclamation-triangle'></i> Cabang belum diisi</font>");
                $("#id_cabang").focus();
                return false;
            }else if($("#kd_plafon").val() ==""){
                $('#message').html("<font style='color:Crimson'><i class='fa fa-exclamation-triangle'></i> Kode Plafon belum diiisi</font>");
                $('#kd_plafon').focus();
                return false;
            }else if($("#jenis_plafon").val() ==""){
                $('#message').html("<font style='color:Crimson'><i class='fa fa-exclamation-triangle'></i> Jenis Plafon belum diiisi</font>");
                $('#jenis_plafon').focus();
                return false;
            }else if($("#max_nominal_plafon").val() ==""){
                $('#message').html("<font style='color:Crimson'><i class='fa fa-exclamation-triangle'></i> Maksimal nominal plafon belum diiisi</font>");
                $('#max_nominal_plafon').focus();
                return false;
            }else if($("#max_nominal_plafon").val() =="0"){
                $('#message').html("<font style='color:Crimson'><i class='fa fa-exclamation-triangle'></i> Maksimal nominal tidak boleh nol</font>");
                $('#max_nominal_plafon').focus();
                return false;
            }

            loading.start();

            var param = 'cabang='+$("#id_cabang").val()+
                        '&kd_plafon='+$("#kd_plafon").val()+
                        '&jenis_plafon='+$("#jenis_plafon").val()+
                        '&id_jenis_plafon='+$("#id_jenis_plafon").val()+
                        '&max_nominal_plafon='+$("#max_nominal_plafon").val();

            $.ajax({
                type: 'POST',
                url: "<?= route('master.jenisplafon.save') ?>",
                data: param,
                dataType:'JSON',
                success: function(result) {
                    if(result.success == true){
                        if(result.type == 'save'){
                            $("#form_input input[type=text]").prop("disabled", true);
                            $("#form_input #id_cabang").prop("disabled", true);
                            $('.btn_save').hide();
                            $('#btn_new_input').show();
                        }
                    }
                    loading.stop();
                    $('#message').show();
                    $('#message').html(result.message);
                },
                error:function(){
                    loading.stop();
                    $('#message').show();
                    $('#message').html('<font color="#eb3a28"><i class="fa fa-close(alias)">&nbsp;</i><strong>Gagal : Terjadi Kesalahan</strong></font>');
                }
            });
        });

        // PROSES DELETE
        $(document).on('click','.btn_hapus',function(){
            var loading = Ladda.create(this);
            loading.start();
            $.ajax({
                type: 'POST',
                url: "<?= route('master.jenisplafon.delete') ?>",
                data: 'id_jenis_plafon='+$("#id_jenis_plafon").val(),
                dataType:'JSON',
                success: function(result){
                    if(result.success == true){
                        $("#modal_form .card-body").hide();
                        $("#modal_form .card-footer").hide();
                        $("#modal_form .title").html(result.message);
                    }
                },
                error:function(){
                    loading.stop();
                    $("#modal_form .card-body").hide();
                    $("#modal_form .card-footer").hide();
                    $("#modal_form .title").html('<font color="#eb3a28"><i class="fa fa-close(alias)">&nbsp;</i><strong>Gagal : Terjadi Kesalahan</strong></font>');
                }
            });
        });
    });
</script>
<div class="content-header">
    <div class="row align-items-center">
        <div class="content-header-left col-md-6 col-12 mb-md-0 mb-1">
            <h3 class="content-header-title"><?= $judul ?></h3>
            <div class="row breadcrumbs-top">
                <div class="breadcrumb-wrapper col-12">
                    <ol class="breadcrumb">
                        </li>
                        <li class="breadcrumb-item"><a href="#">Master Data</a>
                        </li>
                        <li class="breadcrumb-item active"><?= $judul ?>
                        </li>
                    </ol>
                </div>
            </div>
        </div>
        <div class="content-header-right col-md-6 col-12">
            <?php
                if(in_array($this->level_user, array("1"))){
            ?>
                    <div class="float-md-right">
                        <button type="button" class="btn_tambah btn btn-biru">
                            <i class="fa fa-plus"></i> Tambah Data
                        </button>
                    </div>
            <?php
                }
            ?>
        </div>
    </div>
</div>

<div class="card card-rounded">
    <div class="card-content">
        <div class="card-body">
            <div class="row filter">
                <?php
                    if(in_array($this->level_user, array("1"))) {
                ?>
                        <div class="col-md-3">
                            <select id="id_cabang" class="form-control select2">
                                <option value="0">Semua Cabang</option>
                            </select>
                        </div>
                        <div class="col-md-3">
                            <button type="button" id="btn_filter" class="btn btn-success">TAMPILKAN</button>
                        </div>

                <?php
                    }
                ?>
            </div>

            <div class="row mt-2">
                <div class="col-md-12 table-responsive">
                    <table id="list_data" class="table table-striped" style="width:100%">
                        <thead>
                            <tr>
                                <th class="text-center">NO</th>
                                <th class="text-left">CABANG</th>
                                <th class="text-center">KODE PLAFON</th>
                                <th class="text-left">JENIS PLAFON</th>
                                <th class="text-right">MAKSIMAL NOMINAL PLAFON</th>
                                <?php
                                    if(in_array($this->level_user, array("1"))) {
                                ?>
                                        <th class="text-center">AKSI</th>
                                <?php
                                    }
                                ?>
                            </tr>
                        </thead>
                        <tbody>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- START TAMPIL MODAL -->
<div id="modal_form" class="modal fade in">
    <div class="modal-dialog modal-md">
        <div id="tampil_form" class="modal-content"></div>
    </div>
</div>
<!-- END TAMPIL MODAL -->


