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

jingrow.ui.form.on('Team', {
	refresh: function (frm) {
		jingrow.dynamic_link = { pg: frm.pg, fieldname: 'name', pagetype: 'Team' };
		jingrow.contacts.render_address_and_contact(frm);

		if (!frm.pg.jerp_partner) {
			frm.add_custom_button('Enable Partner Privileges', () =>
				jingrow.confirm(
					`Enable JERP Partner Privileges for ${frm.pg.name.bold()}? They will be allowed to create sites without adding a card and can use Partner Credits to pay for invoices.`,
					() =>
						frm.call('enable_jerp_partner_privileges').then(() =>
							jingrow.msgprint({
								title: 'Note: Payment mode changed to `Partner Credits`',
								message:
									'Please make sure existing prepaid credits are taken care of.',
							}),
						),
				),
			);
		} else {
			frm.add_custom_button('Disable Partner Privileges', () =>
				jingrow.confirm(
					`Disable JERP Partner Privileges for ${frm.pg.name.bold()}? `,
					() =>
						frm.call('disable_jerp_partner_privileges').then(() => {
							jingrow.msgprint({
								title: 'Partner Privileges Disabled',
								message:
									frm.pg.payment_mode === 'Partner Credits'
										? 'Payment mode is still Partner Credits. If the credits are low for setteling invoices, you can change the payment mode.'
										: '',
							});
							frm.refresh();
						}),
				),
			);
		}

		frm.add_custom_button(
			'Suspend Sites',
			() => {
				jingrow.prompt(
					{ fieldtype: 'Data', label: 'Reason', fieldname: 'reason', reqd: 1 },
					({ reason }) => {
						frm.call('suspend_sites', { reason }).then((r) => {
							const sites = r.message;
							let how_many = 'No';
							if (sites) {
								how_many = sites.length;
							}
							jingrow.show_alert(`${how_many} sites were suspended.`);
						});
					},
				);
			},
			'Actions',
		);
		frm.add_custom_button(
			'Unsuspend Sites',
			() => {
				jingrow.prompt(
					{ fieldtype: 'Data', label: 'Reason', fieldname: 'reason', reqd: 1 },
					({ reason }) => {
						frm.call('unsuspend_sites', { reason }).then((r) => {
							const sites = r.message;
							let how_many = 'No';
							if (sites) {
								how_many = sites.length;
							}
							jingrow.show_alert(`${how_many} sites were unsuspended.`);
						});
					},
				);
			},
			'Actions',
		);

		frm.add_custom_button('Impersonate Team', () => {
			let team = frm.pg.name;
			window.open(`/dashboard/impersonate/${team}`);
		});

		if (frm.pg.payment_mode === 'Partner Credits') {
			frm.add_custom_button('Get Partner Credits', () =>
				frm.call('get_available_partner_credits').then((d) => {
					jingrow.msgprint({
						title: 'Credit Balance Fetched Successfully',
						message: `Available Credits on jingrow.com: <strong>${frm.pg.currency} ${d.message}</strong>`,
						indicator: 'green',
					});
				}),
			);
		}
	},
});

jingrow.ui.form.on('Team Member', {
	impersonate: function (frm, pagetype, member) {
		jingrow.prompt(
			[
				{
					fieldtype: 'HTML',
					options: 'Beware! Your current session will be replaced.',
				},
				{
					fieldtype: 'Text Editor',
					label: 'Reason',
					fieldname: 'reason',
					reqd: 1,
				},
			],
			({ reason }) => {
				frm.call('impersonate', { reason, member }).then((r) => {
					location.href = '/dashboard';
				});
			},
			'Impersonate User',
		);
	},
});
