frappe.ui.form.on('Tax Article Items Report', {
    tax_article: function(frm) {
        if (frm.doc.tax_article) {
            refresh_items_list(frm);
        } else {
            clear_results(frm);
        }
    },
    
    from_date: function(frm) {
        if (frm.doc.tax_article) {
            refresh_items_list(frm);
        }
    },
    
    to_date: function(frm) {
        if (frm.doc.tax_article) {
            refresh_items_list(frm);
        }
    },
    
    company: function(frm) {
        if (frm.doc.tax_article) {
            refresh_items_list(frm);
        }
    },
    
    refresh_button: function(frm) {
        if (frm.doc.tax_article) {
            refresh_items_list(frm);
        } else {
            frappe.msgprint(__('Please select a Tax Article first'));
        }
    }
});

function refresh_items_list(frm) {
    frappe.call({
        method: 'taxes_az.taxes_az.doctype.tax_article_items_report.tax_article_items_report.get_items_by_tax_article',
        args: {
            tax_article: frm.doc.tax_article,
            from_date: frm.doc.from_date,
            to_date: frm.doc.to_date,
            company: frm.doc.company
        },
        callback: function(r) {
            if (r.message) {
                display_items_list(frm, r.message);
            }
        }
    });
}

function display_items_list(frm, data) {
    let html = `
        <div class="table-responsive">
            <table class="table table-bordered">
                <thead>
                    <tr>
                        <th>Sales Invoice</th>
                        <th>Date</th>
                        <th>Customer</th>
                        <th>Item Code</th>
                        <th>Item Name</th>
                        <th>Qty</th>
                        <th>Rate</th>
                        <th>Amount</th>
                        <th>Tax Article</th>
                    </tr>
                </thead>
                <tbody>
    `;
    
    let total_amount = 0;
    
    data.forEach(function(row) {
        html += `
            <tr>
                <td><a href="/app/sales-invoice/${row.parent}" target="_blank">${row.parent}</a></td>
                <td>${frappe.datetime.str_to_user(row.posting_date)}</td>
                <td>${row.customer || ''}</td>
                <td>${row.item_code || ''}</td>
                <td>${row.item_name || ''}</td>
                <td>${row.qty || 0}</td>
                <td>${format_currency(row.rate || 0)}</td>
                <td>${format_currency(row.amount || 0)}</td>
                <td>${row.tax_article || ''}</td>
            </tr>
        `;
        total_amount += (row.amount || 0);
    });
    
    html += `
                </tbody>
                <tfoot>
                    <tr class="text-bold">
                        <td colspan="7">Total</td>
                        <td>${format_currency(total_amount)}</td>
                        <td></td>
                    </tr>
                </tfoot>
            </table>
        </div>
    `;
    
    frm.fields_dict.items_html.$wrapper.html(html);
    frm.set_value('total_items_found', data.length);
}

function clear_results(frm) {
    frm.fields_dict.items_html.$wrapper.html('<div class="text-muted">Select Tax Article to see results</div>');
    frm.set_value('total_items_found', 0);
}