<?php
namespace Modules\Documents\ImportedDocuments;

class View_Utils_FormInsert extends \App\Mvc\View_Utils_FormAbstract
{
	/**
	 * Create the form for manual insert an imported document
	 * @see \App\Mvc\View_Utils_FormAbstract::getForm()
	 */
	public function getForm()
	{

		$CFG = \Engine\Loader_Cfg::getInstance()->getGlobal();

		$importedDocument = \Entity\ImportedDocument::select($this->values['id']);
		$externalSystemCode = \Entity\ImportedDocumentsSystem::select($importedDocument->idExternalSystem)->code;
		if ($externalSystemCode != $CFG['Documents']['ImportedDocuments']['fields']['defaultSystem']) {
		    $isExternalDocument = true;
		} else {
		    $isExternalDocument = false;
		}
		if ($importedDocument->status!='new') {
		    $isProcessedDocument = true;
		} else {
		    $isProcessedDocument = false;
		}

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

		// document category
		$docCategory = new \Engine\Form\Input\Select($this->lang['insert']['form']['docCategory'], 'docCategory', $this->values['docCategory']);
		$docCategoryValues = [
			'cashing' => $this->lang['index']['docCategory']['cashing'],
			'payment' => $this->lang['index']['docCategory']['payment'],
		];
		$docCategory->setPossibleValues($docCategoryValues);
		$docCategory->showDefaultOption();
		$docCategory->setMandatory();
		if ($this->controller->getAction() == 'update') {
			$docCategory->setDisabled();
		}
		$form->addElement($docCategory);

		$docTypeCashing = new \App\Form\Input\Select_CashingDocType($this->lang['insert']['form']['docType'], 'docTypeCashing', $this->values['docTypeCashing']);
		$docTypeCashing->showDefaultOption();
		$docTypeCashing->setMandatory();
		if ($this->controller->getAction()=='update' && ($isExternalDocument || $isProcessedDocument)) {
		    $docTypeCashing->setDisabled();
		}
		$form->addElement($docTypeCashing);

		// document type for payment
		$docTypePayment = new \App\Form\Input\Select_PaymentDocType($this->lang['insert']['form']['docType'], 'docTypePayment', $this->values['docTypePayment']);
		$docTypePayment->showDefaultOption();
		$docTypePayment->setMandatory();
		if ($this->controller->getAction()=='update' && ($isExternalDocument || $isProcessedDocument)) {
		    $docTypePayment->setDisabled();
		}
		$form->addElement($docTypePayment);

		// cashing client
		$clientType = new \Engine\Form\Input\Select($this->lang['insert']['form']['clientType'], 'clientType', $this->values['clientType']);
		$clientType->setMandatory();
		$clientType->showDefaultOption();
		if ($this->controller->getAction()=='update' && ($isExternalDocument || $isProcessedDocument)) {
		    $clientType->setDisabled();
		}
		$clientType->setPossibleValues([
			'legalEnt' 		=> $this->lang['insert']['form']['legalEnt'],
			'individual' 	=> $this->lang['insert']['form']['individual'],
		]);
		$form->addElement($clientType);

		// corporate
		$corporate = new \App\Form\Input\Autocomplete_Corporate($this->lang['insert']['form']['corporate'], 'corporate', $this->values['corporate']);
		$corporate->setMandatory();
		$corporate->addEvent('onChange', "FormField.setValue(null, 'Engine/Form/Input/Autocomplete', null, 'corporateBankAccount', {
			displayValue: '',
			value: ''});");
		if ($this->controller->getAction()=='update' && ($isExternalDocument || $isProcessedDocument)) {
		    $corporate->setDisabled();
		}
		$form->addElement($corporate);

		// corporate bank account
		$corporateBankAccount = new \App\Form\Input\Autocomplete_CorporateBankAccount($this->lang['insert']['form']['corporateBankAccount'], 'corporateBankAccount', $this->values['corporateBankAccount']);
		$corporateBankAccount->setFilters('idCompany', $corporate);
		if ($this->controller->getAction()=='update' && ($isExternalDocument || $isProcessedDocument)) {
		    $corporateBankAccount->setDisabled();
		}
		$form->addElement($corporateBankAccount);

