<?php

namespace Modules\Admin\Config\CurrencyTypes;

class View_Utils_FormAdmin extends \App\Mvc\View_Utils_FormAbstract
{

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

		// NEW FORM ///////////////////////////////////////////////////////////////////////////////////////
		$form = \Engine\Form\General::create();
		$form->setStyle('width', 250);

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


		// code
		$fld=new \Engine\Form\Input\Text($this->lang['form']['fields']['code']['label'], 'id', $this->values['id']);
		$fld->setMandatory();
		if ($this->controller->getAction()=='update') {
			$fld->setDisabled(true);
		}
		$fld->setValidatorParam('maxLength', 20);
		$fieldset->addElement($fld);

		// name
		$fld=new \Engine\Form\Input\Text($this->lang['form']['fields']['name']['label'], 'name', $this->values['name']);
		$fld->setMandatory();
		$fld->setValidatorParam('maxLength', 50);
		$fieldset->addElement($fld);

		if(\App\Config\CurrencyTypes::getAdmin()->getDefaultCurrencyType() != $this->values['id']) {

			//INPUT SELECT
			$inputSelectFormula=new \Engine\Form\Input\Select($this->lang['form']['fields']['formula']['label'], 'formulaType', $this->values['formulaType']);
			$inputSelectFormula->setPossibleValues([
				''=>		$this->lang['form']['fields']['formula']['options']['none'],
				'simple'=>	$this->lang['form']['fields']['formula']['options']['simple'],
				'complex'=>	$this->lang['form']['fields']['formula']['options']['complex']
				]
			);
			$fieldset->addElement($inputSelectFormula);



			// SIMPLE FORMULA
			$inputSimpleFormula=new \App\Form\Input\CurrencyTypeFormula($this->lang['form']['fields']['formula_simple']['label'], 'simpleFormula', $this->values['simpleFormula']);
			$inputSimpleFormula->setDefaultValue($this->values['simpleFormula']['parent']);
			$fieldset->addElement($inputSimpleFormula);

			// COMPLEX FORMULA
			$complexFormulasList=[];
			foreach (\App\Config\Currencies::getAdmin()->getAll() as $codeCurrency=>$valCurrency) {
				if ($codeCurrency!=\App\Config\Currencies::getInstance()->getDefaultCurrency()) {
					$inputComplexFormula[$codeCurrency]=new \App\Form\Input\CurrencyTypeFormula(sprintf($this->lang['form']['fields']['formula_complex']['label'],$codeCurrency), 'complexFormula['.$codeCurrency.']', $this->values['complexFormula'][$codeCurrency]);
					$inputSimpleFormula->setDefaultValue($this->values['complexFormula'][$codeCurrency]['parent']);
					$fieldset->addElement($inputComplexFormula[$codeCurrency]);
					$complexFormulasList[]=$inputComplexFormula[$codeCurrency];
				}
			}

			$inputSelectFormula->setControlledElements(
				[
					'simple'=> [$inputSimpleFormula],
					'complex'=>$complexFormulasList,
				],'hide'
			);
		}



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

		return $form;

	}
}