<div class="row align-items-center mt-1">
    <div class="content-header-left col-md-6 mb-md-0 mb-1">
        <h5 class="dark-blue-color"><?= $judul ?></h5>
    </div>
</div>
<hr>

<div id="view_data" class="col-md-12 bbna"></div>

<script>
    $(document).ready(function () {
        list_data();
    });

    function edit(e){
        var id = $(e).attr('id');
        $.each($('#form_'+id+' input[type="checkbox"]'), function(index,value){
            var i = $(this).attr("data-i");
            var o = $(this).attr("data-o");
            if (i == 1) {
                $(this).prop("disabled", false);
            }else if(i == 2 && o != 1){
                $(this).prop("disabled", false);
            }else if(i == 3 && (o != 1 && o != 2)){
                $(this).prop("disabled", false);
            }else if (i == 4 && (o != 1 && o != 2 && o != 3)) {
                $(this).prop("disabled", false);
            }
        });
        $("#"+ id).hide();
        $("#btn_save_"+id).show();
        $("#btn_batal_"+id).show();
        $("#"+ id).focus();

        $("#btn_batal_" + id).click(function() {
            document.getElementById("btn_save_"+id).disabled = true;
            document.getElementById("btn_batal_"+id).disabled = true;
            list_data();
            $("#"+ id).focus();
        });

        $("#btn_save_" + id).click(function(e) {
            e.preventDefault();
            var loading = Ladda.create(document.getElementById("btn_save_"+id));
            loading.start();

            var form = document.getElementById("form_" + id);
            var formData = new FormData(form);

            $.ajax({
                url: "<?= route('pengaturan.reimbursement.save') ?>",
                type: "POST",
                data: formData,
                contentType: false,
                cache: false,
                dataType: 'json',
                processData: false,
                success: function(result) {
                    if(result.success == true){
                        if (result.type == 'update') {
                            loading.stop();
                            $('#message_'+id).show();
                            $('#message_'+id).html(result.message);
                            list_data();
                            $("#"+ id).focus();
                        } else {
                            loading.stop();
                            $('#message_'+id).show();
                            $('#message_'+id).html(result.message);
                            list_data();
                            $("#"+ id).focus();
                        }
                    }else{
                        loading.stop();
                        $('#message_'+id).show();
                        $('#message_'+id).html(result.message);
                    }                  
                },
                error: function() {
                    loading.stop();
                    $('#message_'+id).show();
                    $('#message_'+id).html('<font color="#eb3a28"><i class="fa fa-close(alias)">&nbsp;</i><strong><?= $this->lang->line('error'); ?></strong></font>');
                }
            });
        });
    }

    function list_data() {
        load_url = "<?= route('pengaturan.reimbursement.data') ?>";
        return $.get(load_url, function(data) {
            $("#view_data").html(data);
        });
    }
</script>