
$("#btnSave").on('click',function(e){
    e.preventDefault();
    let pathogen_id = $('#pathogen_id').val();
    url = parseInt(pathogen_id) === 0 ? save_url : update_url;
    $.ajax({
        url: url,
        type:"POST",
        data:{
            "_token": csrf_token,
            pathogen_id : pathogen_id,
            pathogen_category_concept_id: $('#pathogen_category_concept_id').val(),
            scientific_name: $('#scientific_name').val(),
            common_name: $('#common_name').val(),
            risk_group_concept_id: $('#risk_group_concept_id').val(),
            biosafety_level_concept_id: $('#biosafety_level_concept_id').val(),
            infectious_dose: $('#infectious_dose').val(),
            transmission_mode: $('#transmission_mode').val(),
            storage_requirement: $('#storage_requirement').val(),
            min_temp: $('#min_temp').val(),
            max_temp: $('#max_temp').val(),
        },
        success:function(res){
            handleMessage(res.success, res.message)
            if(res.success){ setTimeout(function () {location.reload()}, messageDuration)}
        }
    });
});

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

        }
    });
}

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

        }
    });
}

$('#modal-pathogen').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 input[name="pathogen_id"]').val(0);
        modal.find('.modal-body select[name="pathogen_category_concept_id"]').val("").trigger('change');
        modal.find('.modal-body select[name="risk_group_concept_id"]').val("").trigger('change');
        modal.find('.modal-body select[name="biosafety_level_concept_id"]').val("").trigger('change');
        modal.find('.modal-title').html(modal_add_title);
    }else if(action === "edit"){
        pathogen_id = button.data('pathogen_id');
        modal.find('.modal-body input[name="pathogen_id"]').val(pathogen_id);
        modal.find('.modal-title').html(modal_edit_title);
        //e.preventDefault();
        $.ajax({
            url: get_url,
            type:"POST",
            data:{
                "_token": csrf_token,
                pathogen_id: pathogen_id,
            },
            success:function(res){
                if(res.success) {
                    let data = res.data;
                    modal.find('.modal-body input[name="scientific_name"]').val(data.scientific_name);
                    modal.find('.modal-body input[name="common_name"]').val(data.common_name);
                    modal.find('.modal-body input[name="infectious_dose"]').val(data.infectious_dose);
                    modal.find('.modal-body input[name="transmission_mode"]').val(data.transmission_mode);
                    modal.find('.modal-body input[name="storage_requirement"]').val(data.storage_requirement);
                    modal.find('.modal-body input[name="min_temp"]').val(data.min_temp);
                    modal.find('.modal-body input[name="max_temp"]').val(data.max_temp);
                    modal.find('.modal-body select[name="risk_group_concept_id"]').val(data.risk_group_concept_id).trigger('change');
                    modal.find('.modal-body select[name="pathogen_category_concept_id"]').val(data.pathogen_category_concept_id).trigger('change');
                    modal.find('.modal-body select[name="biosafety_level_concept_id"]').val(data.biosafety_level_concept_id).trigger('change');
                } else{
                    handleMessage(res.success, res.message)
                }
            },
        });
    }
})

