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

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

		$activeFieldsWithValue = [];
		$activeFieldsNoValue = [];
		$inactiveFieldsWithValue = [];
		$fieldCodes = \App\Config\ExportData_Fields::getInstance()->getFieldsAll('supplierInvoices');
		foreach ($fieldCodes as $fieldCode => $fieldDetails) {
			if ($fieldDetails['active']) {
				if (strlen((string)$this->values['fieldsValues'][$fieldCode])!=0) {
					$activeFieldsWithValue[$fieldCode] = $fieldDetails['name'];
				} else {
					$activeFieldsNoValue[$fieldCode] = $fieldDetails['name'];
				}
			} else {
				if (strlen((string)$this->values['fieldsValues'][$fieldCode])!=0) {
					$inactiveFieldsWithValue[$fieldCode] = $fieldDetails['name'];
				}
			}
		}

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

		// service code
		$servCodeFld = new \App\Form\Input\Select_Service($this->lang['fields']['serviceCode'], 'serviceCode',  $this->values['serviceCode']);
		$servCodeFld->showDefaultOption();
		$servCodeFld->setDefaultOptionText($this->lang['insert']['defOptionServCode']);
		$servCodeFld->setMandatory();
		$servCodeFld->setPossibleValues(array_merge(['notApplicable' => $this->lang['notApplicable']], $servCodeFld->getPossibleValues()));
		$servCodeFld->onChange("$('{$this->submitButtonName}').value=0;$('serviceChanged').value='1';this.form.submit();");
		$fieldset->addElement($servCodeFld);


		if ($this->values['serviceCode']) {
			// invoice rowType
			$rowTypesList = ['notApplicable', 'price', 'advance', 'discount', 'tax', 'penalty'];
			foreach ($rowTypesList as $rowType) {
				$rowTypes[$rowType] = ($rowType == 'notApplicable') ? $this->lang['notApplicable'] : $this->lang['rowTypes'][$rowType];
			}
			$rowTypeFld = new \Engine\Form\Input\Select($this->lang['fields']['rowType'], 'rowType', $this->values['rowType']);
			$rowTypeFld->setMandatory();
			$rowTypeFld->showDefaultOption();
			$rowTypeFld->setDefaultOptionText($this->lang['insert']['defOptionRowType']);
			$rowTypeFld->setPossibleValues($rowTypes);
			$fieldset->addElement($rowTypeFld);

			// location
			$locationFld = new \App\Form\Input\Select_CountryCriterion($this->lang['fields']['location'], 'location', $this->values['location']);
			$locationFld->setMandatory();
			$locationFld->showDefaultOption();
			$locationFld->setDefaultOptionText($this->lang['insert']['defOptionLocation']);
			$locationFld->setPossibleValues(array_merge(['notApplicable' => $this->lang['notApplicable']], $locationFld->getPossibleValues()));
			$fieldset->addElement($locationFld);

			// VAT Type
			$vatTypes = [];
			$invoiceVats = \App\Config\SupplierInvoice_Vats::getInstance()->getAll();
			$vats = ($this->values['serviceCode'] == 'notApplicable') ? $invoiceVats['']['list'] : $invoiceVats[$this->values['serviceCode']]['list'];
			if ($vats) {
				foreach ($vats as $vatName => $vatCode) {
					$vatTypes[$vatName] = $vatName;
				}
			}

			$vatTypeFld = new \Engine\Form\Input\Select($this->lang['fields']['vatType'], 'vatType', $this->values['serviceChanged']? '':$this->values['vatType']);
			$vatTypeFld->setMandatory();
			$vatTypeFld->showDefaultOption();
			$vatTypeFld->setDefaultOptionText($this->lang['insert']['defOptionVatType']);
			$vatTypeFld->setPossibleValues( $vatTypes);
			$fieldset->addElement($vatTypeFld);

			// invoice 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']);
			$fieldset->addElement($currencyFld);
		}

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

		// active fields with and without value
		$exportFieldsActiveFieldset = \Engine\Form\Container\Fieldset::create($this->lang['insert']['exportFieldsActive']);
		$form->addContainer($exportFieldsActiveFieldset);
		foreach ($activeFieldsWithValue as $fieldCode => $fieldName) {
			$valueFld = new \Engine\Form\Input\Text($fieldName, 'fieldsValues['.$fieldCode.']', $this->values['fieldsValues'][$fieldCode]);
			$valueFld->setDisabled();
			$exportFieldsActiveFieldset->addElement($valueFld);
		}

		foreach ($activeFieldsNoValue as $fieldCode => $fieldName) {
			$valueFld = new \Engine\Form\Input\Text($fieldName, 'fieldsValues['.$fieldCode.']');
			$valueFld->setDisabled();
			$exportFieldsActiveFieldset->addElement($valueFld);
		}
		$exportFieldsActiveFieldset->setCollapsible();

		// inactive fields with value
		if ($inactiveFieldsWithValue) {
			$exportFieldsInactiveFieldset = \Engine\Form\Container\Fieldset::create($this->lang['insert']['exportFieldsInactive']);
			$form->addContainer($exportFieldsInactiveFieldset);
			foreach ($inactiveFieldsWithValue as $fieldCode => $fieldName) {
				$valueFld = new \Engine\Form\Input\Text($fieldName, 'fieldsValues['.$fieldCode.']', $this->values['fieldsValues'][$fieldCode]);
				$valueFld->setDisabled();
				$exportFieldsInactiveFieldset->addElement($valueFld);
			}
			$exportFieldsInactiveFieldset->setCollapsible();
			$exportFieldsInactiveFieldset->setCollapsed();
		}

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