const tabela = document.getElementById('tabelaMateriais').querySelector('tbody'); btn.addEventListener('click', async function (event) { event.preventDefault(); const resultado = await gravaItem(); if (resultado && resultado.status === 'success') { const select = document.getElementById('item_ambiente_id'); const nomeAmbiente = select.options[select.selectedIndex].text; const item = resultado.id_item || 'código'; const orcamentoId = resultado.orcamento_id || 'cód_orca'; const ambiente = nomeAmbiente; const material = document.getElementById('material_input').value || 'vazio'; const valorTotal = document.getElementById('item_vlr_total').value || ''; const descricao = document.getElementById('item_descricao').value || ''; // console.log('valorTotal 4991 :', valorTotal); document.getElementById('item_id').value = item; let linhaEditada = null; // console.log('btnEditarClicado 4947: ', btnEditarClicado); if (typeof btnEditarClicado !== 'undefined' && btnEditarClicado === true) { for (let i = 0; i < tabela.rows.length; i++) { const celulaItem = tabela.rows[i].cells[0]; // console.log('celulaItem 4951: ',celulaItem); if (celulaItem && celulaItem.textContent === item) { linhaEditada = tabela.rows[i]; break; } } } const criarCelulas = (linha) => { // Garante que a linha tenha 7 células while (linha.cells.length < 7) { linha.insertCell(); } linha.cells[0].textContent = item; linha.cells[1].textContent = orcamentoId; linha.cells[2].textContent = ambiente; linha.cells[3].textContent = material; linha.cells[4].textContent = valorTotal; linha.cells[5].textContent = descricao; const celulaAcoes = linha.cells[6]; celulaAcoes.innerHTML = ''; celulaAcoes.style.textAlign = 'center'; celulaAcoes.style.whiteSpace = 'nowrap'; const btnEditar = document.createElement('button'); btnEditar.type = 'button'; btnEditar.innerHTML = '✏️'; btnEditar.title = 'Editar item'; btnEditar.style.border = 'none'; btnEditar.style.background = 'transparent'; btnEditar.style.cursor = 'pointer'; btnEditar.style.marginRight = '6px'; btnEditar.style.fontSize = '14px'; btnEditar.addEventListener('click', async function (event) { event.preventDefault(); event.preventDefault(); const confirmadoe = await confirmarAcaolinha( 'Confirma Editar ?', 'Tem certeza que deseja Editar este item?', 'warning' ); if (confirmadoe) { btnEditarClicado = true; const linhaAtual = event.target.closest('tr'); if (linhaAtual) { const valorTotal = linhaAtual.cells[4].textContent; const descricao = linhaAtual.cells[5].textContent; const itemLinha = linhaAtual.cells[0].textContent; await lerItem(itemLinha); document.getElementById('item_vlr_total').value = valorTotal; document.getElementById('item_descricao').value = descricao; habilitarCampos(); } else { console.warn('Não foi possível localizar a linha para edição.'); } } }); const btnExcluir = document.createElement('button'); btnExcluir.type = 'button'; btnExcluir.textContent = '❌'; btnExcluir.title = 'Excluir item'; btnExcluir.style.border = 'none'; btnExcluir.style.background = 'transparent'; btnExcluir.style.cursor = 'pointer'; btnExcluir.style.fontSize = '14px'; btnExcluir.addEventListener('click', async function (event) { event.preventDefault(); const confirmado = await confirmarAcaolinha( 'Confirma exclusão?', 'Tem certeza que deseja excluir este item?', 'warning' ); if (confirmado) { const linhaAtual = event.target.closest('tr'); if (linhaAtual) { const itemLinha = linhaAtual.cells[0].textContent; await excluiItem(itemLinha); linhaAtual.remove(); somaTotalOrcamento(); console.log('Item excluído:', itemLinha); } else { console.warn('Não foi possível localizar a linha para exclusão.'); } } }); celulaAcoes.appendChild(btnEditar); celulaAcoes.appendChild(btnExcluir); }; if (linhaEditada) { criarCelulas(linhaEditada); btnEditarClicado = false; } else { const linha = tabela.insertRow(-1); criarCelulas(linha); }