<?php
namespace Modules\Admin\CurrencyExchange;

class View_Utils_FormAdmin extends \App\Mvc\View_Utils_FormAbstract
{

	/**
	 * (non-PHPdoc)
	 * @see \App\Mvc\View_Utils_FormAbstract::getForm()
	 */
	public function getForm()
	{
		// load the configurations for this form
		$form = \Engine\Form\General::create();
		$form->setId('currencyExchForm');

		// add container
		$container = \Engine\Form\Container\Fieldset::create($this->lang['index']['exchageRates']);
		$form->addContainer($container);

		$currencyExchDate = new \Engine\Form\Input\Date("Currency exchange date", 'currencyExchDate', $this->values['currencyExchDate']);
		$currencyExchDate->onChange("$('{$this->submitButtonName}').value=0;$('currencyExchDateChanged').value='1';$('currencyExchForm').submit();");
		$container->addElement($currencyExchDate);

		$currencyTypes = \App\Config\CurrencyTypes::getInstance();
		$currencyTypesList = [];
		$disabledCurrencyFields = [];
		$currencyRates = [];
		foreach(\App\Config\Currencies::getInstance()->getSimple() as $currencyCode) {
			foreach ($currencyTypes->getAll() as $currencyDetails) {
				if (!$currencyDetails['disabled']) {
					$currencyTypesList[$currencyCode][$currencyDetails['code']] = $currencyDetails['name'];
					if ($currencyDetails['parent']) {
						$disabledCurrencyFields[$currencyCode][$currencyDetails['code']] = $currencyDetails['code'];
					}

					if ($currencyDetails['currencies'][$currencyCode]) {
						if ($currencyDetails['currencies'][$currencyCode]['parent']) {
							$disabledCurrencyFields[$currencyCode][$currencyDetails['code']] = $currencyDetails['code'];
						}
					}

					$currencyRates[$currencyCode][$currencyDetails['code']] = $this->values['currencyRates'][$currencyCode][$currencyDetails['code']] ?: 0;
				}
			}
		}

		foreach(\App\Config\Currencies::getInstance()->getSimple() as $currencyCode) {
			if (\App\Config\Currencies::getInstance()->getDefaultCurrency() != $currencyCode) {
				$fld = new \App\Form\Input\MultipliedText($currencyCode, "currencyRates[".$currencyCode."]", $currencyRates[$currencyCode]);
				$fld->setMultiplier(count($currencyTypesList[$currencyCode]));
				$fld->setMultiplierLabelKeys(array_keys($currencyTypesList[$currencyCode]));
				$fld->setMultiplierLabels(array_values($currencyTypesList[$currencyCode]));
				$fld->setStyle('CSSClass', 'input-multipliedElementLabelTop');
				$fld->setDisabled(false, $disabledCurrencyFields[$currencyCode]);
				$container->addElement($fld);
			}
		}

		$fld_prevRates = new \Engine\Form\Button\Button($this->lang['index']['buttons']['importPrevExchRate']);
		$fld_prevRates->addEvent('onClick', "window.location=MVC.appUrl(MVC.CURRENT_MODULE, 'index', {'operation': 'importPrevExchRate', 'currencyExchDate': \$F('currencyExchDate')});");
		$container->addButton($fld_prevRates);

		$fld_extRates = new \Engine\Form\Button\Button($this->lang['index']['buttons']['importExtSystExchRate']);
		$fld_extRates->addEvent('onClick', "window.location=MVC.appUrl(MVC.CURRENT_MODULE, 'index', {'operation': 'importExtSystExchRate', 'currencyExchDate': \$F('currencyExchDate')});");
		$container->addButton($fld_extRates);

		$form->addElement(new \Engine\Form\Input\Hidden('', 'currencyExchDateChanged', 0));

		// submit button
		$form->addElement(new \Engine\Form\Input\Hidden(null, $this->submitButtonName, 1));
		$container->addButton(new \Engine\Form\Button\Submit($this->submitButtonName));

		return $form;
	}

}