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

class View_Utils_FormAdmin extends \App\Mvc\View_Utils_FormAbstract
{
	/**
	 *
	 * {@inheritDoc}
	 * @see \App\Mvc\View_Utils_FormAbstract::getForm()
	 */
	public function getForm()
	{
		$CFG = \Engine\Loader_Cfg::getInstance()->getGlobal();

		$form = \Engine\Form\General::create();
		$fieldset = \Engine\Form\Container\Fieldset::create($this->lang['insert']['criteriaFields']);
		$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(['*' => '*'], $servCodeFld->getPossibleValues()));

		if ($this->controller->getAction() == 'insert') {
			$servCodeFld->onChange("$('{$this->submitButtonName}').value=0;$('serviceChanged').value='1';this.form.submit();");
		} elseif ($this->controller->getAction() == 'update') {
			$servCodeFld->setDisabled();
		}
		$fieldset->addElement($servCodeFld);

		if ($this->values['serviceCode']) {

			// service calculation and location
			$servLocationCalculationFld = new \App\Form\Input\Select_LocationCalculation($this->lang['fields']['locationCalculation'], 'servLocationCalculation', $this->values['serviceChanged']? '':$this->values['servLocationCalculation']);
			$servLocationCalculationFld->setMandatory();
			$servLocationCalculationFld->showDefaultOption();
			$servLocationCalculationFld->setDefaultOptionText($this->lang['insert']['defOptionLocCalc']);
			$servLocationCalculationFld->setServiceCode($this->values['serviceCode']);

			$locCalcValues = array_merge(['*' => ['*|*' => '*']], $servLocationCalculationFld->getPossibleValues());
			foreach ($CFG['countriesCriteria'] as $location) {
				$locationName = $this->globalLang['locations']['countriesCriteria']['long'][$location];
				if ($locCalcValues[$locationName]) {
					$locCalcValues[$locationName] = array_merge([$location.'|*' => '*'], $locCalcValues[$locationName]);
				}
				else {
					$locCalcValues[$locationName][$location.'|*'] = '*';
				}
			}
			$servLocationCalculationFld->setPossibleValues($locCalcValues);
			if ($this->controller->getAction() == 'update') {
				$servLocationCalculationFld->setDisabled();
			}

			$fieldset->addElement($servLocationCalculationFld);

			$rowTypesList = ['price', 'commission', 'discount', 'serviceFee', 'tax', 'advance', 'penalty'];
			// invoice row type
			$rowTypes = [];
			foreach ($rowTypesList as $rowType) {
				$rowTypes[$rowType] = $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(array_merge(['*' => '*'], $rowTypes));
			$fieldset->addElement($rowTypeFld);
			if ($this->controller->getAction() == 'update') {
				$rowTypeFld->setDisabled();
			}

			$clientTypesList = ['individual', 'legalEntity'];
			// invoice client type
			$clientTypes = [];
			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();
			}

			// 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']);
			$currencyFld->setPossibleValues(array_merge(['*' => '*'], $currencyFld->getPossibleValues()));
			$fieldset->addElement($currencyFld);
			if ($this->controller->getAction() == 'update') {
				$currencyFld->setDisabled();
			}

			// invoiced in advance
			$advanceFld = new \Engine\Form\Input\Select($this->lang['fields']['advanceInvoice'], 'advanceInvoice', $this->values['advanceInvoice']);
			$advanceFld->setMandatory();
			$advanceFld->showDefaultOption();
			$advanceFld->setDefaultOptionText($this->lang['insert']['defOptionAdvance']);
			$advanceFld->setPossibleValues(['*'=>'*', '0'=>$this->globalLang['no'], '1'=>$this->globalLang['yes']]);
			$fieldset->addElement($advanceFld);
			if ($this->controller->getAction() == 'update') {
				$advanceFld->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('clientInvoices');
				foreach ($fieldCodes as $fieldCode => $fieldName) {
					// field value
					$valueFld = new \Engine\Form\Input\Text(\App\Config\ExportData_Fields::getInstance()->getFieldName('clientInvoices', $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);

			} elseif ($this->controller->getAction() == 'update') {
				$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('clientInvoices', $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('', 'serviceChanged', 0));
		} elseif ($this->controller->getAction() == 'update') {
			$form->addElement(new \Engine\Form\Input\Hidden('', 'serviceCode', $this->values['serviceCode']));
			$form->addElement(new \Engine\Form\Input\Hidden('', 'servLocationCalculation', $this->values['servLocationCalculation']));
			$form->addElement(new \Engine\Form\Input\Hidden('', 'rowType', $this->values['rowType']));
			$form->addElement(new \Engine\Form\Input\Hidden('', 'clientType', $this->values['clientType']));
			$form->addElement(new \Engine\Form\Input\Hidden('', 'currency', $this->values['currency']));
			$form->addElement(new \Engine\Form\Input\Hidden('', 'advanceInvoice', $this->values['advanceInvoice']));
			$form->addElement(new \Engine\Form\Input\Hidden('', 'field', $this->values['field']));
		}

		return $form;

	}
}