jingrow.ui.form.on("Report", {
	refresh: function (frm) {
		if (frm.pg.is_standard === "Yes" && !jingrow.boot.developer_mode) {
			// make the document read-only
			frm.disable_form();
		} else {
			frm.enable_save();
		}

		let pg = frm.pg;
		if (!pg.__islocal) {
			frm.add_custom_button(
				__("Show Report"),
				function () {
					switch (pg.report_type) {
						case "Report Builder":
							jingrow.set_route("List", pg.ref_pagetype, "Report", pg.name);
							break;
						case "Query Report":
							jingrow.set_route("query-report", pg.name);
							break;
						case "Script Report":
							jingrow.set_route("query-report", pg.name);
							break;
						case "Custom Report":
							jingrow.set_route("query-report", pg.name);
							break;
					}
				},
				"fa fa-table"
			);
		}

		if (pg.is_standard === "Yes" && frm.perm[0].write) {
			frm.add_custom_button(
				pg.disabled ? __("Enable Report") : __("Disable Report"),
				function () {
					frm.call("toggle_disable", {
						disable: pg.disabled ? 0 : 1,
					}).then(() => {
						frm.reload_pg();
					});
				},
				pg.disabled ? "fa fa-check" : "fa fa-off"
			);
		}

		frm.set_query("ref_pagetype", () => {
			return {
				filters: {
					istable: 0,
				},
			};
		});

		if (["en", "en-GB", "en-US"].includes(jingrow.boot.lang)) {
			frm.set_df_property("add_translate_data", "hidden", 1);
		} else {
			frm.set_df_property("add_translate_data", "hidden", 0);
		}
	},

	ref_pagetype: function (frm) {
		if (frm.pg.ref_pagetype) {
			frm.trigger("set_pagetype_roles");
		}
	},

	set_pagetype_roles: function (frm) {
		return frm.call("set_pagetype_roles").then(() => {
			frm.refresh_field("roles");
		});
	},
});
