function aggiungiRiga() {
const asset = document.getElementById('asset').value;
const quantita = parseFloat(document.getElementById('quantita').value);
const prezzo = parseFloat(document.getElementById('prezzo').value);
if (asset === "" || isNaN(quantita) || isNaN(prezzo)) {
alert("Per favore, compila tutti i campi correttamente.");
return;
}
const tabella = document.getElementById('tabellaInvestimenti').getElementsByTagName('tbody')[0];
const nuovaRiga = tabella.insertRow();
// Calcolo del Totale
const totale = (quantita * prezzo).toFixed(2);
nuovaRiga.innerHTML = `
<td>${asset}</td>
<td>${quantita}</td>
<td>€ ${prezzo.toFixed(2)}</td>
<td>€ ${totale}</td>
<td><button class="btn-delete" onclick="eliminaRiga(this)">Elimina</button></td>
`;
// Pulisce i campi input
document.getElementById('asset').value = "";
document.getElementById('quantita').value = "";
document.getElementById('prezzo').value = "";
}
function eliminaRiga(btn) {
const riga = btn.parentNode.parentNode;
riga.parentNode.removeChild(riga);
}
function svuotaTabella() {
document.querySelector('#tabellaInvestimenti tbody').innerHTML = "";
}
function scaricaCSV() {
let csv = "Asset,Quantita,Prezzo,Totale\n";
const righe = document.querySelectorAll("#tabellaInvestimenti tr");
for (let i = 1; i < righe.length; i++) {
const col = righe[i].querySelectorAll("td");
const rigaDati = Array.from(col).map(c => c.innerText.replace('€ ', '')).slice(0, 4).join(",");
csv += rigaDati + "\n";
}
const blob = new Blob([csv], { type: 'text/csv' });
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.setAttribute('href', url);
a.setAttribute('download', 'portafoglio_investimenti.csv');
a.click();
}