
$("#btnSave").on('click',function(e){
    e.preventDefault();
    data_id = $('#data-id').val();
    let url = base_comment_url.toString() + (data_id < 1 ? '.save' : '.update').toString();
    $.ajax({
        url: url,
        type:"POST",
        data :{
            "_token": csrf_token,
            uid : data_id,
            comment_desc: $('#comment-desc').val(),
            sample_type_id: $('#sample-type-id').val()
        },
        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: base_comment_url.toString() + '.delete',
                method:'POST',
                data:{
                    "_token": csrf_token,
                    uid : uid,
                },
                success:function(res){
                    handleMessage(res.success, res.message)
                    if(res.success) setTimeout(function () {location.reload()}, messageDuration)
                }
            })

        }
    });
}

function restore_data(uid){
    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: base_comment_url.toString() + '.restore',
                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-comment').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);
    $('#comment-desc').val("");
    if(action === "new"){
        // Clear inputs
        modal.find('.modal-body input[name="uid"]').val(0);
        modal.find('.modal-body input').val("");
        modal.find('.modal-title').html(modal_add_title);
        modal.find('.modal-body select[name="sample_type_id"]').attr('multiple', true)
    }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);
        modal.find('.modal-body select[name="sample_type_id"]').attr('multiple', false)
        $.ajax({
            url: base_comment_url.toString() + '.get',
            type:"POST",
            data:{
                "_token": csrf_token,
                uid: uid,
            },
            success:function(res){
                let data = res.data;
                modal.find('.modal-body select[name="department_id"]').val(data.sample_type.department_id).trigger('change');
                getSampleTypeByDepartmentId(data.sample_type.department_id, data.sample_type_id)
                //modal.find('.modal-body input[name="comment_desc"]').val(data.comment_desc);
                $('#comment-desc').val(data.comment_desc)
                if(!res.success) handleMessage(res.success, res.message)
            }
        });
    }
})

$('#department-id').change(function () {
    var departmentId = $(this).val()
    getSampleTypeByDepartmentId(departmentId);
})

function getSampleTypeByDepartmentId(departmentId, selected_sample_type_id = 0) {
    $.ajax({
        url: get_refs_base_url+'/sample-type/department/'+departmentId,
        type:"GET",
        dataType:"JSON",
        success:function(response){
            $('#sample-type-id').html('');
            $('#sample-type-id').append('<option></option>');
            response.data.map(item =>{
                var patientOption = document.createElement('OPTION');
                patientOption.setAttribute('value',item.id);
                if(selected_sample_type_id == item.id){
                    patientOption.setAttribute('selected','selected');
                }
                patientOption.text = item.name_en;
                $('#sample-type-id').append(patientOption);
            })
            if(!response.success) handleMessage(res.success, res.message)
        },
        error: function(response) {
        },
    });
}
