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

jingrow.ui.form.on("Milestone Tracker", {
	refresh: function (frm) {
		frm.trigger("update_options");
	},
	document_type: function (frm) {
		frm.trigger("update_options");
	},
	update_options: function (frm) {
		// update select options for `track_field`
		let pagetype = frm.pg.document_type;
		let track_fields = [];

		if (pagetype) {
			jingrow.model.with_pagetype(pagetype, () => {
				// get all date and datetime fields
				jingrow.get_meta(pagetype).fields.map((df) => {
					if (["Link", "Select"].includes(df.fieldtype)) {
						track_fields.push({ label: df.label, value: df.fieldname });
					}
				});
				frm.set_df_property("track_field", "options", track_fields);
			});
		} else {
			// update select options
			frm.set_df_property("track_field", "options", []);
		}
	},
});
