jingrow.listview_settings["PageType"] = {
	onload: function (list_view) {
		// Setup module field filtering based on package
		const package_field = list_view.page.fields_dict.package;
		const module_field = list_view.page.fields_dict.module;
		
		if (package_field && module_field) {
			module_field.get_query = function() {
				const package_value = package_field.get_value();
				return package_value ? { filters: { package: package_value } } : {};
			};

			package_field.$input.on("change", function() {
				module_field.refresh();
			});
		}
	},

	primary_action: function () {
		this.new_pagetype_dialog();
	},

	new_pagetype_dialog() {
		let non_developer = jingrow.session.user !== "Administrator" || !jingrow.boot.developer_mode;
		let package_value = null;
		
		let fields = [
			{
				label: __("Name"),
				fieldname: "name",
				fieldtype: "Data",
				reqd: 1,
			},
			{ fieldtype: "Column Break" },
			{
				label: __("Package"),
				fieldname: "package",
				fieldtype: "Link",
				options: "Package",
				reqd: 0,
			},
			{ fieldtype: "Column Break" },
			{
				label: __("Module"),
				fieldname: "module",
				fieldtype: "Link",
				options: "Module Def",
				reqd: 1,
				get_query: function() {
					return package_value ? { filters: { package: package_value } } : {};
				}
			},
			{ fieldtype: "Section Break" },
			{
				label: __("Is Submittable"),
				fieldname: "is_submittable",
				fieldtype: "Check",
				description: __(
					"Once submitted, submittable documents cannot be changed. They can only be Cancelled and Amended."
				),
				depends_on: "eval:!pg.istable && !pg.issingle",
			},
			{
				label: __("Is Child Table"),
				fieldname: "istable",
				fieldtype: "Check",
				description: __("Child Tables are shown as a Grid in other PageTypes"),
				depends_on: "eval:!pg.is_submittable && !pg.issingle",
			},
			{
				label: __("Editable Grid"),
				fieldname: "editable_grid",
				fieldtype: "Check",
				depends_on: "istable",
				default: 1,
			},
			{
				label: __("Is Single"),
				fieldname: "issingle",
				fieldtype: "Check",
				description: __(
					"Single Types have only one record no tables associated. Values are stored in tabSingles"
				),
				depends_on: "eval:!pg.istable && !pg.is_submittable",
			},
			{
				label: "Is Tree",
				fieldname: "is_tree",
				fieldtype: "Check",
				default: "0",
				depends_on: "eval:!pg.istable",
				description: "Tree structures are implemented using Nested Set",
			},
			{
				label: __("Custom?"),
				fieldname: "custom",
				fieldtype: "Check",
				default: non_developer,
				read_only: non_developer,
			},
		];

		if (!non_developer) {
			fields.push({
				label: "Is Virtual",
				fieldname: "is_virtual",
				fieldtype: "Check",
				default: "0",
			});
		}

		let new_d = new jingrow.ui.Dialog({
			title: __("Create New PageType"),
			fields: fields,
			primary_action_label: __("Create & Continue"),
			primary_action(values) {
				if (!values.istable) values.editable_grid = 0;
				jingrow.db
					.insert({
						pagetype: "PageType",
						...values,
						permissions: [
							{
								create: 1,
								delete: 1,
								email: 1,
								export: 1,
								print: 1,
								read: 1,
								report: 1,
								role: "System Manager",
								share: 1,
								write: 1,
								submit: values.is_submittable ? 1 : 0,
							},
						],
						fields: [{ fieldtype: "Section Break" }],
					})
					.then((pg) => {
						jingrow.set_route("Form", "PageType", pg.name);
					});
			},
			secondary_action_label: __("Cancel"),
			secondary_action() {
				new_d.hide();
				if (jingrow.get_route()[0] === "Form") {
					jingrow.set_route("List", "PageType");
				}
			},
		});
		
		// Setup onchange for package field
		setTimeout(() => {
			let package_field = new_d.get_field("package");
			if (package_field) {
				let original_onchange = package_field.df.onchange;
				package_field.df.onchange = function() {
					package_value = new_d.get_value("package");
					new_d.set_value("module", null);
					// Refresh module field to apply new filter
					let module_field = new_d.get_field("module");
					if (module_field) {
						module_field.refresh();
					}
					if (original_onchange) {
						original_onchange.apply(this);
					}
				};
			}
		}, 100);
		
		new_d.show();
	},
};
