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

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

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

$('#modal-rack').on('show.bs.modal', function (event) {
    var button = $(event.relatedTarget)
    var action = button.data('action')
    var modal = $(this);
    if(action === "new"){
        modal.find('.modal-body input').val("");
        modal.find('.modal-body textarea').val("");
        modal.find('.modal-body select[name="equipment_id"]').val("").trigger('change');
        modal.find('.modal-body input[name="rack_id"]').val(0);
        modal.find('.modal-title').html(modal_add_title);
    }else if(action === "edit"){
        rack_id = button.data('rack_id');
        modal.find('.modal-body input[name="rack_id"]').val(rack_id);
        modal.find('.modal-title').html(modal_edit_title);
        $.ajax({
            url: get_url,
            type: "POST",
            data: {
                "_token": csrf_token,
                rack_id: rack_id,
            },
            success: function(res){
                if(res.success) {
                    let data = res.data;
                    modal.find('.modal-body input[name="code"]').val(data.code);
                    modal.find('.modal-body input[name="name"]').val(data.name);
                    modal.find('.modal-body textarea[name="description"]').val(data.description);
                    modal.find('.modal-body select[name="equipment_id"]').val(data.equipment_id).trigger('change');
                } else{
                    handleMessage(res.success, res.message)
                }
            },
        });
    }
})
