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

class View_Utils_FormCloneBulk extends \App\Mvc\View_Utils_FormAbstract
{
	/**
	 *
	 * {@inheritDoc}
	 * @see \App\Mvc\View_Utils_FormAbstract::getForm()
	 */
	public function getForm()
	{
		$form = \Engine\Form\General::create();
		$filtersFieldset = \Engine\Form\Container\Fieldset::create($this->lang['cloneBulk']['fieldset']['filters']);
		$form->addContainer($filtersFieldset);

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


		if ($this->values['filterServiceCode']) {
			// VAT Type
			$filterVatTypes = [];
			$filterInvoiceVats = \App\Config\SupplierInvoice_Vats::getInstance()->getAll();
			$filterVats = ($this->values['filterServiceCode'] == 'notApplicable') ? $filterInvoiceVats['']['list'] : $filterInvoiceVats[$this->values['filterServiceCode']]['list'];
			if ($filterVats) {
				foreach ($filterVats as $vatName => $vatCode) {
					$filterVatTypes[$vatName] = $vatName;
				}
			}
			$filtersVatTypeFld = new \Engine\Form\Input\Select($this->lang['fields']['vatType'], 'filterVatType', $this->values['filterServiceChanged']? '':$this->values['filterVatType']);
			$filtersVatTypeFld->setMandatory();
			$filtersVatTypeFld->showDefaultOption();
			$filtersVatTypeFld->setDefaultOptionText($this->lang['insert']['defOptionVatType']);
			$filtersVatTypeFld->setPossibleValues(array_merge(['*' => '*'], $filterVatTypes));
			$filtersFieldset->addElement($filtersVatTypeFld);
		}

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

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


		if ($this->values['newServiceCode']) {
			// VAT Type
			$newVatTypes = [];
			$newInvoiceVats = \App\Config\SupplierInvoice_Vats::getInstance()->getAll();
			$newVats = ($this->values['newServiceCode'] == 'notApplicable') ? $newInvoiceVats['']['list'] : $newInvoiceVats[$this->values['newServiceCode']]['list'];
			if ($newVats) {
				foreach ($newVats as $vatName => $vatCode) {
					$newVatTypes[$vatName] = $vatName;
				}
			}
			$newVatTypeFld = new \Engine\Form\Input\Select($this->lang['fields']['vatType'], 'newVatType', $this->values['newServiceChanged']? '':$this->values['newVatType']);
			$newVatTypeFld->setMandatory();
			$newVatTypeFld->showDefaultOption();
			$newVatTypeFld->setDefaultOptionText($this->lang['insert']['defOptionVatType']);
			$newVatTypeFld->setPossibleValues(array_merge(['*' => '*'], $newVatTypes));
			$newFieldset->addElement($newVatTypeFld);
		}


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

		$label = new \Engine\Form\Input\Label();
		$label->setDetails($this->lang['cloneBulk']['label']['exportFields']);
		$label->setStyle("noLabel", true);
		$fieldsFieldset->addElement($label);

		$fieldCodes = \App\Config\ExportData_Fields::getInstance()->getFieldsAll('supplierInvoices');
		$activeFieldCodes = \App\Config\ExportData_Fields::getInstance()->getFieldsActive('supplierInvoices');
		$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('', 'filterServiceChanged', 0));
		$form->addElement(new \Engine\Form\Input\Hidden('', 'newServiceChanged', 0));
		$form->addElement(new \Engine\Form\Input\Hidden('', $this->submitButtonName, 1));
		return $form;
	}
}