<?php

namespace Modules\Utils\RemarksAssistedInputs;

class View_Utils_FormAdmin extends \App\Mvc\View_Utils_FormAbstract
{
	/**
	 * (non-PHPdoc)
	 * @see \App\Mvc\View_Utils_FormAbstract::getForm()
	 */
	public function getForm()
	{
		global $_LANG;

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

		// get remark from all categories
		foreach ($this->values['groupedAssistedRemarks'] as $assistedRemarksCategory => $assistedRemarksVarList) {

			$fieldset = \Engine\Form\Container\Fieldset::create($assistedRemarksCategory);
			$fieldset->setCollapsible();

			// get remarks from current category
			foreach ($assistedRemarksVarList as $assistRemarkVar) {

				// add field label
				$fieldLabel = $assistRemarkVar['assistedInputDetails']['name'] ? $assistRemarkVar['assistedInputDetails']['name'] : $assistRemarkVar['name'];

				// check if remark input type is autocomplete
				if ($assistRemarkVar['assistedInputDetails']['autocompleteType']) {
					switch ($assistRemarkVar['assistedInputDetails']['autocompleteType']) {
						// check the types of autocomplete for input
						// supplier
						case 'supplier': {
							$field = new \App\Form\Input\Autocomplete_Supplier($fieldLabel, 'remarksValues['.$assistRemarkVar['name'].']', $this->values['remarksValues'][$assistRemarkVar['name']]);
							break;
						}

						// user
						case 'user': {
							$field = new \App\Form\Input\Autocomplete_User($fieldLabel, 'remarksValues['.$assistRemarkVar['name'].']', $this->values['remarksValues'][$assistRemarkVar['name']]);
							break;
						}

						// customer
						case 'customer': {
							$field = new \App\Form\Input\Autocomplete_Customer($fieldLabel, 'remarksValues['.$assistRemarkVar['name'].']', $this->values['remarksValues'][$assistRemarkVar['name']]);
							break;
						}

						// corporate
						case 'corporate': {
							$field  = new \App\Form\Input\Autocomplete_Corporate($fieldLabel, 'remarksValues['.$assistRemarkVar['name'].']', $this->values['remarksValues'][$assistRemarkVar['name']]);
							break;
						}

						// office
						case 'office': {
							$field = new \App\Form\Input\Autocomplete_Office($fieldLabel, 'remarksValues['.$assistRemarkVar['name'].']', $this->values['remarksValues'][$assistRemarkVar['name']]);
							break;
						}

						// city
						case 'city': {
							$field = new \App\Form\Input\Autocomplete_City($fieldLabel, 'remarksValues['.$assistRemarkVar['name'].']', $this->values['remarksValues'][$assistRemarkVar['name']]);
							break;
						}

						// document series
						case 'documentSeries': {
							$field = new \App\Form\Input\Autocomplete_DocumentSeries($fieldLabel, 'remarksValues['.$assistRemarkVar['name'].']', $this->values['remarksValues'][$assistRemarkVar['name']]);
							break;
						}

						// invalid autocomplete type
						default: {
							throw new \Exception("This autocomplete type (`{$assistRemarkVar['assistedInputDetails']['autocompleteType']}`) is not valid for remark variable type.");
						}
					}


				}
				else { // switch by all the other type of remarks
					switch ($assistRemarkVar['type']) {
						// currency type
						case 'currency': {
							$field = new \App\Form\Input\Select_Currency($fieldLabel, 'remarksValues['.$assistRemarkVar['name'].']', $this->values['remarksValues'][$assistRemarkVar['name']]);
							$field->showDefaultOption(false);
							break;
						}

						// select type
						case 'select': {
							$field = new \Engine\Form\Input\Select($fieldLabel, 'remarksValues['.$assistRemarkVar['name'].']', $this->values['remarksValues'][$assistRemarkVar['name']]);
							$optionList=[''=>'-'];
							foreach($assistRemarkVar['options'] as $option) {
								// check if some translation exists for this option
								if ($assistRemarkVar['assistedInputDetails']['selectOptions'][$option]) {
									$optionList[$option]=$assistRemarkVar['assistedInputDetails']['selectOptions'][$option];
								}
								else {
									$optionList[$option]=$option;
								}
							}
							$field->setPossibleValues($optionList);
							break;
						}

						// date type
						case 'date': {
							$field = new \Engine\Form\Input\Date($fieldLabel, 'remarksValues['.$assistRemarkVar['name'].']', $this->values['remarksValues'][$assistRemarkVar['name']]);
							$field->setValidatorParam('type', $assistRemarkVar['type']);
							break;
						}

						// boolean type
						case 'boolean': {
							$field = new \Engine\Form\Input\Radio($fieldLabel, 'remarksValues['.$assistRemarkVar['name'].']', $this->values['remarksValues'][$assistRemarkVar['name']]);
							$field->setPossibleValues([false => $_LANG['no'], true => $_LANG['yes']]);
							break;
						}

						// only default value type
						case 'blank': {
							$field = new \Engine\Form\Input\Radio($fieldLabel, 'remarksValues['.$assistRemarkVar['name'].']', $this->values['remarksValues'][$assistRemarkVar['name']]);
							$field->setPossibleValues([0 => $_LANG['no'], 1 => $_LANG['yes']]);
							break;
						}

						case 'string': {
							if ($assistRemarkVar['assistedInputDetails']['multiLine']) {
								$field = new \Engine\Form\Input\Textarea($fieldLabel, 'remarksValues['.$assistRemarkVar['name'].']', $this->values['remarksValues'][$assistRemarkVar['name']]);
							}
							else {
								$field = new \Engine\Form\Input\Text($fieldLabel, 'remarksValues['.$assistRemarkVar['name'].']', $this->values['remarksValues'][$assistRemarkVar['name']]);
							}

							$field->setValidatorParam('type', $assistRemarkVar['type']);

							if ($assistRemarkVar['minLength']) {
								$field->setValidatorParam('minLength', $assistRemarkVar['minLength']);
							}
							if ($assistRemarkVar['maxLength']) {
								$field->setValidatorParam('maxLength', $assistRemarkVar['maxLength']);
							}
							if ($assistRemarkVar['fixLength']) {
								$field->setValidatorParam('fixLength', $assistRemarkVar['fixLength']);
							}
							break;
						}

						case 'alnum':
						case 'digits': {
							$field = new \Engine\Form\Input\Text($fieldLabel, 'remarksValues['.$assistRemarkVar['name'].']', $this->values['remarksValues'][$assistRemarkVar['name']]);

							$field->setValidatorParam('type', $assistRemarkVar['type']);

							if ($assistRemarkVar['minLength']) {
								$field->setValidatorParam('minLength', $assistRemarkVar['minLength']);
							}
							if ($assistRemarkVar['maxLength']) {
								$field->setValidatorParam('maxLength', $assistRemarkVar['maxLength']);
							}
							if ($assistRemarkVar['fixLength']) {
								$field->setValidatorParam('fixLength', $assistRemarkVar['fixLength']);
							}
							break;
						}

						case 'float':
						case 'int': {
							$field = new \Engine\Form\Input\Text($fieldLabel, 'remarksValues['.$assistRemarkVar['name'].']', $this->values['remarksValues'][$assistRemarkVar['name']]);
							$field->setValidatorParam('type', $assistRemarkVar['type']);
							break;
						}

						default: {
							throw new \Exception("This type (`{$assistRemarkVar['type']}`) is not valid for remark variable type.");
						}
					}
				}

				// add field tooltip & details
				$fieldTooltip='';
				if ($assistRemarkVar['assistedInputDetails']['description']) {
					$fieldTooltip=$assistRemarkVar['assistedInputDetails']['description'];
					$field->setTooltip(trim($fieldTooltip));
				}

				$fieldDetails='';
				if ($assistRemarkVar['assistedInputDetails']['name']) {
					$fieldDetails=sprintf($this->lang['index']['messages']['rmVarName'], $assistRemarkVar['name']);
					$field->setDetails($fieldDetails);
				}

				$fieldset->addElement($field);
			}

			$form->addContainer($fieldset);
		}

		$form->addElement(new \Engine\Form\Input\Hidden(null, 'remarksBulk', $this->values['remarksBulk']));
		$form->addElement(new \Engine\Form\Input\Hidden(null, 'remarksType', $this->values['remarksType']));
		$form->addElement(new \Engine\Form\Input\Hidden(null, 'remarksFieldId', $this->values['remarksFieldId']));

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

		return $form;
	}
}