<?php
namespace Modules\Admin\ExportData\Cashings;

class View_Utils_FormAdmin extends \App\Mvc\View_Utils_FormAbstract
{
	/**
	 *
	 * {@inheritDoc}
	 * @see \App\Mvc\View_Utils_FormAbstract::getForm()
	 */
	public function getForm()
	{

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


		$docTypeFld = new \Engine\Form\Input\Select($this->lang['fields']['docType'], 'docType', $this->values['docType']);
		$docTypeFld->showDefaultOption();
		$docTypeFld->setMandatory();
		$docTypeFld->setDefaultOptionText($this->lang['insert']['defOptionDocType']);
		$docTypeFld->setPossibleValues(array_merge(["*"=>"*"], \App\Config\CashingDocuments::getInstance()->getList()));

		if ($this->controller->getAction() != 'insert') {
			$docTypeFld->setDisabled();
		}
		$fieldset->addElement($docTypeFld);


		$officeFld = new \App\Form\Input\Select_AgencyOffice($this->lang['fields']['idOffice'], 'idOffice', $this->values['idOffice']);
		$officeFld->setMandatory();
		$officeFld->showDefaultOption();
		$officeFld->setDefaultOptionText($this->lang['insert']['defOptionOffice']);
		$officeFld->setPossibleValues(["*"=>"*"]+$officeFld->getPossibleValues());
		$form->addElement($officeFld);
		if ($this->controller->getAction() == 'update') {
			$officeFld->setDisabled();
		}

		// CASHING client type
		$clientTypesList = ['individual', 'legalEntity'];
		// invoice client type
		foreach ($clientTypesList as $clientType) {
			$clientTypes[$clientType] = $this->lang['clientTypes'][$clientType];
		}
		$clientTypeFld = new \Engine\Form\Input\Select($this->lang['fields']['clientType'], 'clientType', $this->values['clientType']);
		$clientTypeFld->setMandatory();
		$clientTypeFld->showDefaultOption();
		$clientTypeFld->setDefaultOptionText($this->lang['insert']['defOptionClientType']);

		$clientTypeFld->setPossibleValues(array_merge(['*' => '*'], $clientTypes));
		$fieldset->addElement($clientTypeFld);
		if ($this->controller->getAction() == 'update') {
			$clientTypeFld->setDisabled();
		}

		// CASHING bank account
		$bankAccountFld = new \App\Form\Input\Select_AgencyBankAccount($this->lang['fields']['idBankAccount'], 'idBankAccount', $this->values['idBankAccount']);
		$bankAccountFld->setMandatory();
		$bankAccountFld->showDefaultOption();
		$bankAccountFld->setDefaultOptionText($this->lang['insert']['defOptionBankAccount']);
		$bankAccountFld->setPossibleValues(["*"=>"*"]+$bankAccountFld->getPossibleValues());
		$fieldset->addElement($bankAccountFld);
		if ($this->controller->getAction() == 'update') {
			$bankAccountFld->setDisabled();
		}

		// cashing currency
		$currencyFld = new \App\Form\Input\Select_Currency($this->lang['fields']['currency'], 'currency', $this->values['currency']);
		$currencyFld->setMandatory();
		$currencyFld->showDefaultOption();
		$currencyFld->setDefaultOptionText($this->lang['insert']['defOptionCurrency']);
		$currencyFld->setPossibleValues(array_merge(['*' => '*'], $currencyFld->getPossibleValues()));
		$fieldset->addElement($currencyFld);
		if ($this->controller->getAction() == 'update') {
			$currencyFld->setDisabled();
		}

		if ($this->controller->getAction() == 'insert') {
			$exportFieldsFieldset = \Engine\Form\Container\Fieldset::create($this->lang['insert']['exportFields']);

			// form description
			$label = new \Engine\Form\Input\Label();
			$label->setDetails($this->lang['insert']['label']['exportFields']);
			$label->setStyle("noLabel", true);
			$exportFieldsFieldset->addElement($label);

			$form->addContainer($exportFieldsFieldset);
			// field name
			$fieldCodes = \App\Config\ExportData_Fields::getInstance()->getFieldsActive('cashings');
			foreach ($fieldCodes as $fieldCode => $fieldName) {
				// field value
				$valueFld = new \Engine\Form\Input\Text(\App\Config\ExportData_Fields::getInstance()->getFieldName('cashings', $fieldCode), 'fields['.$fieldCode.']', $this->values['fields'][$fieldCode]);
				$exportFieldsFieldset->addElement($valueFld);
			}

			$newRuleFieldset = \Engine\Form\Container\Fieldset::create($this->lang['insert']['addNewRule']);
			$addNewRule = new \Engine\Form\Input\Checkbox($this->lang['insert']['addNewRule'], 'addNewRule', $this->values['addNewRule']);
			$newRuleFieldset->addElement($addNewRule);
			$form->addContainer($newRuleFieldset);
		}
		else {
			$exportFieldsFieldset = \Engine\Form\Container\Fieldset::create($this->lang['insert']['exportFields']);
			$form->addContainer($exportFieldsFieldset);
			$valueFld = new \Engine\Form\Input\Text(\App\Config\ExportData_Fields::getInstance()->getFieldName('cashings', $this->values['field']), 'fields['.$this->values['field'].']', $this->values['fields'][$this->values['field']]);
			$exportFieldsFieldset->addElement($valueFld);
		}



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

		if ($this->controller->getAction() != 'insert') {
			$form->addElement(new \Engine\Form\Input\Hidden('', 'docType', $this->values['docType']));
			$form->addElement(new \Engine\Form\Input\Hidden('', 'idOffice', $this->values['idOffice']));
			$form->addElement(new \Engine\Form\Input\Hidden('', 'clientType', $this->values['clientType']));
			$form->addElement(new \Engine\Form\Input\Hidden('', 'idBankAccount', $this->values['idBankAccount']));
			$form->addElement(new \Engine\Form\Input\Hidden('', 'currency', $this->values['currency']));
			$form->addElement(new \Engine\Form\Input\Hidden('', 'field', $this->values['field']));
		}

		return $form;

	}


}