const formID10 = '240666519501860';
const apiKey10 = '6e2676fa4067aecefdfae6fca7c4bb41';

// Fetch data from the API
fetch(`https://api.jotform.com/form/${formID10}/submissions?apiKey=${apiKey10}`)
    .then(response => response.json())
    .then(data => {
        
        // Filter to get submissions with contratoFirmado
        const filteredSubmissions = data.content.filter(entry => {
            const answers = entry.answers;
            for (const key in answers) {
                if (answers[key].name === "asambleaAnexo" && answers[key].answer== 1) {
                    return true;
                }
            }
            return false;
        });
        
        console.log(filteredSubmissions)
        
        let AmountCompra = 0;
        filteredSubmissions.forEach(entry => {
            const answers = entry.answers;
            let amount = 0;
            let comision = 0;

            // Extract monto and comision from the answers
            for (const key in answers) {
                if (answers[key].name === "compra") {
                    amount = parseFloat(answers[key].answer.replace(/[\$,]/g, ""));
                }
            }

            AmountCompra += amount; // Add result to total
            
        });
        
        totalAmountCompra = (20000-AmountCompra).toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
        console.log(totalAmountCompra)
        localStorage.setItem('totalAcciones', totalAmountCompra)
    })