<?php
namespace Modules\Documents\DocumentsList;

class View_Utils_FormFilters extends \App\Mvc\View_Utils_FormAbstract
{
	/**
	 * Creates the form for document series filters.
	 * @see \App\Mvc\View_Utils_FormAbstract::getForm()
	 */
	public function getForm()
	{
		$_LANG = \Engine\Loader_Lang::getInstance()->getGlobal();
		$fieldsValues = $this->values['listingParams']->getFilters();

		$form=\Engine\Form\Filters::create();
		$form->setColumns(2);

		// document date
		$docDate = new \Engine\Form\Input\DateInterval($this->lang['index']['filters']['docDate'], 'filters[docDate]', $fieldsValues['docDate']);
		$docDate
   			->setValidatorParam('denyOpenIntervals', true)
   			->setValidatorParam('maxDifference', $this->cfg['listingParams']['filters']['docDate']['maxDifference'])
   			->setMandatory();
		$form->addElement($docDate);

		// document type
		$docType = new \App\Form\Input\Select_DocumentType($this->lang['index']['filters']['docType'], 'filters[docType]', $fieldsValues['docType']);
		$form->addElement($docType);

		// doc number
		$docNumber = new \Engine\Form\Input\Text($this->lang['index']['filters']['docNumber'], 'filters[docNumber]', $fieldsValues['docNumber']);
		$form->addElement($docNumber);

		// doc id
		$idDoc = new \Engine\Form\Input\Text($this->lang['index']['filters']['idDoc'], 'filters[idDoc]', $fieldsValues['idDoc']);
		$form->addElement($idDoc);

		// pivot id
		$idPivot = new \Engine\Form\Input\Text($this->lang['index']['filters']['idPivot'], 'filters[idPivot]', $fieldsValues['idPivot']);
		$form->addElement($idPivot);

		// pivot type
		$pivot = new \Engine\Form\Input\Select($this->lang['index']['filters']['pivot'], 'filters[pivot]', $fieldsValues['pivot']);
		$pivot->setPossibleValues( [
			'invoice' => $_LANG['documentPivotName']['invoice'],
			'order' => $_LANG['documentPivotName']['order'],
			'service' => $_LANG['documentPivotName']['service'],
			'cashing' => $_LANG['documentPivotName']['cashing'],
			'payment' => $_LANG['documentPivotName']['payment'],
			'balance' => $_LANG['documentPivotName']['balance'],
			'cashBookDoc' => $_LANG['documentPivotName']['cashBookDoc'],
			'importedService' => $_LANG['documentPivotName']['importedService'],
			'importedDocument' => $_LANG['documentPivotName']['importedDocument'],
			] );
		$pivot->showDefaultOption();
		$form->addElement($pivot);

		// generation type
		$genType = new \Engine\Form\Input\Select($this->lang['index']['filters']['generationType'], 'filters[generationType]', $fieldsValues['generationType']);
		$genType->setPossibleValues( [
			'auto' => $this->lang['generationType']['auto'],
			'manual' => $this->lang['generationType']['manual'],
		] );
		$genType->showDefaultOption();
		$form->addElement($genType);

		// all docs type listing
		$availableMainDocTypes = array_keys( \App\Config\IssueDocuments::getInstance()->getInvoiceDocsList() );
		
		// document template
		$docTemplate = new \App\Form\Input\Select_DocumentTemplate($this->lang['index']['filters']['docTemplate'], 'filters[docTemplate]', $fieldsValues['docTemplate']);
		$docTemplate->showDefaultOption();
		$docTemplate->setDocTypeFilter($availableMainDocTypes);
		$form->addElement($docTemplate);
		
		// reset button
		$form->showResetButton(true);

		$form->setJsCallback('MVC.listing.installedHandlers["listing"].reloadAndResetListing');
		$form->loadMissingDefaultValues($this->values['listingParams']->getDefaultFilters());
		return $form;
	}
}