<script type="text/javascript">
    // ACTION EDIT
    function edit(e){
        var actionDiv = $(e).closest('.div_action');
        actionDiv.find('.btn_edit').hide();
        actionDiv.find('.btn_save').show();
        actionDiv.find('.btn_batal').show();
        
        var id = $(e).attr('id');
        var jumlahPoinDiv = $('div#' + id).closest('.jumlah_poin_div');
        jumlahPoinDiv.find('.jumlah_poin_value').hide();
        jumlahPoinDiv.find('.jumlah_poin_edit').show();

        $('.btn_edit').prop("disabled", true).attr("onclick", null);
    }

    // ACTION BATAL
    function batal(e){
        var actionDiv = $(e).closest('.div_action');
        actionDiv.find('.btn_edit').show();
        actionDiv.find('.btn_save').hide();
        actionDiv.find('.btn_batal').hide();
        
        var id = $(e).attr('id');
        var jumlahPoinDiv = $('div#' + id).closest('.jumlah_poin_div');
        jumlahPoinDiv.find('.jumlah_poin_value').show();
        jumlahPoinDiv.find('.jumlah_poin_edit').hide();

        $('.btn_edit').prop("disabled", false).attr("onclick", "edit(this)");
    }

    // ACTION SIMPAN
    function simpan(e){
        var id              = $(e).attr('id');
        var status          = $(e).attr('data-status');
        var id_karyawan     = $(e).attr('data-id-karyawan');
        var jumlahPoinDiv   = $('div#' + id).closest('.jumlah_poin_div');
        var jumlahPoinInput = jumlahPoinDiv.find('#jumlah_poin_input').val();

        var param             = {};
            param.id          = id;
            param.status      = status;
            param.id_karyawan = id_karyawan;
            param.jumlahPoin  = jumlahPoinInput;

        $.ajax({
            type: 'POST',
            url: "<?= route('sales_activity.master.target.poin.save') ?>",
            data: param,
            dataType: 'JSON',
            success: function(result) {
                showMsg(result.message);
            },
            error: function() {
                showMsg("<?= $this->lang->line('error') ?>");
            }
        });
    }
</script>