<?php

namespace Modules\Utils\Calendar\Listing;

class View_Utils_FormAdmin extends \App\Mvc\View_Utils_FormAbstract
{

	/**
	 * Creates the form for corporate insert/update.
	 * @see \App\Mvc\View_Utils_FormAbstract::getForm()
	 */
	public function getForm()
	{

		$form = \Engine\Form\General::create();
		$form->setStyle('width', '400');

		// EVENT ////////////////////////////////////////////////////////////////////////////////
		$fieldset1 = \Engine\Form\Container\Fieldset::create($this->lang['fieldSet']['event']);
		$form->addContainer($fieldset1);

		// event title
		$fld = new \Engine\Form\Input\Text($this->lang['formFields']['eventTitle'], 'title', $this->values['title']);
		$fld->setMandatory();
		$fieldset1->addElement($fld);

		// event description
		$fld = new \Engine\Form\Input\Textarea($this->lang['formFields']['eventDescription'], 'description', $this->values['description']);
		$fieldset1->addElement($fld);

		// event location
		$fld = new \Engine\Form\Input\Text($this->lang['formFields']['eventLocation'], 'location', $this->values['location']);
		$fieldset1->addElement($fld);

		// DATE AND TIME ////////////////////////////////////////////////////////////////////////
		$fieldset2 = \Engine\Form\Container\Fieldset::create($this->lang['fieldSet']['dateAndTime']);
		$form->addContainer($fieldset2);

		// start date time
		$fldStartDateTime = new \Engine\Form\Input\Datetime($this->lang['formFields']['startDate'], 'startDateTime', $this->values['startDateTime']);
		$fldStartDateTime->setMandatory();

		// end date time
		$fldEndDateTime = new \Engine\Form\Input\Datetime($this->lang['formFields']['endDate'], 'endDateTime', $this->values['endDateTime']);
		$fldEndDateTime->setMandatory();

		$fldStartDate = new \Engine\Form\Input\Date($this->lang['formFields']['startDate'], 'startDate', $this->values['startDate']);
		$fldStartDate->setMandatory();

		$fldEndDate = new \Engine\Form\Input\Date($this->lang['formFields']['endDate'], 'endDate', $this->values['endDate']);
		$fldEndDate->setMandatory();

		// all day
		$fld = new \Engine\Form\Input\Checkbox($this->lang['formFields']['allDay'], 'allDay', $this->values['allDay']);
		$fld->setControlledElements([
			'0'=>[$fldStartDateTime, $fldEndDateTime],
			'1'=>[$fldStartDate, $fldEndDate],
		]);
		$fieldset2->addElement($fld);

		// display the start/end date/time fields
		$fieldset2->addElement($fldStartDateTime);
		$fieldset2->addElement($fldEndDateTime);
		$fieldset2->addElement($fldStartDate);
		$fieldset2->addElement($fldEndDate);


		// DATE AND TIME ////////////////////////////////////////////////////////////////////////
		$fieldset3 = \Engine\Form\Container\Fieldset::create($this->lang['fieldSet']['alert']);
		$form->addContainer($fieldset3);

		// email
		$fldEmail = new \Engine\Form\Input\Checkbox($this->lang['formFields']['sendEmail'], 'mailStatus', $this->values['mailStatus']);
		if ($this->controller->getAction()=='update' and $this->values['mailStatus']==2) {
			$fldEmail->setDisabled();
			$fldEmail->setValue(true);
		}
		$fldEmail->setTooltip($this->lang['formFields']['sendMailTooltip']);

		// alert
		if ($this->controller->getAction()=='insert') {

			$fld = new \Engine\Form\Input\Select($this->lang['formFields']['alert'], 'alertPeriod', $this->values['alertPeriod']);
			$possibleValuesAlert = ['' => $this->lang['reminder']['noReminder']];
			foreach ($this->cfg['alertPeriodsList'] as $alertTime) {
				$possibleValuesAlert[$alertTime] = $this->lang['reminder'][$alertTime];
			}
			$fld->setPossibleValues($possibleValuesAlert);
			$fld->setControlledElements([''=>[$fldEmail]], 'enable');
			$fieldset3->addElement($fld);

		}
		elseif ($this->controller->getAction()=='update') {

			$fld=new \Engine\Form\Input\Datetime($this->lang['formFields']['alert'], 'alertDateTime', $this->values['alertDateTime']);
			$fieldset3->addElement($fld);

		}

		$fieldset3->addElement($fldEmail);

		// OPTIONS ////////////////////////////////////////////////////////////////////////
		$fieldset4 = \Engine\Form\Container\Fieldset::create($this->lang['fieldSet']['options']);
		$form->addContainer($fieldset4);

		// access
		$fld = new \Engine\Form\Input\Radio($this->lang['formFields']['access'], 'access', $this->values['access']);
		$accessList = [];
		foreach (['private', 'public'] as $access) {
			$accessList[$access] = $this->lang['access'][$access];
		}
		$fld->setPossibleValues($accessList);
		$fieldset4->addElement($fld);

		// users
		// event responsible
		if ($this->controller->getAction()=='insert') {
			$fld=new \App\Form\Input\Autocomplete_User($this->lang['formFields']['users'], 'idUser', $this->values['idUser']);
			$fld->setMandatory(true);
			$fieldset4->addElement($fld);
		}

		// submit button
		$form->addElement(new \Engine\Form\Input\Hidden('', $this->submitButtonName, 1));

		return $form;
	}

}
