<?php
namespace Modules\Documents\ImportedDocuments\BulkDistributeImportedDocs;

class View_Utils_FormAdmin extends \App\Mvc\View_Utils_FormAbstract
{
	/**
	 * (non-PHPdoc)
	 * @see App\Mvc.View_Utils_FormAbstract::getForm()
	 */
	public function getForm()
	{
		$form = \Engine\Form\General::create();

		$form->addContainer(\Engine\Form\Container\Fieldset::create());

		$clientName = '';
		if ($this->values['idCorporate']) {
			$corporateEnt = \Entity\Corporate::select($this->values['idCorporate']);
			$clientName = $corporateEnt->companyFull;
		}
		elseif ($this->values['idCustomer']) {
			$customerEnt = \Entity\Customer::select($this->values['idCustomer']);
			$clientName = $customerEnt->nameStandard;
		}
		elseif ($this->values['idSupplier']) {
			$supplierEnt = \Entity\Supplier::select($this->values['idSupplier']);
			$clientName = $supplierEnt->companyFull;
		}

		$label = new \Engine\Form\Input\Label();
		if ($this->values['entityType'] == 'clientInvoice') {
			$label->setDetails(sprintf($this->lang['index']['label']['invoiceData'], $this->values['invValue'], $this->values['invCurrency'], $clientName));
		}
		elseif ($this->values['entityType'] == 'supplierInvoice') {
			$label->setDetails(sprintf($this->lang['index']['label']['supplierInvoiceData'], $this->values['invValue'], $this->values['invCurrency'], $clientName));
		}

		$label->setStyle("noLabel", true);
		$form->addElement($label);

		$bulkOperationFld = new \Engine\Form\Input\Select($this->lang['index']['bulkOperation'], 'bulkOperation', $this->values['bulkOperation']);
		$bulkOperationFld->setMandatory();
		$bulkOperationFld->showDefaultOption();
		$bulkOperationFld->setPossibleValues([
			'existingImpDoc' => $this->lang['index']['bulkOperationType']['existingImpDoc'],
			'newImpDoc' => $this->lang['index']['bulkOperationType']['newImpDoc']
		]);
		$bulkOperationFld->setDetails($this->lang['index']['bulkOperationDetails']);
		$form->addElement($bulkOperationFld);

		$importedDocsPossibleValues = ['manual' => $this->lang['index']['manualInput']];
		if ($this->values['idImportedDocSelectList']) {
			foreach ($this->values['idImportedDocSelectList'] as $impDocId) {
				$impDocEnt = \Entity\ImportedDocument::select($impDocId);
				$importedDocsPossibleValues[$impDocId] = $impDocEnt->docNumber.' ('.\Engine\DateTime::create($impDocEnt->docDate)->formatDateStd().') - '.$impDocEnt->docValue.' '.$impDocEnt->docCurrency.' [#'.$impDocId.']';
			}
		}

		$importedDocsList = new \Engine\Form\Input\Select($this->lang['index']['idImportedDocSelect'], 'idImportedDocSelect', $this->values['idImportedDocSelect']);
		$importedDocsList->setPossibleValues($importedDocsPossibleValues);
		$importedDocsList->setMandatory();
		$importedDocsList->showDefaultOption();
		$form->addElement($importedDocsList);

		$importedDocId = new \Engine\Form\Input\Text($this->lang['index']['idImportedDocManual'], 'idImportedDocManual', $this->values['idImportedDocManual']);
		$importedDocId->setMandatory();
		$form->addElement($importedDocId);

		$processImpDocument = new \Engine\Form\Input\Checkbox($this->lang['index']['processImpDocument'], 'processImpDocument', $this->values['processImpDocument']);
		$form->addElement($processImpDocument);

		// controlled elements
		$bulkOperationFld->setControlledElements(
			[
				'existingImpDoc' => [$importedDocsList],
			]
		);

		$importedDocsList->setControlledElements(
			[
				'manual' => [$importedDocId]
			]
		);



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

		return $form;
	}
}
