# Copyright (c) 2015, Xhive LLC
# For license information, please see license.txt


import xhiveframework
from xhiveframework import _
from xhiveframework.model.document import Document


class TrainingFeedback(Document):
	def validate(self):
		training_event = xhiveframework.get_doc("Training Event", self.training_event)
		if training_event.docstatus != 1:
			xhiveframework.throw(_("{0} must be submitted").format(_("Training Event")))

		emp_event_details = xhiveframework.db.get_value(
			"Training Event Employee",
			{"parent": self.training_event, "employee": self.employee},
			["name", "attendance"],
			as_dict=True,
		)

		if not emp_event_details:
			xhiveframework.throw(
				_("Employee {0} not found in Training Event Participants.").format(
					xhiveframework.bold(self.employee_name)
				)
			)

		if emp_event_details.attendance == "Absent":
			xhiveframework.throw(_("Feedback cannot be recorded for an absent Employee."))

	def on_submit(self):
		employee = xhiveframework.db.get_value(
			"Training Event Employee", {"parent": self.training_event, "employee": self.employee}
		)

		if employee:
			xhiveframework.db.set_value("Training Event Employee", employee, "status", "Feedback Submitted")

	def on_cancel(self):
		employee = xhiveframework.db.get_value(
			"Training Event Employee", {"parent": self.training_event, "employee": self.employee}
		)

		if employee:
			xhiveframework.db.set_value("Training Event Employee", employee, "status", "Completed")
