
$("#btnSave").on('click',function(e){
    e.preventDefault();
    let container_type_id = $('#container_type_id').val();
    url = parseInt(container_type_id) === 0 ? save_url : update_url;
    $.ajax({
        url: url,
        type:"POST",
        data:{
            "_token": csrf_token,
            container_type_id : container_type_id,
            container_type_concept_id: $('#container_type_concept_id').val(),
            sample_type_concept_id: $('#sample_type_concept_id').val(),
            additive: $('#additive').val(),
            cap_color: $('#cap_color').val(),
            volume_ml: $('#volume_ml').val(),
            storage_temp: $('#storage_temp').val(),
            sterile: $('input[name="sterile"]:checked').attr('data-id'),
        },
        success:function(res){
            handleMessage(res.success, res.message)
            if(res.success){ setTimeout(function () {location.reload()}, messageDuration)}
        }
    });
});

function delete_lab(container_type_id){
    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,
                    container_type_id : container_type_id,
                },
                success:function(res){
                    handleMessage(res.success, res.message)
                    if(res.success) {setTimeout(function () {location.reload()}, messageDuration)}
                }
            })

        }
    });
}

function restore_lab(container_type_id){
    Swal.fire({
        text: confirm_restore_record,
        icon: 'question',
        showCancelButton: true,
        cancelButtonClass:'danger',
        confirmButtonText: confirm_ok_restore,
        cancelButtonText: confirm_cancel_restore,
    }).then((result) => {
        if (result.value) {
            $.ajax({
                url: restore_url,
                method:'POST',
                data:{
                    "_token": csrf_token,
                    container_type_id : container_type_id,
                },
                success:function(res){
                    handleMessage(res.success, res.message)
                    if(res.success) { setTimeout(function () {location.reload()}, messageDuration)}
                }
            })

        }
    });
}

$('#modal-sample-container').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);
    if(action === "new"){
        // Clear inputs
        modal.find('.modal-body input').val("");
        modal.find('.modal-body select[name="container_type_concept_id"]').val("").trigger('change');
        modal.find('.modal-body select[name="sample_type_concept_id"]').val("").trigger('change');
        modal.find('.modal-body input[name="container_type_id"]').val(0);
        modal.find('.modal-body input[name="cap_color"]').val(0);
        modal.find('.modal-title').html(modal_add_title);
    }else if(action === "edit"){
        container_type_id = button.data('container_type_id');
        modal.find('.modal-body input[name="container_type_id"]').val(container_type_id);
        modal.find('.modal-title').html(modal_edit_title);
        //e.preventDefault();
        $.ajax({
            url: get_url,
            type:"POST",
            data:{
                "_token": csrf_token,
                container_type_id: container_type_id,
            },
            success:function(res){
                if(res.success) {
                    let data = res.data;
                    modal.find('.modal-body input[name="additive"]').val(data.additive);
                    modal.find('.modal-body select[name="cap_color"]').val(data.cap_color);
                    modal.find('.modal-body input[name="volume_ml"]').val(data.volume_ml);
                    modal.find('.modal-body input[name="storage_temp"]').val(data.storage_temp);
                    modal.find('.modal-body input:checkbox[name="sterile"]').filter("[data-id=" + data.sterile + "]").prop('checked', true);
                    modal.find('.modal-body select[name="container_type_concept_id"]').val(data.container_type_concept_id).trigger('change');
                    modal.find('.modal-body select[name="sample_type_concept_id"]').val(data.sample_type_concept_id).trigger('change');
                } else{
                    handleMessage(res.success, res.message)
                }
            },
        });
    }
})

