
$("#btnSave").on('click',function(e){
    var formData = $('#form-stock-out').serializeArray()
    let data_id = $('#data-id').val();
    let url = (data_id < 1 ? save_url : update_url);
    $.ajax({
        url: url,
        type: "POST",
        data: formData,
        dataType: "json",
        success: function (res) {
            handleMessage(res.success, res.message)
            if(res.success) setTimeout(function () {location.reload()}, messageDuration)
        }
    });
});

function delete_data(uid){
    Swal.fire({
        text: confirm_delete_record,
        icon: 'question',
        showCancelButton: true,
        cancelButtonClass:'danger',
        confirmButtonText: confirm_ok_delete,
        cancelButtonText: confirm_cancel
    }).then((result) => {
        if (result.value) {
            $.ajax({
                url: delete_url,
                method:'POST',
                data:{
                    "_token": csrf_token,
                    uid : uid,
                },
                success:function(res){
                    handleMessage(res.success, res.message)
                    if(res.success) setTimeout(function () {location.reload()}, messageDuration)
                }
            })

        }
    });
}

$('#modal-medicine-release').on('show.bs.modal', function (event) {
    var button = $(event.relatedTarget) // Button that triggered the modal
    var action = button.data('action') // Extract info from data-* attributes
    var modal = $(this);
    $('#release_detail_body').empty();
    if(action === "new"){
        // Clear inputs
        modal.find('.modal-body input[name="uid"]').val(0);
        modal.find('.modal-body .clearable').val("");
        modal.find('.modal-title').html(modal_add_title);
        modal.find('.modal-body select[name="supplier_id"]').val("").trigger("change");
    }else if(action === "edit"){
        uid = button.data('uid');
        modal.find('.modal-body input[name="uid"]').val(uid);
        modal.find('.modal-title').html(modal_edit_title);
        $.ajax({
            url: get_url,
            type:"POST",
            data:{
                "_token": csrf_token,
                uid: uid,
            },
            success:function(res){
                let data = res.data;

                modal.find('.modal-body input[name="gin_number"]').val(data.gin_number);
                modal.find('.modal-body input[name="issue_date"]').val(data.issue_date);
                modal.find('.modal-body #note').val(data.description);
                data.release_detail.map(item=>{
                    addReleaseMedicalItem(item)
                })

                if(!res.success)handleMessage(res.success, res.message)
            }
        });
    }
})
