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

jingrow.ui.form.on("Kanban Board", {
	onload: function (frm) {
		frm.trigger("reference_pagetype");
	},
	refresh: function (frm) {
		if (frm.is_new()) return;
		frm.add_custom_button("Show Board", function () {
			jingrow.set_route("List", frm.pg.reference_pagetype, "Kanban", frm.pg.name);
		});
	},
	reference_pagetype: function (frm) {
		// set field options
		if (!frm.pg.reference_pagetype) return;

		jingrow.model.with_pagetype(frm.pg.reference_pagetype, function () {
			var options = $.map(jingrow.get_meta(frm.pg.reference_pagetype).fields, function (d) {
				if (
					d.fieldname &&
					d.fieldtype === "Select" &&
					jingrow.model.no_value_type.indexOf(d.fieldtype) === -1
				) {
					return d.fieldname;
				}
				return null;
			});
			frm.set_df_property("field_name", "options", options);
			frm.get_field("field_name").refresh();
		});
	},
	field_name: function (frm) {
		var field = jingrow.meta.get_field(frm.pg.reference_pagetype, frm.pg.field_name);
		frm.pg.columns = [];
		field.options &&
			field.options.split("\n").forEach(function (o) {
				o = o.trim();
				if (!o) return;
				var d = frm.add_child("columns");
				d.column_name = o;
			});
		frm.refresh();
	},
});