		// customer
		$customer = new \App\Form\Input\Autocomplete_Customer($this->lang['insert']['form']['customer'], 'customer', $this->values['customer']);
		$customer->setMandatory();
		if ($this->controller->getAction()=='update' && ($isExternalDocument || $isProcessedDocument)) {
		    $customer->setDisabled();
		}
		$form->addElement($customer);

		// supplier
		$supplier = new \App\Form\Input\Autocomplete_Supplier($this->lang['insert']['form']['supplier'], 'supplier', $this->values['supplier']);
		$supplier->setMandatory();
		$supplier->addEvent('onChange', "FormField.setValue(null, 'Engine/Form/Input/Autocomplete', null, 'supplierBankAccount', {
			displayValue: '',
			value: ''});");
		if ($this->controller->getAction()=='update' && ($isExternalDocument || $isProcessedDocument)) {
		    $supplier->setDisabled();
		}
		$form->addElement($supplier);

		// supplier bank account
		$supplierBankAccount = new \App\Form\Input\Autocomplete_SupplierBankAccount($this->lang['insert']['form']['supplierBankAccount'], 'supplierBankAccount', $this->values['supplierBankAccount']);
		$supplierBankAccount->setFilters('idSupplier', $supplier);
		if ($this->controller->getAction()=='update' && ($isExternalDocument || $isProcessedDocument)) {
		    $supplierBankAccount->setDisabled();
		}
		$form->addElement($supplierBankAccount);

		// doc number
		$docNumber = new \Engine\Form\Input\Text($this->lang['insert']['form']['docNumber'], 'docNumber', $this->values['docNumber']);
		$docNumber->setMandatory($this->cfg['fields']['docNumber']=='mandatory');
		if ($this->controller->getAction()=='update' && ($isExternalDocument || $isProcessedDocument)) {
		    $docNumber->setDisabled();
		}
		$form->addElement($docNumber);

		// doc date
		$docDate = new \Engine\Form\Input\Date($this->lang['insert']['form']['docDate'], 'docDate', $this->values['docDate']);
		$docDate->setMandatory();
		if ($this->controller->getAction()=='update' && ($isExternalDocument || $isProcessedDocument)) {
		    $docDate->setDisabled();
		}
		$form->addElement($docDate);

		// value
		$value = new \App\Form\Input\Price($this->lang['insert']['form']['value'], 'value', $this->values['value']);
		$value->setMandatory();
		if ($this->controller->getAction()=='update' && ($isExternalDocument || $isProcessedDocument)) {
		   $value->setDisabled();
		}
		$form->addElement($value);

		// agency bank account
		$agencyBankAccount = new \App\Form\Input\Select_AgencyBankAccount($this->lang['insert']['form']['agencyBankAccount'], 'idAgencyBankAccount', $this->values['idAgencyBankAccount']);
		$agencyBankAccount->setMandatory($this->cfg['fields']['agencyBankAccount']=='mandatory');
		$agencyBankAccount->showDefaultOption();
		$form->addElement($agencyBankAccount);

		// comments
		$comments = new \Engine\Form\Input\Textarea($this->lang['insert']['form']['comments'], 'comments', $this->values['comments']);
		$form->addElement($comments);

		// explanations
		$explanations = new \Engine\Form\Input\Textarea($this->lang['insert']['form']['explanations'], 'explanations', $this->values['explanations']);
		$form->addElement($explanations);

		// remarks
		$remaksCashing = new \App\Form\Input\Remarks($this->lang['insert']['form']['remarks'], 'remarksCashing', $this->values['remarksCashing']);
		$remaksCashing->setRemarksType('invoiceCashing');
		$form->addElement($remaksCashing);

		$remaksPayment = new \App\Form\Input\Remarks($this->lang['insert']['form']['remarks'], 'remarksPayment', $this->values['remarksPayment']);
		$remaksPayment->setRemarksType('supplierInvoicePayment');
		$form->addElement($remaksPayment);

		// entities
		$entityCashing = new \Engine\Form\Input\Select($this->lang['insert']['form']['entity'], 'entity', $this->values['entity']);
		$entityCashing->showDefaultOption();
		$entityCashing->setPossibleValues([
			'service' 			=> $this->lang['insert']['entity']['service'],
			'importedService' 	=> $this->lang['insert']['entity']['importedService'],
			'invoice' 			=> $this->lang['insert']['entity']['invoice'],
			'supplierInvoice' 	=> $this->lang['insert']['entity']['supplierInvoice'],
		]);
		$entityCashing->setTooltip($this->lang['insert']['tooltips']['entity']);
		if ($this->controller->getAction()=='update' && ($isExternalDocument || $isProcessedDocument)) {
		    $entityCashing->setDisabled();
		}
		$form->addElement($entityCashing);

		// entity ids
		$entityIds = new \Engine\Form\Input\Cloned_Text($this->lang['insert']['form']['entityIds'], 'entityIds', $this->values['entityIds']);
		$entityIds->setTooltip($this->lang['insert']['tooltips']['entityIds']);
		if ($this->controller->getAction()=='update' && ($isExternalDocument || $isProcessedDocument)) {
		    $entityIds->setDisabled();
		}
		$form->addElement($entityIds);

		// redirect ro process imported document
		if ($this->controller->getAction() == 'insert') {
			$processImpDocument = new \Engine\Form\Input\Checkbox($this->lang['insert']['form']['processImpDocument'], 'processImpDocument', $this->values['processImpDocument']);
			$form->addElement($processImpDocument);
		}

		// controlled elements
		$docCategory->setControlledElements( [
			'cashing'=>[
			    $docTypeCashing, $remaksCashing,
				$clientType->setControlledElements( [
					'legalEnt'	=> [$corporate, $corporateBankAccount],
					'individual'=> [$customer]
				]),
			],
		    'payment'=>[ $docTypePayment, $supplier, $supplierBankAccount, $remaksPayment],
		] );


		// hidden elements that can be disabled
		if ($this->controller->getAction() == 'update') {
			$form->addElement(new \Engine\Form\Input\Hidden(null, 'docCategory', $this->values['docCategory']));
			if ($docTypeCashing->getDisabled()) {
				$form->addElement(new \Engine\Form\Input\Hidden(null, 'docTypeCashing', $this->values['docTypeCashing']));
			}
			if ($docTypePayment->getDisabled()) {
				$form->addElement(new \Engine\Form\Input\Hidden(null, 'docTypePayment', $this->values['docTypePayment']));
			}
			if ($clientType->getDisabled()) {
				$form->addElement(new \Engine\Form\Input\Hidden(null, 'clientType', $this->values['clientType']));
				$form->addElement(new \Engine\Form\Input\Hidden(null, 'corporate[id]', $this->values['corporate']['id']));
				$form->addElement(new \Engine\Form\Input\Hidden(null, 'customer[id]', $this->values['customer']['id']));
				$form->addElement(new \Engine\Form\Input\Hidden(null, 'supplier[id]', $this->values['supplier']['id']));
			}
			if ($docNumber->getDisabled()) {
				$form->addElement(new \Engine\Form\Input\Hidden(null, 'docNumber', $this->values['docNumber']));
			}
			if ($docDate->getDisabled()) {
				$form->addElement(new \Engine\Form\Input\Hidden(null, 'docDate', $this->values['docDate']));
			}
			if ($value->getDisabled()) {
				$form->addElement(new \Engine\Form\Input\Hidden(null, 'value[value]', $this->values['value']['value']));
				$form->addElement(new \Engine\Form\Input\Hidden(null, 'value[currency]', $this->values['value']['currency']));
			}
			if ($entityCashing->getDisabled()) {
				$form->addElement(new \Engine\Form\Input\Hidden(null, 'entity', $this->values['entity']));
				if ($this->values['entityIds']) {
					foreach ($this->values['entityIds'] as $idx=>$value) {
						$form->addElement(new \Engine\Form\Input\Hidden(null, 'entityIds['.($idx+1).']', $value));
					}
				}
			}
		}

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

		return $form;
	}
}