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

jingrow.ui.form.on("Calendar View", {
	onload: function (frm) {
		frm.trigger("reference_pagetype");
	},
	refresh: function (frm) {
		if (!frm.is_new()) {
			frm.add_custom_button(__("Show Calendar"), () =>
				jingrow.set_route("List", frm.pg.reference_pagetype, "Calendar", frm.pg.name)
			);
		}
	},
	reference_pagetype: function (frm) {
		const { reference_pagetype } = frm.pg;
		if (!reference_pagetype) return;

		jingrow.model.with_pagetype(reference_pagetype, () => {
			const meta = jingrow.get_meta(reference_pagetype);

			const subject_options = meta.fields
				.filter((df) => !jingrow.model.no_value_type.includes(df.fieldtype))
				.map((df) => df.fieldname);

			const date_options = meta.fields
				.filter((df) => ["Date", "Datetime"].includes(df.fieldtype))
				.map((df) => df.fieldname);

			frm.set_df_property("subject_field", "options", subject_options);
			frm.set_df_property("start_date_field", "options", date_options);
			frm.set_df_property("end_date_field", "options", date_options);
			frm.refresh();
		});
	},
});
