<script>
    function edit(e){
        $('#tampil_form').load("<?= route('kpi.pengaturan.predikat.form.edit') ?>"+ "/" + $(e).attr('id'), function() {
            $('#modal_form').modal('show');
            $(".title").text("Form Edit Data");
            $(".btn_save").addClass("btn_save predikat btn btn-warning");
            $(".btn_save").append('<i class="fa fa-edit (alias)"></i> ');
            $(".btn_save").append('Perbarui');
            $("#btn_new_input").hide();
        });
    }

    function save(e){
        $('#message').html("");
        var loading = Ladda.create(e);

        if ($("#batas_bawah").val() == "") {
            $('#message').html("<font style='color:Crimson'><i class='fa fa-exclamation-triangle'></i> Poin batas bawah belum diisi</font>");
            $("#batas_bawah").focus();
            return false;
        }else if ($("#batas_atas").val() == "") {
            $('#message').html("<font style='color:Crimson'><i class='fa fa-exclamation-triangle'></i> Poin batas atas belum diisi</font>");
            $("#batas_atas").focus();
            return false;
        }else if ($("#keterangan").val() == "") {
            $('#message').html("<font style='color:Crimson'><i class='fa fa-exclamation-triangle'></i> Keterangan belum diisi</font>");
            $("#keterangan").focus();
            return false;
        }

        loading.start();

        var param = 'id_predikat=' + $("#id_predikat").val() +
            '&batas_bawah=' + $("#batas_bawah").val()+
            '&batas_atas=' + $("#batas_atas").val()+
            '&keterangan=' + $("#keterangan").val();

        $.ajax({
            type: 'POST',
            url: "<?= route('kpi.pengaturan.predikat.save') ?>",
            data: param,
            dataType: 'JSON',
            success: function(result) {
                if (result.success == true) {
                    if (result.type == 'update') {
                        $("#form_input :input").prop("disabled", true);
                        $('.btn_save').hide();
                    }
                    
                    setTimeout(() => {
                        $("#modal_form").modal("hide");
                    }, 2000);
                }
                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><?= $this->lang->line('error') ?></strong></font>');
            }
        });
    }
</script>