
$("#btnSave").on('click',function(e){
    e.preventDefault();
    labid = $('#lab-id').val();
    url = parseInt(labid) === 0 ? save_lab_url : update_lab_url;
    $.ajax({
        url: url,
        type:"POST",
        data:{
            "_token": csrf_token,
            lab_id : labid,
            lab_name_en: $('#lab-name-en').val(),
            lab_name_kh: $('#lab-name-kh').val(),
            lab_code: $('#lab-code').val(),
            address_en: $('#address-en').val(),
            address_kh: $('#address-kh').val(),
            sample_number: $('input[name="sample_number"]:checked').attr('data-id'),
            patient_code: $('input[name="patient_number"]:checked').attr('data-id'),
            is_auto_make_invoice: $('input[name="is_auto_make_invoice"]:checked').attr('data-id'),
            start_date : $('#start-date').val(),
            end_date : $('#end-date').val(),
            phone_number: $('#phone-number').val(),
            email: $('#email').val(),
            shareable_lab_result: $('input[name="shareable_lab_result"]:checked').attr('data-id'),
            lab_category_id : $('#lab-category-id').val()
        },
        success:function(res){
            handleMessage(res.success, res.message)
            setTimeout(function () {location.reload()}, messageDuration)
        }
    });
});

function delete_lab(labid){
    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_lab_url,
                method:'POST',
                data:{
                    "_token": csrf_token,
                    lab_id : labid,
                },
                success:function(res){
                    handleMessage(res.success, res.message)
                    setTimeout(function () {location.reload()}, messageDuration)
                }
            })

        }
    });
}

function restore_lab(labid){
    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_lab_url,
                method:'POST',
                data:{
                    "_token": csrf_token,
                    lab_id : labid,
                },
                success:function(res){
                    handleMessage(res.success, res.message)
                    setTimeout(function () {location.reload()}, messageDuration)
                }
            })

        }
    });
}

$('#modal-laboratory').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
    $('#shareable_lab_result').prop("checked", false)
    var modal = $(this);
    if(action === "new"){
        // Clear inputs
        modal.find('.modal-body input').val("");
        modal.find('.modal-body input[name="lab_id"]').val(0);
        modal.find('.modal-title').html(modal_add_title);
    }else if(action === "edit"){
        lab_id = button.data('labid');
        modal.find('.modal-body input[name="lab_id"]').val(lab_id);
        modal.find('.modal-title').html(modal_edit_title);
        //e.preventDefault();
        $.ajax({
            url: get_lab_url,
            type:"POST",
            data:{
                "_token": csrf_token,
                lab_id: lab_id,
            },
            success:function(res){
                if(res.success) {
                    let data = res.data;
                    modal.find('.modal-body input[name="lab_name_en"]').val(data.name_en);
                    modal.find('.modal-body input[name="lab_name_kh"]').val(data.name_kh);
                    modal.find('.modal-body input[name="lab_code"]').val(data.short_name);
                    modal.find('.modal-body select[name="lab_category_id"]').val(data.lab_category_id);
                    modal.find('.modal-body input:radio[name="sample_number"]').filter("[data-id=" + data.sample_number + "]").prop('checked', true);
                    modal.find('.modal-body input:radio[name="is_auto_make_invoice"]').filter("[data-id=" + data.is_auto_make_invoice + "]").prop('checked', true);
                    modal.find('.modal-body input[name="address_en"]').val(data.address_en);
                    modal.find('.modal-body input[name="address_kh"]').val(data.address_kh);
                    modal.find('.modal-body input[name="start_date"]').val(getDate(data.start_date));
                    modal.find('.modal-body input[name="end_date"]').val(getDate(data.end_date));
                    modal.find('.modal-body input:radio[name="patient_number"]').filter("[data-id=" + data.patient_code + "]").prop('checked', true);
                    modal.find('.modal-body input:checkbox[name="shareable_lab_result"]').filter("[data-id=" + data.shareable_lab_result + "]").prop('checked', true);
                    modal.find('.modal-body input[name="email"]').val(data.email);
                    modal.find('.modal-body input[name="phone_number"]').val(data.phone_number);
                } else{
                    handleMessage(res.success, res.message)
                }
            },
        });
    }
})

