document.addEventListener("DOMContentLoaded", function () {
    const fields = [
        "gross_total_income", "disallowance_14a", "disallowance_37",
        "deduction_80ia_business", "deduction_80ia_misc", "deduction_80ia_other",
        "deduction_sec37_disallowance", "deduction_80g", "net_taxable_income",
        "tax_30_percent", "tax_book_profit_18_5", "tax_payable", "surcharge_12",
        "edu_cess_3", "total_tax_payable", "mat_credit", "interest_234c",
        "total_tax", "advance_tax", "tds", "tcs", "tax_on_assessment", "refund"
    ];

    function getValue(id) {
        return parseFloat(document.getElementsByName(id)[0].value) || 0;
    }

    function setValue(id, val) {
        document.getElementsByName(id)[0].value = val.toFixed(2);
    }

    function calculate() {

        let gross_total_income = getValue("gross_total_income");
        let disallowance_14a = getValue("disallowance_14a");
        let disallowance_37 = getValue("disallowance_37");

        // FORMULAS
        setValue("gross_total_income", disallowance_37 + gross_total_income);
        setValue("disallowance_37", disallowance_14a + disallowance_37);

        // Deductions
        let d80_business = getValue("deduction_80ia_business");
        let d80_misc = getValue("deduction_80ia_misc");
        let d80_other = getValue("deduction_80ia_other");

        let deduction_sec37 = d80_business + d80_misc + d80_other - 1.35;
        setValue("deduction_sec37_disallowance", deduction_sec37);

        let deduction_80g = getValue("deduction_80g");

        // Net taxable income
        let net_taxable_income = gross_total_income - deduction_sec37 - deduction_80g;
        setValue("net_taxable_income", net_taxable_income);

        // Tax calculations
        setValue("tax_30_percent", net_taxable_income * 0.30);

        let tax_book_profit = getValue("tax_book_profit_18_5");
        setValue("tax_payable", tax_book_profit);

        let surcharge = tax_book_profit * 0.12;
        setValue("surcharge_12", surcharge);

        let edu_cess = (tax_book_profit + surcharge) * 0.03;
        setValue("edu_cess_3", edu_cess);

        let total_tax_payable = tax_book_profit + surcharge + edu_cess;
        setValue("total_tax_payable", total_tax_payable);

        let mat_credit = getValue("mat_credit");
        let interest_234c = getValue("interest_234c");

        let total_tax = total_tax_payable + mat_credit + interest_234c;
        setValue("total_tax", total_tax);

        // Assessment
        let adv_tax = getValue("advance_tax");
        let tds = getValue("tds");
        let tcs = getValue("tcs");

        let tax_on_assessment = adv_tax + tds + tcs;
        setValue("tax_on_assessment", tax_on_assessment);

        let refund = total_tax - tax_on_assessment;
        setValue("refund", refund);
    }

    // Attach event listeners
    fields.forEach(id => {
        const element = document.getElementsByName(id)[0];
        if (element) {
            element.addEventListener("input", calculate);
        }
    });
});
