jingrow.pages["print-format-builder-beta"].on_page_load = function (wrapper) {
	jingrow.ui.make_app_page({
		parent: wrapper,
		title: __("Print Format Builder"),
		single_column: true,
	});

	// hot reload in development
	if (jingrow.boot.developer_mode) {
		jingrow.hot_update = jingrow.hot_update || [];
		jingrow.hot_update.push(() => load_print_format_builder_beta(wrapper));
	}
};

jingrow.pages["print-format-builder-beta"].on_page_show = function (wrapper) {
	load_print_format_builder_beta(wrapper);
};

function load_print_format_builder_beta(wrapper) {
	let route = jingrow.get_route();
	let $parent = $(wrapper).find(".layout-main-section");
	$parent.empty();

	if (route.length > 1) {
		jingrow.require("print_format_builder.bundle.js").then(() => {
			jingrow.print_format_builder = new jingrow.ui.PrintFormatBuilder({
				wrapper: $parent,
				page: wrapper.page,
				print_format: route[1],
			});
		});
	} else {
		let d = new jingrow.ui.Dialog({
			title: __("Create or Edit Print Format"),
			fields: [
				{
					label: __("Action"),
					fieldname: "action",
					fieldtype: "Select",
					options: [
						{ label: __("Create New"), value: "Create" },
						{ label: __("Edit Existing"), value: "Edit" },
					],
					change() {
						let action = d.get_value("action");
						d.get_primary_btn().text(action === "Create" ? __("Create") : __("Edit"));
					},
				},
				{
					label: __("Select Document Type"),
					fieldname: "pagetype",
					fieldtype: "Link",
					options: "PageType",
					filters: {
						istable: 0,
					},
					reqd: 1,
					default: jingrow.route_options ? jingrow.route_options.pagetype : null,
				},
				{
					label: __("New Print Format Name"),
					fieldname: "print_format_name",
					fieldtype: "Data",
					depends_on: (pg) => pg.action === "Create",
					mandatory_depends_on: (pg) => pg.action === "Create",
				},
				{
					label: __("Select Print Format"),
					fieldname: "print_format",
					fieldtype: "Link",
					options: "Print Format",
					only_select: 1,
					depends_on: (pg) => pg.action === "Edit",
					get_query() {
						return {
							filters: {
								pg_type: d.get_value("pagetype"),
								print_format_builder_beta: 1,
							},
						};
					},
					mandatory_depends_on: (pg) => pg.action === "Edit",
				},
			],
			primary_action_label: __("Edit"),
			primary_action({ action, pagetype, print_format, print_format_name }) {
				if (action === "Edit") {
					jingrow.set_route("print-format-builder-beta", print_format);
				} else if (action === "Create") {
					d.get_primary_btn().prop("disabled", true);
					jingrow.db
						.insert({
							pagetype: "Print Format",
							name: print_format_name,
							pg_type: pagetype,
							print_format_builder_beta: 1,
						})
						.then((pg) => {
							jingrow.set_route("print-format-builder-beta", pg.name);
						})
						.finally(() => {
							d.get_primary_btn().prop("disabled", false);
						});
				}
			},
		});
		d.set_value("action", "Create");
		d.show();
	}
}
