# Copyright (c) 2021, InfluxFramework LLC
# License: MIT. See LICENSE

import influxframework
from influxframework.model.document import Document
from influxframework.modules.export_file import export_to_files


class FormTour(Document):
	def before_save(self):
		meta = influxframework.get_meta(self.reference_doctype)
		for step in self.steps:
			if step.is_table_field and step.parent_fieldname:
				parent_field_df = meta.get_field(step.parent_fieldname)
				step.child_doctype = parent_field_df.options

				field_df = influxframework.get_meta(step.child_doctype).get_field(step.fieldname)
				step.label = field_df.label
				step.fieldtype = field_df.fieldtype
			else:
				field_df = meta.get_field(step.fieldname)
				step.label = field_df.label
				step.fieldtype = field_df.fieldtype

	def on_update(self):
		if influxframework.conf.developer_mode and self.is_standard:
			export_to_files([["Form Tour", self.name]], self.module)
