// Copyright (c) 2025, JINGROW
// For license information, please see license.txt

jingrow.ui.form.on('Partner Payment Payout', {
	refresh(frm) {
		if (frm.pg.pagestatus == 0) {
			frm.add_custom_button('Fetch Payments', () => {
				jingrow.call({
					method: 'jcloud.api.local_payments.mpesa.utils.fetch_payments',
					args: {
						// transaction_pagetype: frm.pg.transaction_pagetype,
						from_date: frm.pg.from_date,
						to_date: frm.pg.to_date,
						partner: frm.pg.partner,
						payment_gateway: frm.pg.payment_gateway,
					},
					callback: function (response) {
						if (response.message) {
							// Clear existing entries in transfer_items
							frm.clear_table('transfer_items');

							response.message.forEach((payment) => {
								let row = frm.add_child('transfer_items');
								row.transaction_id = payment.name;
								row.posting_date = payment.posting_date;
								row.amount = payment.amount;
							});

							frm.refresh_field('transfer_items');
							// jingrow.msgprint("Payments fetched and added to the transfer items table.");
						}
					},
				});
			});
		}
	},
});
