<?php
namespace Modules\Admin\Config\Remarks\AssistedInputs;

class View_Utils_FormAdmin extends \App\Mvc\View_Utils_FormAbstract
{

	/**
	 * (non-PHPdoc)
	 * @see \App\Mvc\View_Utils_FormAbstract::getForm()
	 */
	public function getForm()
	{

		// NEW FORM ///////////////////////////////////////////////////////////////////////////////////////
		$form = \Engine\Form\General::create();

		$fieldset = \Engine\Form\Container\Fieldset::create();
		$form->addContainer($fieldset);

		// INPUT NAME
		$nameFld = new \Engine\Form\Input\Text($this->lang['index']['labels']['remarkFormat'], "format", $this->values['format']);
		$nameFld->setDisabled();
		$fieldset->addElement($nameFld);

		$fieldset = \Engine\Form\Container\Fieldset::create($this->lang['index']['fieldsets']['serviceTypes']);
		$fieldset->setCollapsible();
		$fieldset->setCollapsed();
		$form->addContainer($fieldset);

		// NEW CONTAINER ----------------------------------------------------------------------------------


		$serviceTypes = \App\Config\Services_Basic::getInstance()->getAll();

		foreach ($serviceTypes as $groupDetails) {

			$groupLabel = $groupDetails['name'];

			$possibleValues = [];

			if ($groupDetails['list']) {
				foreach ($groupDetails['list'] as $code =>$serviceDetails) {
					$possibleValues[$code] = $serviceDetails['name'];
				}
			}

			$checkboxesServices = new \Engine\Form\Input\Checkboxes($groupLabel, 'services', $this->values['services'], $possibleValues);
			$checkboxesServices->setStyle('renderMode', 'aligned');
			$fieldset->addElement($checkboxesServices);
		}

		// ASSISTED INPUTS DETAILS ---------------------------------------------------------------------

		// SET FIELDSET FOR EACH VARIABLE OF REMARK ////////////////////////////////////////////////////////
		foreach ($this->values['vars'] as $indexVar => $varDetails) {

			$fieldset = \Engine\Form\Container\Fieldset::create($varDetails['name']);
			$form->addContainer($fieldset);
			$fieldset->setCollapsible();

			// INPUT ASSISTEDINPUT
			$assistedInputFld = new \Engine\Form\Input\Checkbox($this->lang['index']['labels']['assistedInput'], "vars[{$indexVar}][assistedInput]", $this->values['vars'][$indexVar]['assistedInput']);
			$fieldset->addElement($assistedInputFld);

			// INPUT NAME
			$nameFld = new \Engine\Form\Input\Text($this->lang['index']['labels']['name'], "vars[{$indexVar}][assistedInputDetails][name]", $this->values['vars'][$indexVar]['assistedInputDetails']['name']);
			$nameFld->setTooltip($this->lang['index']['tooltips']['name']);
			$fieldset->addElement($nameFld);

			// INPUT CATEGORY
			$categoryFld = new \Engine\Form\Input\Text($this->lang['index']['labels']['category'], "vars[{$indexVar}][assistedInputDetails][category]", $this->values['vars'][$indexVar]['assistedInputDetails']['category']);
			$categoryFld->setTooltip($this->lang['index']['tooltips']['category']);
			$fieldset->addElement($categoryFld);

			// INPUT DESCRIPTION
			$descriptionFld = new \Engine\Form\Input\Textarea($this->lang['index']['labels']['description'], "vars[{$indexVar}][assistedInputDetails][description]", $this->values['vars'][$indexVar]['assistedInputDetails']['description']);
			$descriptionFld->setTooltip($this->lang['index']['tooltips']['description']);
			$fieldset->addElement($descriptionFld);

			// INPUT MULTILINE
			$multiLineFld = new \Engine\Form\Input\Checkbox($this->lang['index']['labels']['multiLine'], "vars[{$indexVar}][assistedInputDetails][multiLine]", $this->values['vars'][$indexVar]['assistedInputDetails']['multiLine']);
			$multiLineFld->setTooltip($this->lang['index']['tooltips']['multiLine']);
			$fieldset->addElement($multiLineFld);

			// INPUT SELECT OPTIONS FOR TYPE SELECT
			if ($varDetails['type'] == 'select') {
				$containerOption=\Engine\Form\Container\Table::create('option');

				// scan each option from session
				foreach ($varDetails['options'] as $idxOption => $optionValue) {
					$selectOptionsFld[$idxOption] = new \Engine\Form\Input\Text($optionValue, "vars[{$indexVar}][assistedInputDetails][selectOptions][{$optionValue}]", $this->values['vars'][$indexVar]['assistedInputDetails']['selectOptions'][$optionValue]);
					$containerOption->addElement($selectOptionsFld[$idxOption]);
				}

				$input = new \Engine\Form\Input\Label($this->lang['index']['labels']['selectOptions']);
				$input->addContainer($containerOption);
				$input->setTooltip($this->lang['index']['tooltips']['selectOptions']);
				$input->setStyle('asElement', true);
				$fieldset->addElement($input);

				// controlled elements
				$assistedInputFld->setControlledElements([true => [$input, $nameFld, $categoryFld, $multiLineFld]],'disable');
			}

			// CURRENCY, SELECT, DATE,  BLANK
			elseif (in_array($varDetails['type'], ['date', 'currency'])) {

				// controlled elements
				$assistedInputFld->setControlledElements([true => [$nameFld, $categoryFld, $descriptionFld, $multiLineFld]],'disable');

			}

			// DIGITS, INT, FLOAT, ALNUM, STRING, BOOLEAN
			else {
				$autocompleteTypeFld=new \Engine\Form\Input\Select($this->lang['index']['labels']['autocompleteType'], 'vars['.$indexVar.'][assistedInputDetails][autocompleteType]', $this->values['vars'][$indexVar]['assistedInputDetails']['autocompleteType']);

				// possible values for autocomplete type
				$autoTypePossVal=[];
				// scan each autocomplete type
				foreach (\App\Config\Remarks::getAdmin()->getValidRemarkVarAutocompleteTypes() as $autoType) {
					// for this autocomplete type add lang
					$autoTypePossVal[$autoType] = $this->lang['index']['autocompleteType'][$autoType];

				}
				$autocompleteTypeFld->setPossibleValues($autoTypePossVal);
				$autocompleteTypeFld->showDefaultOption();
				$fieldset->addElement($autocompleteTypeFld);

				$controlledElements = [$nameFld, $categoryFld, $descriptionFld, $multiLineFld];

				if(!in_array($varDetails['type'], ['float','boolean'])) {
					$controlledElements[] = $autocompleteTypeFld;
				}

				// controlled elements
				$assistedInputFld->setControlledElements([true => $controlledElements],'disable');
			}
		}

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

		return $form;
	}

}

