<?php

namespace Modules\Orders\OrdersList;


class View_Utils_FormFilters extends \App\Mvc\View_Utils_FormAbstract
{

	/**
	 * Creates the form for orders filter
	 * @see \App\Mvc\View_Utils_FormAbstract::getForm()
	 */
	public function getForm()
	{
		$fieldsValues = $this->values['listingParams']->getFilters();

		$_LANG = \Engine\Loader_Lang::getInstance()->getGlobal();

		// create form
		$form = \Engine\Form\Filters::create();
		$form->setColumns(4);
		$form->showResetButton(true);

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

		// reservationStatusOp
		$fld = new \Engine\Form\Input\Checkboxes($this->lang['index']['filters']['reservationStatusOp'], 'filters[reservationStatusOp]', $fieldsValues['reservationStatusOp']);
		$fld->setPossibleValues( [
				'RQ'=>($_LANG['reservations']['statusOpName']['RQ']?:'RQ'),
				'WL'=>($_LANG['reservations']['statusOpName']['WL']?:'WL'),
				'OK'=>($_LANG['reservations']['statusOpName']['OK']?:'OK'),
				'XX'=>($_LANG['reservations']['statusOpName']['XX']?:'XX'),
		]);
		$form->addElement($fld);

		// reservationStatusFin
		$fld = new \Engine\Form\Input\Checkboxes($this->lang['index']['filters']['reservationStatusFin'], 'filters[reservationStatusFin]', $fieldsValues['reservationStatusFin']);
		$fld->setPossibleValues( [
				'NI'=>($_LANG['reservations']['statusFinName']['NI']?:'NI'),
				'PI'=>($_LANG['reservations']['statusFinName']['PI']?:'PI'),
				'TI'=>($_LANG['reservations']['statusFinName']['TI']?:'TI'),
		]);
		$form->addElement($fld);

		// idReservation
		$idReservationTypes = [
			$this->lang['index']['filters']['idReservationTypes']['orderGroup'] => [
				'id'			=> $this->lang['index']['filters']['idReservationTypes']['id'],
				'code'			=> $this->lang['index']['filters']['idReservationTypes']['code'],
				'number'		=> $this->lang['index']['filters']['idReservationTypes']['number'],
			],
			$this->lang['index']['filters']['idReservationTypes']['serviceGroup'] => [
				'idService'		=> $this->lang['index']['filters']['idReservationTypes']['idService']],
			$this->lang['index']['filters']['idReservationTypes']['impOrderGroup'] => [
				'idImportedOrder' => $this->lang['index']['filters']['idReservationTypes']['idImpOrd'],
				'idImportedService' => $this->lang['index']['filters']['idReservationTypes']['idImpServ']
			]
		];
		$fld = new \Engine\Form\Input\ElementWithElements($this->lang['index']['filters']['idReservation']);
		$fld->addElement( \Engine\Form\Input\Text::create(null, 'filters[idReservation]', $fieldsValues['idReservation']), 'main' );
		$fld->addElement( \Engine\Form\Input\Select::create(null, 'filters[idReservationType]', $fieldsValues['idReservationType'])
				->setPossibleValues($idReservationTypes)
				, 'post');
		$fld->setStyle('renderMode', 'inline');
		$form->addElement($fld);

		// office and user
		$fld = new \App\Form\Input\Autocomplete_OfficeAndUser($this->lang['index']['filters']['idOffice'], 'filters[idOffice]', $fieldsValues['idOffice']);
		$form->addElement($fld);

		// idCustomer
		$fld = new \App\Form\Input\Autocomplete_Customer($this->lang['index']['filters']['idCustomer'], 'filters[customer]', $fieldsValues['customer']);
		$fld->setFilters('hideDuplicates', 1);
		$form->addElement($fld);

		// idCorporate
		$fld = new \App\Form\Input\Autocomplete_Corporate($this->lang['index']['filters']['idCorporate'], 'filters[corporate]', $fieldsValues['corporate']);
		$fld->setFilters('hideDuplicates', 1);
		$form->addElement($fld);

		// corporate holding
		$corporateHolding = new \App\Form\Input\Autocomplete_CorporateTrust($this->lang['index']['filters']['idCorporateTrust'], 'filters[idCorporateTrust]', $fieldsValues['idCorporateTrust']);
		$form->addElement($corporateHolding);

		// idExtSystem
		$fld = new \App\Form\Input\Select_OrdersExternalSystem($this->lang['index']['filters']['idExtSystem'], 'filters[idExtSystem]', $fieldsValues['idExtSystem']);
		$fld->showDefaultOption();
		$fld->setManualValue('manual', $this->lang['index']['filters']['extSystemManual']);
		$form->addElement($fld);

		// serviceCode
		$fld = new \App\Form\Input\Select_Service($this->lang['index']['filters']['serviceCode'], 'filters[serviceCode]', $fieldsValues['serviceCode']);
		$fld->showDefaultOption();
		$fld->setDefaultOptionText($this->lang['index']['filters']['anyService']);
		$form->addElement($fld);

		// supplier
		$fld = new \App\Form\Input\Autocomplete_Supplier($this->lang['index']['filters']['idSupplier'], 'filters[supplier]', $fieldsValues['supplier']);
		$fld->setFilters('hideDuplicates', 1);
		$form->addElement($fld);

		// supplier product
		$fld = new \App\Form\Input\Autocomplete_SupplierProduct($this->lang['index']['filters']['idSuppProduct'], 'filters[supplierProduct]', $fieldsValues['supplierProduct']);
		$form->addElement($fld);

		// idDestination
		$fld = new \App\Form\Input\Autocomplete_CountryCity($this->lang['index']['filters']['idDestination'], 'filters[idDestination]', $fieldsValues['idDestination']);
		$form->addElement($fld);


		// resEndDate
		$fld = new \Engine\Form\Input\DateInterval($this->lang['index']['filters']['resEndDate'], 'filters[resEndDate]', $fieldsValues['resEndDate']);
		$form->addElement($fld);


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

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

		// confirmation number
		$fld = new \Engine\Form\Input\TextWithElements($this->lang['index']['filters']['confirmationNumber'], 'filters[confirmationNumber]', $fieldsValues['confirmationNumber']);
		$fld->addElement(\Engine\Form\Input\Select::create("", "filters[confirmationNumberSearchMode]", $fieldsValues['confirmationNumberSearchMode'], [
			'starts' 	=> $this->lang['index']['filters']['confirmationNumberSearchMode']['starts'],
			'contains' 	=> $this->lang['index']['filters']['confirmationNumberSearchMode']['contains'],
			'exact' 	=> $this->lang['index']['filters']['confirmationNumberSearchMode']['exact'],
		]), 'post');
		$form->addElement($fld);

		// orderType
		$fld = new \App\Form\Input\Select_OrderType($this->lang['index']['filters']['resType'], 'filters[orderType]', $fieldsValues['orderType']);
		$fld->showDefaultOption();
		$fld->setDefaultOptionText( $this->lang['index']['filters']['all'] );
		$form->addElement($fld);

		// servicesType
		$fld = new \App\Form\Input\Select_OrderServicesType($this->lang['index']['filters']['orderServTypes'], 'filters[orderServType]', $fieldsValues['orderServType']);
		$fld->showDefaultOption();
		$fld->setDefaultOptionText( $this->lang['index']['filters']['all'] );
		$form->addElement($fld);

		// mice project and project type
		$fld = new \App\Form\Input\Autocomplete_MiceTypeAndProject($this->lang['index']['filters']['idMiceTypeAndProject'], 'filters[idMiceTypeAndProject]', $fieldsValues['idMiceTypeAndProject']);
		$form->addElement($fld);

		// keywords
		$fld = new \Engine\Form\Input\Text($this->lang['index']['filters']['keywords'], 'filters[keywords]', $fieldsValues['keywords']);
		$fld->setTooltip( $this->lang['index']['filters']['tooltips']['keywords'] );
		$form->addElement($fld);


		// ADVANCED ///////////////////////////////////////////////////////////////////////////////

		// serviceStartDate
		$fld = new \Engine\Form\Input\DateInterval($this->lang['index']['filters']['serviceStartDate'], 'filters[serviceStartDate]', $fieldsValues['serviceStartDate']);
		$form->addElement($fld, 'advanced');

		// serviceStatusOp
		$fld = new \Engine\Form\Input\Checkboxes($this->lang['index']['filters']['serviceStatusOp'], 'filters[serviceStatusOp]', $fieldsValues['serviceStatusOp']);
		$fld->setPossibleValues( [
				'NP'=>($_LANG['reservationServices']['statusOpName']['NP']?:'NP'),
				'PE'=>($_LANG['reservationServices']['statusOpName']['PE']?:'PE'),
				'OF'=>($_LANG['reservationServices']['statusOpName']['OF']?:'OF'),
				'OK'=>($_LANG['reservationServices']['statusOpName']['OK']?:'OK'),
				'XX'=>($_LANG['reservationServices']['statusOpName']['XX']?:'XX'),
		]);
		$form->addElement($fld, 'advanced');

		// serviceStatusFin
		$fld = new \Engine\Form\Input\Checkboxes($this->lang['index']['filters']['serviceStatusFin'], 'filters[serviceStatusFin]', $fieldsValues['serviceStatusFin']);
		$fld->setPossibleValues( [
				'NI'=>($_LANG['reservationServices']['statusFinName']['NI']?:'NI'),
				'PI'=>($_LANG['reservationServices']['statusFinName']['PI']?:'PI'),
				'TI'=>($_LANG['reservationServices']['statusFinName']['TI']?:'TI'),
		]);
		$form->addElement($fld, 'advanced');

		// flag
		$fld = new \App\Form\Input\OrderFlag($this->lang['index']['filters']['flag'], 'filters[flag]', $fieldsValues['flag']);
		$fld->showDefaultOption();
		$fld->setDefaultOptionText($this->lang['index']['filters']['allFlags']);
		$form->addElement($fld, 'advanced');

		// serviceEndDate
		$fld = new \Engine\Form\Input\DateInterval($this->lang['index']['filters']['serviceEndDate'], 'filters[serviceEndDate]', $fieldsValues['serviceEndDate']);
		$form->addElement($fld, 'advanced');

		// service responsible office and user
		$fld = new \App\Form\Input\Autocomplete_OfficeAndUser($this->lang['index']['filters']['idServiceOffice'], 'filters[idServiceOffice]', $fieldsValues['idServiceOffice']);
		$form->addElement($fld, 'advanced');

		// balancedServices
		$fld = new \Engine\Form\Input\Select($this->lang['index']['filters']['balancedServices'], 'filters[balancedServices]', $fieldsValues['balancedServices']);
		$fld->setPossibleValues( [
				'notBalanced'=>$this->lang['index']['filters']['balancedOptions']['notBalanced'],
				'balanced'	 =>$this->lang['index']['filters']['balancedOptions']['balanced'],
		]);
		$fld->showDefaultOption();
		$fld->setDefaultOptionText($this->lang['index']['filters']['all']);
		$form->addElement($fld, 'advanced');


		// id supplier invoice
		$fld = new \Engine\Form\Input\Text($this->lang['index']['filters']['idSupplierInvoice'], 'filters[idSupplierInvoice]', $fieldsValues['idSupplierInvoice']);
		$form->addElement($fld, 'advanced');

		// external reservation date
		$fld = new \Engine\Form\Input\DateInterval($this->lang['index']['filters']['extResDate'], 'filters[extResDate]', $fieldsValues['extResDate']);
		$form->addElement($fld, 'advanced');

		// idContactPerson
		$fld = new \App\Form\Input\Autocomplete_ContactPersonCorporate($this->lang['index']['filters']['idContactPerson'], 'filters[idContactPerson]', $fieldsValues['idContactPerson']);
		$form->addElement($fld, 'advanced');

		// agencyCostCenter
		$fld = new \Engine\Form\Input\Select($this->lang['index']['filters']['agencyCostCenter'], 'filters[agencyCostCenter]', $fieldsValues['agencyCostCenter']);
		$fld->setPossibleValues( \App\Config\AgencyCostCenters::getInstance()->getListActive() );
		$fld->showDefaultOption();
		$fld->setDefaultOptionText($this->lang['index']['filters']['all']);
		$form->addElement($fld, 'advanced');

		// remarks
		$fld = new \Engine\Form\Input\Text($this->lang['index']['filters']['remarks'], 'filters[remarks]', $fieldsValues['remarks']);
		$fld->setTooltip( $this->lang['index']['filters']['tooltips']['remarks'] );
		$form->addElement($fld, 'advanced');

		// offOptionDate
		$fld = new \Engine\Form\Input\DateInterval($this->lang['index']['filters']['offOptionDate'], 'filters[offOptionDate]', $fieldsValues['offOptionDate']);
		$form->addElement($fld, 'advanced');

		// responsible department
		$fld = new \App\Form\Input\Autocomplete_Department($this->lang['index']['filters']['responsibleDepartment'], 'filters[responsibleDepartment]', $fieldsValues['responsibleDepartment']);
		$form->addElement($fld, 'advanced');

		// add service
		$serviceAddDate = new \Engine\Form\Input\DateInterval($this->lang['index']['filters']['serviceAddDate'], 'filters[serviceAddDate]', $fieldsValues['serviceAddDate']);
		$form->addElement($serviceAddDate, 'advanced');

		// filterDocNumber
		$fld = new \Engine\Form\Input\ElementWithElements($this->lang['index']['filters']['filterDocNumber'], 'filters[filterDocNumber]', null);

		// get doc types (grouped)
		$docTypesActive = [];
		foreach( \App\Config\IssueDocuments::getInstance()->getOrderDocsList() as $docType=>$docName ) {
			$docTypesActive[ $this->lang['index']['filters']['filterDocNumbers']['optGroup']['order'] ] ['order|'.$docType] = $docName;
		}
		foreach( \App\Config\IssueDocuments::getInstance()->getServiceDocsList() as $docType=>$docName ) {
			$docTypesActive[ $this->lang['index']['filters']['filterDocNumbers']['optGroup']['service'] ] ['service|'.$docType] = $docName;
		}

		$fld->addElement( \Engine\Form\Input\Text::create(null, 'filters[filterDocNumber]', $fieldsValues['filterDocNumber']), 'main');
		$fld->addElement( \Engine\Form\Input\Select::create(null, 'filters[filterDocType]', $fieldsValues['filterDocType'])
				->showDefaultOption()
				->setDefaultOptionText($this->lang['index']['filters']['filterDocNumbers']['all'])
				->setPossibleValues($docTypesActive)
				, 'post');
		$fld->setStyle('renderMode', 'inline');
		$form->addElement($fld, 'advanced');

		//automatic/manually processed services
		$autoProcFilterServTypes = new \Engine\Form\Input\ElementWithElements($this->lang['index']['filters']['autoProcFilterServTypes'], null, null);
		$idAutoProcessRuleFilter= \Engine\Form\Input\Text::create(null, 'filters[idAutoProcessRule]', $fieldsValues['idAutoProcessRule']);
		$servTypesFilter = new \Engine\Form\Input\Select(null, 'filters[servTypes]', $fieldsValues['servTypes']);
		$servTypesFilter->setPossibleValues([
			'automatically' => $this->lang['index']['filters']['servTypes']['automatically'],
			'manually' => $this->lang['index']['filters']['servTypes']['manually'],
			'idRule' => $this->lang['index']['filters']['servTypes']['idRule'],
		]);
		$servTypesFilter->showDefaultOption();
		$servTypesFilter->setDefaultOptionText($this->lang['index']['filters']['all']);

		$autoProcFilterServTypes->addElement($servTypesFilter, 'main' );
		$autoProcFilterServTypes->addElement($idAutoProcessRuleFilter, 'post' );

		$autoProcFilterServTypes->setStyle('renderMode', 'inline');
		$form->addElement($autoProcFilterServTypes, 'advanced');

		// Groups
		$groups = \App\Config\CorporateGroups::getInstance()->getAll();
		if ($groups) {
			foreach ($groups as $idGroup=>$groupDetails) {
				if ($groupDetails['groupElements']) {
					$groupElement = new \Engine\Form\Input\Select($groupDetails['groupName'], "filters[idGroupElements][{$idGroup}]", $fieldsValues['idGroupElements'][$idGroup]);
					$groupElement->setPossibleValues($groupDetails['groupElements']);
					$groupElement->showDefaultOption(true);
					$form->addElement($groupElement, 'corporatesGroups');
				}
			}
		}

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

	}

}


