
$("#btnSave").on('click',function(e){
    e.preventDefault();
    console.log($("input:radio[name=gender]:checked").val())
    data_id = $('#data-id').val();
    let url = parseInt(data_id) === 0 ? save_url : update_url;
    $.ajax({
        url: url,
        type:"POST",
        data :{
            "_token": csrf_token,
            uid : data_id,
            name_en: $('#name-en').val(),
            gender: $("input[name='gender']:checked").attr('data-id'),
            age_from: $('#age-from').val(),
            age_from_unit: $('#age-from-unit').val(),
            age_from_sign: $('#age-from-sign').val(),
            age_to: $('#age-to').val(),
            age_to_unit: $('#age-to-unit').val(),
            age_to_sign: $('#age-to-sign').val()
            //,lab_ids: $('#lab-ids').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: 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)
                }
            })

        }
    });
}

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: restore_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-patient-type').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="lab_ids"]').attr('multiple', true)
        modal.find('.modal-body input[name="uid"]').val(0);
        modal.find('.modal-title').html(modal_add_title);
    }else if(action === "edit"){
        uid = button.data('uid');
        modal.find('.modal-body select[name="lab_ids"]').attr('multiple', false)
        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;
                $("#name-en").trigger('change')
                $("#name-en").empty().trigger("change");
                $('#name-en').select2({tags:true,data: [{id: data.name_en, text:data.name_en, selected: true}]})
                modal.find('.modal-body input[name="age_from"]').val(data.range_start);
                modal.find('.modal-body select[name="age_from_unit"]').val(data.range_start_unit);
                modal.find('.modal-body select[name="age_from_sign"]').val(data.range_start_sign);
                modal.find('.modal-body input[name="age_to"]').val(data.range_end);
                modal.find('.modal-body select[name="age_to_unit"]').val(data.range_end_unit);
                modal.find('.modal-body select[name="age_to_sign"]').val(data.range_end_sign);
                //modal.find('.modal-body select[name="lab_ids"]').val(data.lab_id).trigger('change');
                modal.find('.modal-body input:radio[name="gender"]').filter("[data-id="+data.gender+"]").prop('checked', true);

                if(!res.success) handleMessage(res.success, res.message)
            },
            error: function(response) {
            },
        });
    }
})
