
$("#btnSave").on('click',function(e){
    e.preventDefault();
    let room_id = $('#room_id').val();
    url = parseInt(room_id) === 0 ? save_url : update_url;
    $.ajax({
        url: url,
        type:"POST",
        data:{
            "_token": csrf_token,
            room_id : room_id,
            name_en: $('#name-en').val(),
            name_kh: $('#name-kh').val(),
            code: $('#code').val(),
            building_id: $('#building_id').val(),
            biosafety_level_concept_id: $('#biosafety_level_concept_id').val(),
            temperature_range: $('#temperature_range').val(),
            description: $('#description').val(),
        },
        success:function(res){
            handleMessage(res.success, res.message)
            if(res.success){ setTimeout(function () {location.reload()}, messageDuration)}
        }
    });
});

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

        }
    });
}

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

        }
    });
}

$('#modal-room').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 textarea').val("");
        modal.find('.modal-body select[name="building_id"]').val("").trigger('change');
        modal.find('.modal-body select[name="biosafety_level_concept_id"]').val("").trigger('change');
        modal.find('.modal-body input[name="room_id"]').val(0);
        modal.find('.modal-title').html(modal_add_title);
    }else if(action === "edit"){
        room_id = button.data('room_id');
        modal.find('.modal-body input[name="room_id"]').val(room_id);
        modal.find('.modal-title').html(modal_edit_title);
        //e.preventDefault();
        $.ajax({
            url: get_url,
            type:"POST",
            data:{
                "_token": csrf_token,
                room_id: room_id,
            },
            success:function(res){
                if(res.success) {
                    let data = res.data;
                    modal.find('.modal-body input[name="name_en"]').val(data.name_en);
                    modal.find('.modal-body input[name="name_kh"]').val(data.name_kh);
                    modal.find('.modal-body input[name="code"]').val(data.code);
                    modal.find('.modal-body input[name="temperature_range"]').val(data.temperature_range);
                    modal.find('.modal-body textarea[name="description"]').val(data.description);
                    modal.find('.modal-body select[name="building_id"]').val(data.building_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)
                }
            },
        });
    }
})

