<?php

namespace Modules\TaskManagement\Tasks;

class View_Utils_FormAdmin extends \App\Mvc\View_Utils_FormAbstract
{

	/**
	 * Creates the form for task insert/update.
	 * @see \App\Mvc\View_Utils_FormAbstract::getForm()
	 */
	public function getForm()
	{
		// load globals
		$CFG   = \Engine\Loader_Cfg::getInstance()->getGlobal();
		$_LANG = \App\Loader_Lang::getInstance()->getGlobal();

		// load the configurations for this form
		$entCfg = \Entity\Task::loadCFG();

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

		// GENERAL ////////////////////////////////////////////////////////////////////////////////
		$fieldset = \Engine\Form\Container\Fieldset::create($this->lang['formFieldCat']['general']);
		$form->addContainer($fieldset);

		// category
		$fld_category = new \App\Form\Input\Select_TaskCategories($this->lang['formFields']['idCategory'], 'idCategory', $this->values['idCategory']);
		$fld_category->showDefaultOption(true)
			->setDefaultOptionText($this->lang['formFields']['select']['idCategory']);
		$fld_category->importFromConfig($entCfg['idCategory']);
		$fieldset->addElement($fld_category);

		// description
		$fld_description = new \Engine\Form\Input\Textarea($this->lang['formFields']['description'], 'description', $this->values['description']);
		//$fld_description->setStyle('width', '250')->setStyle('height', '25');
		$fld_description->importFromConfig($entCfg['description']);
		$fieldset->addElement($fld_description);

		// priority
		$fld_priority = new \Engine\Form\Input\Select($this->lang['formFields']['priority'], 'priority', $this->values['priority']);
		$fld_priority->showDefaultOption(true)
			->setDefaultOptionText($this->lang['formFields']['select']['priority']);
		$fld_priority->importFromConfig($entCfg['priority']);
		$fld_priority->setPossibleValues([
				'1' => ($_LANG['tasks']['priorities']['1']?:"lowest"),
				'2' => ($_LANG['tasks']['priorities']['2']?:"low"),
				'3' => ($_LANG['tasks']['priorities']['3']?:"medium"),
				'4' => ($_LANG['tasks']['priorities']['4']?:"high"),
				'5' => ($_LANG['tasks']['priorities']['5']?:"highest"),
		]);
		$fieldset->addElement($fld_priority);

		// office
		$fld_office = new \App\Form\Input\Autocomplete_Office($this->lang['formFields']['idOffice'], 'idOffice', $this->values['idOffice']);
		$fld_office->importFromConfig($entCfg['idOffice']);
		$form->addElement($fld_office);

		// departament
		$fld_department = new \App\Form\Input\Autocomplete_Department($this->lang['formFields']['idDepartment'], 'idDepartment', $this->values['idDepartment']);
		$fld_department->importFromConfig($entCfg['idDepartment']);
		$form->addElement($fld_department);

		// assignor
		$fld_assignor = new \App\Form\Input\Autocomplete_User($this->lang['formFields']['idAssignorUser'], 'idAssignorUser', $this->values['idAssignorUser']);
		$fld_assignor->importFromConfig($entCfg['idAssignorUser']);
		$form->addElement($fld_assignor);

		// responsible
		$fld_responsible = new \App\Form\Input\Autocomplete_User($this->lang['formFields']['idResponsibleUser'], 'idResponsibleUser', $this->values['idResponsibleUser']);
		$fld_responsible->importFromConfig($entCfg['idResponsibleUser']);
		$form->addElement($fld_responsible);

		// free
		$fld_free = new \Engine\Form\Input\Checkbox($this->lang['formFields']['free'], 'free', $this->values['free']);
		$fld_free->importFromConfig($entCfg['free']);
		$fld_free->setCustomHTML($this->lang['formFields']['details']['free']);
		$fld_free->setInvisible(!\App\UserAccess\CurrentUserAccessFast::getInstance()->hasAccessToOption('taskSetAsFree'));
		$fieldset->addElement($fld_free);

		// deadline
		$fld_deadline = new \Engine\Form\Input\Datetime($this->lang['formFields']['deadline'], 'deadline', $this->values['deadline']);
		$fld_deadline->importFromConfig($entCfg['deadline']);
		$fld_deadline->setCustomHTML($this->lang['formFields']['details']['deadline']);
		$fieldset->addElement($fld_deadline);

		// pivot
		$pivotPossibleValues = [];
		foreach ($CFG['tasks']['pivotsList'] as $pivot) {
			$pivotPossibleValues[$pivot] = $_LANG['tasks']['pivotsArr'][$pivot];
		}
		$fld_pivot = new \Engine\Form\Input\Select($this->lang['formFields']['pivot'], 'pivot', $this->values['pivot']);
		$fld_pivot->showDefaultOption(true)
			->setDefaultOptionText($this->lang['formFields']['select']['pivot']);
		$fld_pivot->importFromConfig($entCfg['pivot']);
		$fld_pivot->setPossibleValues($pivotPossibleValues);
		$fieldset->addElement($fld_pivot);

		// idPivot
		$fld_idPivot = new \Engine\Form\Input\Text($this->lang['formFields']['idPivot'], 'idPivot', $this->values['idPivot']);
		$fieldset->addElement($fld_idPivot);

		// idOperationType
		$fld_operation = new \App\Form\Input\Select_TaskOperationTypes($this->lang['formFields']['idOperationType'], "idOperationType", $this->values['idOperationType']);
		$fld_operation->showDefaultOption(true)->setDefaultOptionText($this->lang['formFields']['select']['idOperationType']);
		$fld_operation->importFromConfig($entCfg['idOperationType']);
		$fieldset->addElement($fld_operation);

		// comments
		$fld_comments = new \Engine\Form\Input\Textarea($this->lang['formFields']['comments'], 'comments', $this->values['comments']);
		$fld_comments->importFromConfig($entCfg['comments']);
		$fieldset->addElement($fld_comments);


		// events fieldset (for the insert operation)
		if ($this->controller->getAction()=='insert') {

			$fieldset = \Engine\Form\Container\Fieldset::create($this->lang['formFieldCat']['events']);
			$form->addContainer($fieldset);

			$eventsTypesPossibleValues = [];
			foreach ($entCfg['_special']['eventTypes'] as $eventType) {
				$eventsTypesPossibleValues[$eventType] = $this->lang['formFields']['eventTypes'][$eventType];
			}

			// noResponsible
			$fld = new \Engine\Form\Input\Select($this->lang['formFields']['events']['noResponsibleNote'], 'events[noResponsibleNote]', $this->values['events']['noResponsibleNote']);
			$fld->showDefaultOption(true);
			$fld->setPossibleValues($eventsTypesPossibleValues);
			$fld->setTooltip($this->lang['formFields']['tooltips']['events']['noResponsible']);
			$fieldset->addElement($fld);

			// responsibleInstantNote
			$fld = new \Engine\Form\Input\Select($this->lang['formFields']['events']['responsibleInstantNote'], 'events[responsibleInstantNote]', $this->values['events']['responsibleInstantNote']);
			$fld->showDefaultOption(true);
			$fld->setPossibleValues($eventsTypesPossibleValues);
			$fld->setTooltip($this->lang['formFields']['tooltips']['events']['responsibleInstantNote']);
			$fieldset->addElement($fld);

			// responsibleDeadlineNote
			$fld = new \Engine\Form\Input\Select($this->lang['formFields']['events']['responsibleDeadlineNote'], 'events[responsibleDeadlineNote]', $this->values['events']['responsibleDeadlineNote']);
			$fld->showDefaultOption(true);
			$fld->setPossibleValues($eventsTypesPossibleValues);
			$fld->setTooltip($this->lang['formFields']['tooltips']['events']['responsibleDeadlineNote']);
			$fieldset->addElement($fld);
		}


		// FILES fieldset
		$fieldset = \Engine\Form\Container\Fieldset::create($this->lang['formFieldCat']['files']);
		$form->addContainer($fieldset);

		$idx = 0;
		if ($this->controller->getAction()=='update' && $this->values['filesList']) {
			foreach($this->values['filesList'] as $file) {
				// add file field
				$fld_file = new \Engine\Form\Input\File(sprintf("%s #%s", $this->lang['formFields']['fileName'], ($idx+1)), 'fileName_'.$idx);
				$fld_file->setFileName($file);
				$fld_file->setFilePath($this->values['filesPath'].$file);
				$fld_file->setFileDownloader( \Engine\Mvc\Dispatcher::getInstance()->getAppUrl('Modules/TaskManagement/Tasks', 'downloadFile', [
						'idTask'=>$this->values['id'],
						'file'=>$file,
				]) );
				$fieldset->addElement($fld_file);

				// add file name hidden field
				$fieldset->addElement(new \Engine\Form\Input\Hidden('', 'files[fileName_'.$idx.']', $file));

				$idx++;
			}
		}

		for($i=$idx; $i<($idx+3); $i++) {
			$fld_file = new \Engine\Form\Input\File(sprintf("%s #%s", $this->lang['formFields']['fileName'], ($i+1)), 'fileName_'.$i);
			$fieldset->addElement($fld_file);
		}


		// task id (for update operation)
		if ($this->controller->getAction()=='update') {
			$form->addElement(new \Engine\Form\Input\Hidden('', 'id', $this->values['id']));
		}

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

		return $form;
	}

}
