<script type="text/javascript">
    function tambah(e){
        $('#tampil_form').load("<?= route("sales_activity.pengaturan.admin.sales.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('<?= $this->lang->line('button_form_simpan') ?>');
            $('.btn_save').attr('onClick','simpan(this)');
            $("#btn_new_input").hide();
        });
    }

    function edit(e){
        $('#tampil_form').load("<?= route('sales_activity.pengaturan.admin.sales.form.edit') ?>"+ "/" + $(e).attr('id'), function() {
            $('#modal_form').modal('show');
            $(".title").text("<?= $this->lang->line('title_form_edit_data') ?>");
            $(".btn_save").append('<i class="fa fa-update"></i> ');
            $(".btn_save").append("<?= $this->lang->line('button_form_perbarui') ?>");
            $(".btn_save").removeClass("btn_save").addClass("btn_update btn btn-warning");
            $('.btn_update').attr('onClick','simpan(this)');
            $("#btn_new_input").hide();
        });
    }

    function hapus_form(e){
        $('#tampil_form').load("<?= route('sales_activity.pengaturan.admin.sales.form.hapus') ?>"+ "/" + $(e).attr('id'), function() {
            $('#modal_form').modal('show');
            $(".title").text("<?= $this->lang->line('title_form_hapus_data') ?>");
            $("#form_input #id_karyawan").prop("disabled", true);
            $("#form_input #id_group_m").prop("disabled", true);
            $(".btn_save").append('<i class="fa fa-trash"></i> ');
            $(".btn_save").append('<?= $this->lang->line('button_form_hapus') ?>');
            $(".btn_save").removeClass("btn_save").addClass("btn_hapus btn btn-danger");
            $('.btn_hapus').attr('onClick','hapus_action(this)');
        });
    }

    function new_input(e){
        $("#form_input :input").prop("disabled", false);
        $("#form_input #id_karyawan").select2("val", "0");    
        $("#form_input #id_group_m").val(null).trigger("change"); 
        $('.btn_save').show();
        $('#btn_new_input').hide();
        $('#message').html('');
    }

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

        if ($("#form_input #id_karyawan").val() == '' || $("#form_input #id_karyawan").val() == '0') {
            $('#message').html("<font style='color:Crimson'><i class='fa fa-exclamation-triangle'></i> <?= $this->lang->line('error_pilih_karyawan') ?></font>");
            $("#form_input #id_karyawan").focus();
            return false;
        }

        if ($("#form_input #id_group_m").val() == '') {
            $('#message').html("<font style='color:Crimson'><i class='fa fa-exclamation-triangle'></i> Silahkan pilih group/area</font>");
            $("#form_input #id_group_m").focus();
            return false;
        }

        loading.start();
        var param             = {};
            param.id          = $("#form_input #id").val();
            param.id_karyawan = $("#form_input #id_karyawan").val();
            param.id_group_m  = JSON.stringify($("#id_group_m").val());

        $.ajax({
            type: 'POST',
            url: "<?= route('sales_activity.pengaturan.admin.sales.save') ?>",
            data: param,
            dataType: 'JSON',
            success: function(result) {
                if (result.success == true) {
                    if (result.type == 'save') {
                        $("#form_input :input").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><?= $this->lang->line('error') ?></strong></font>');
            }
        });
    }

    function hapus_action(e){
        var loading = Ladda.create(e);
        loading.start();

        var param = {};
        param.id = $("#id").val();

        $.ajax({
            type: 'POST',
            url: "<?= route('sales_activity.pengaturan.admin.sales.delete') ?>",
            data: param,
            dataType: 'JSON',
            success: function(result) {
                $("#modal_form .card-body").hide();
                $("#modal_form .card-footer").hide();
                $("#modal_form .title").html(result.message.replace("009900", "FFFFFF"));
            },
            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><?= $this->lang->line('error') ?></strong></font>');
            }
        });
    }
</script>