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

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

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

		// service code
		$servCodeFld = new \App\Form\Input\Select_Service($this->lang['fields']['serviceCode'], 'serviceCode',  $this->values['serviceCode']);
		$servCodeFld->showDefaultOption(true);
		$servCodeFld->setDefaultOptionText($this->lang['list']['filters']['searchAll']);
		$servCodeFld->setPossibleValues($servCodeFld->getPossibleValues());
		$criteriaFieldset->addElement($servCodeFld);

		// service location
		$locationFld = new \App\Form\Input\Select_CountryCriterion($this->lang['fields']['location'], 'location',  $this->values['location']);
		$locationFld->showDefaultOption(true);
		$locationFld->setDefaultOptionText($this->lang['list']['filters']['searchAll']);
		$locationFld->setPossibleValues($locationFld->getPossibleValues());
		$criteriaFieldset->addElement($locationFld);

		$rowTypesList = ['price', 'commission', 'discount', 'serviceFee', 'tax', 'advance', 'penalty'];
		// invoice row type
		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->showDefaultOption(true);
		$rowTypeFld->setDefaultOptionText($this->lang['list']['filters']['searchAll']);
		$rowTypeFld->setPossibleValues($rowTypes);
		$criteriaFieldset->addElement($rowTypeFld);

		$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->showDefaultOption(true);
		$clientTypeFld->setDefaultOptionText($this->lang['list']['filters']['searchAll']);
		$clientTypeFld->setPossibleValues($clientTypes);
		$criteriaFieldset->addElement($clientTypeFld);

		// invoice currency
		$currencyFld = new \App\Form\Input\Select_Currency($this->lang['fields']['currency'], 'currency',  $this->values['currency']);
		$currencyFld->showDefaultOption(true);
		$currencyFld->setDefaultOptionText($this->lang['list']['filters']['searchAll']);
		$currencyFld->setPossibleValues($currencyFld->getPossibleValues());
		$criteriaFieldset->addElement($currencyFld);

		// invoiced in advance
		$advanceFld = new \Engine\Form\Input\Select($this->lang['fields']['advanceInvoice'], 'advanceInvoice',  $this->values['advanceInvoice']);
		$advanceFld->showDefaultOption(true);
		$advanceFld->setDefaultOptionText($this->lang['list']['filters']['searchAll']);
		$advanceFld->setPossibleValues(['0'=>$this->globalLang['no'], '1'=>$this->globalLang['yes']]);
		$criteriaFieldset->addElement($advanceFld);

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

		$fieldCodes = \App\Config\ExportData_Fields::getInstance()->getFieldsAll('clientInvoices');
		$activeFieldCodes = \App\Config\ExportData_Fields::getInstance()->getFieldsActive('clientInvoices');
		$inactiveFieldCodesKeys = array_diff_key($fieldCodes, $activeFieldCodes);

		foreach ($activeFieldCodes as $fieldCode => $fieldName) {
			$fieldFld = new \Engine\Form\Input\Checkbox($fieldName, 'fields['.$fieldCode.']', $this->values['fields'][$fieldCode]);
			$fieldsFieldset->addElement($fieldFld);
		}
		foreach ($inactiveFieldCodesKeys as $fieldCode => $fieldDetails) {
			$fieldFld = new \Engine\Form\Input\Checkbox($fieldDetails['name'].$this->lang['form']['fields']['inactive'], 'fields['.$fieldCode.']', $this->values['fields'][$fieldCode]);
			$fieldsFieldset->addElement($fieldFld);
		}

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