
$("#btnSave").on('click',function(e){
    e.preventDefault();
    let equipment_id = $('#equipment_id').val();
    url = parseInt(equipment_id) === 0 ? save_url : update_url;
    $.ajax({
        url: url,
        type:"POST",
        data:{
            "_token": csrf_token,
            equipment_id : equipment_id,
            room_id: $('#room_id').val(),
            name_en: $('#name-en').val(),
            capacity: $('#capacity').val(),
            code: $('#code').val(),
            serial_number: $('#serial_number').val(),
            equipment_type_concept_id: $('#equipment_type_concept_id').val(),
            model: $('#model').val(),
            installed_date: $('#installed_date').val(),
            min_temperature: $('#min_temperature').val(),
            max_temperature: $('#max_temperature').val(),
        },
        success:function(res){
            handleMessage(res.success, res.message)
            if(res.success){ setTimeout(function () {location.reload()}, messageDuration)}
        }
    });
});

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

        }
    });
}

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

        }
    });
}

$('#modal-equipment').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="room_id"]').val("").trigger('change');
        modal.find('.modal-body select[name="equipment_type_concept_id"]').val("").trigger('change');
        modal.find('.modal-body input[name="equipment_id"]').val(0);
        modal.find('.modal-title').html(modal_add_title);
    }else if(action === "edit"){
        equipment_id = button.data('equipment_id');
        modal.find('.modal-body input[name="equipment_id"]').val(equipment_id);
        modal.find('.modal-title').html(modal_edit_title);
        //e.preventDefault();
        $.ajax({
            url: get_url,
            type:"POST",
            data:{
                "_token": csrf_token,
                equipment_id: equipment_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="capacity"]').val(data.capacity);
                    modal.find('.modal-body input[name="code"]').val(data.code);
                    modal.find('.modal-body input[name="model"]').val(data.model);
                    modal.find('.modal-body input[name="serial_number"]').val(data.serial_number);
                    modal.find('.modal-body input[name="min_temperature"]').val(data.min_temperature);
                    modal.find('.modal-body input[name="max_temperature"]').val(data.max_temperature);
                    modal.find('.modal-body input[name="installed_date"]').val(data.installed_date);
                    modal.find('.modal-body select[name="room_id"]').val(data.room_id).trigger('change');
                    modal.find('.modal-body select[name="equipment_type_concept_id"]').val(data.equipment_type_concept_id).trigger('change');
                } else{
                    handleMessage(res.success, res.message)
                }
            },
        });
    }
})

