// Copyright (c) 2018, JINGROW
// For license information, please see license.txt
jingrow.provide("jingrow.auto_repeat");

jingrow.ui.form.on("Auto Repeat", {
	setup: function (frm) {
		frm.fields_dict["reference_pagetype"].get_query = function () {
			return {
				query: "jingrow.automation.pagetype.auto_repeat.auto_repeat.get_auto_repeat_pagetypes",
			};
		};

		frm.fields_dict["reference_document"].get_query = function () {
			return {
				filters: {
					auto_repeat: "",
				},
			};
		};

		frm.fields_dict["print_format"].get_query = function () {
			return {
				filters: {
					pg_type: frm.pg.reference_pagetype,
				},
			};
		};
	},

	refresh: function (frm) {
		// auto repeat message
		if (frm.is_new()) {
			let customize_form_link = `<a href="/app/customize-form">${__("Customize Form")}</a>`;
			frm.dashboard.set_headline(
				__('To configure Auto Repeat, enable "Allow Auto Repeat" from {0}.', [
					customize_form_link,
				])
			);
		}

		// view document button
		if (!frm.is_dirty()) {
			let label = __("View {0}", [__(frm.pg.reference_pagetype)]);
			frm.add_custom_button(label, () =>
				jingrow.set_route("List", frm.pg.reference_pagetype, { auto_repeat: frm.pg.name })
			);
		}

		// auto repeat schedule
		jingrow.auto_repeat.render_schedule(frm);

		frm.trigger("toggle_submit_on_creation");
	},

	reference_pagetype: function (frm) {
		frm.trigger("toggle_submit_on_creation");
	},

	toggle_submit_on_creation: function (frm) {
		// submit on creation checkbox
		if (frm.pg.reference_pagetype) {
			jingrow.model.with_pagetype(frm.pg.reference_pagetype, () => {
				let meta = jingrow.get_meta(frm.pg.reference_pagetype);
				frm.toggle_display("submit_on_creation", meta.is_submittable);
			});
		}
	},

	template: function (frm) {
		if (frm.pg.template) {
			jingrow.model.with_pg("Email Template", frm.pg.template, () => {
				let email_template = jingrow.get_pg("Email Template", frm.pg.template);
				frm.set_value("subject", email_template.subject);
				frm.set_value("message", email_template.response);
				frm.refresh_field("subject");
				frm.refresh_field("message");
			});
		}
	},

	get_contacts: function (frm) {
		frm.call("fetch_linked_contacts");
	},

	preview_message: function (frm) {
		if (frm.pg.message) {
			jingrow.call({
				method: "jingrow.automation.pagetype.auto_repeat.auto_repeat.generate_message_preview",
				args: {
					reference_dt: frm.pg.reference_pagetype,
					reference_pg: frm.pg.reference_document,
					subject: frm.pg.subject,
					message: frm.pg.message,
				},
				callback: function (r) {
					if (r.message) {
						jingrow.msgprint(r.message.message, r.message.subject);
					}
				},
			});
		} else {
			jingrow.msgprint(__("Please setup a message first"), __("Message not setup"));
		}
	},
});

jingrow.auto_repeat.render_schedule = function (frm) {
	if (!frm.is_dirty() && frm.pg.status !== "Disabled") {
		frm.call("get_auto_repeat_schedule").then((r) => {
			frm.dashboard.reset();
			frm.dashboard.add_section(
				jingrow.render_template("auto_repeat_schedule", {
					schedule_details: r.message || [],
				}),
				__("Auto Repeat Schedule")
			);
			frm.dashboard.show();
		});
	} else {
		frm.dashboard.hide();
	}
};
