// Copyright (c) 2015, JINGROW and Contributors
// MIT License. See license.txt

// Refresh
// --------

jingrow.ui.form.on("Custom Field", {
	setup: function (frm) {
		frm.set_query("dt", function (pg) {
			var filters = [
				["PageType", "issingle", "=", 0],
				["PageType", "custom", "=", 0],
				["PageType", "name", "not in", jingrow.model.core_pagetypes_list],
				["PageType", "restrict_to_domain", "in", jingrow.boot.active_domains],
			];
			if (jingrow.session.user !== "Administrator") {
				filters.push(["PageType", "module", "not in", ["Core", "Custom"]]);
			}
			return {
				filters: filters,
			};
		});
	},
	refresh: function (frm) {
		frm.toggle_enable("dt", frm.pg.__islocal);
		frm.trigger("dt");
		frm.toggle_reqd("label", !frm.pg.fieldname);
		frm.trigger("add_rename_field");

		if (frm.pg.is_system_generated) {
			frm.dashboard.add_comment(
				__(
					"<strong>Warning:</strong> This field is system generated and may be overwritten by a future update. Modify it using {0} instead.",
					[
						jingrow.utils.get_form_link(
							"Customize Form",
							"Customize Form",
							true,
							__("Customize Form"),
							{
								pg_type: frm.pg.dt,
							}
						),
					]
				),
				"yellow",
				true
			);
		}
	},
	dt: function (frm) {
		if (!frm.pg.dt) {
			set_field_options("insert_after", "");
			return;
		}
		var insert_after = frm.pg.insert_after || null;
		return jingrow.call({
			method: "jingrow.custom.pagetype.custom_field.custom_field.get_fields_label",
			args: { pagetype: frm.pg.dt, fieldname: frm.pg.fieldname },
			callback: function (r) {
				if (r) {
					if (r._server_messages && r._server_messages.length) {
						frm.set_value("dt", "");
					} else {
						set_field_options("insert_after", r.message);
						var fieldnames = $.map(r.message, function (v) {
							return v.value;
						});

						if (insert_after == null || !fieldnames.includes(insert_after)) {
							insert_after = fieldnames[-1];
						}

						frm.set_value("insert_after", insert_after);
					}
				}
			},
		});
	},
	label: function (frm) {
		if (frm.pg.label && jingrow.utils.has_special_chars(frm.pg.label)) {
			frm.fields_dict["label_help"].disp_area.innerHTML =
				'<font color = "red">' + __("Special Characters are not allowed") + "</font>";
			frm.set_value("label", "");
		} else {
			frm.fields_dict["label_help"].disp_area.innerHTML = "";
		}
	},
	fieldtype: function (frm) {
		if (frm.pg.fieldtype == "Link") {
			frm.fields_dict["options_help"].disp_area.innerHTML = __(
				"Name of the Document Type (PageType) you want this field to be linked to. e.g. Customer"
			);
		} else if (frm.pg.fieldtype == "Select") {
			frm.fields_dict["options_help"].disp_area.innerHTML =
				__("Options for select. Each option on a new line.") +
				" " +
				__("e.g.:") +
				"<br>" +
				__("Option 1") +
				"<br>" +
				__("Option 2") +
				"<br>" +
				__("Option 3") +
				"<br>";
		} else if (frm.pg.fieldtype == "Dynamic Link") {
			frm.fields_dict["options_help"].disp_area.innerHTML = __(
				"Fieldname which will be the PageType for this link field."
			);
		} else {
			frm.fields_dict["options_help"].disp_area.innerHTML = "";
		}
	},
	add_rename_field(frm) {
		if (!frm.is_new()) {
			frm.add_custom_button(__("Rename Fieldname"), () => {
				jingrow.prompt(
					{
						fieldtype: "Data",
						label: __("Fieldname"),
						fieldname: "fieldname",
						reqd: 1,
						default: frm.pg.fieldname,
					},
					function (data) {
						jingrow
							.xcall(
								"jingrow.custom.pagetype.custom_field.custom_field.rename_fieldname",
								{
									custom_field: frm.pg.name,
									fieldname: data.fieldname,
								}
							)
							.then(() => frm.reload());
					},
					__("Rename Fieldname"),
					__("Rename")
				);
			});
		}
	},
});

jingrow.utils.has_special_chars = function (t) {
	var iChars = "!@#$%^&*()+=-[]\\';,./{}|\":<>?";
	for (var i = 0; i < t.length; i++) {
		if (iChars.indexOf(t.charAt(i)) != -1) {
			return true;
		}
	}
	return false;
};
