<?php

namespace Modules\Invoices\SupplierInvoices;


class View_Utils_FormFilters extends \App\Mvc\View_Utils_FormAbstract
{

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

		$supplierInvoiceCfg =  \Entity\SupplierInvoice::loadCFG();

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

		// STANDARD FILTERS -----------------------------------------------------------------------

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

		// invoiceStatus
		$invoiceStatus = new \Engine\Form\Input\Select($this->lang['index']['filters']['invoiceStatus'], 'filters[invoiceStatus]', $fieldsValues['invoiceStatus']);
		$invoiceStatusArr = [
			"" => $this->lang['index']['filters']['invoiceStatusValues']['all'],
			"notConfirmed" => $this->lang['index']['filters']['invoiceStatusValues']['notConfirmed'],
			"confirmed|" => $this->lang['index']['filters']['invoiceStatusValues']['confirmed'],
			"confirmed|n" => $this->lang['index']['filters']['invoiceStatusValues']['confirmed|n'],
			"confirmed|p" => $this->lang['index']['filters']['invoiceStatusValues']['confirmed|p'],
			"confirmed|t" => $this->lang['index']['filters']['invoiceStatusValues']['confirmed|t'],
		];
		$invoiceStatus->setPossibleValues($invoiceStatusArr);
		$form->addElement($invoiceStatus);

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

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


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

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

		// invoice currency
		$invoiceCurrency = new \App\Form\Input\Select_Currency($this->lang['index']['filters']['currency'], 'filters[currency]', $fieldsValues['currency']);
		$invoiceCurrency->showDefaultOption(true);
		$form->addElement($invoiceCurrency);

		// balancedRows
		$balancedRows = new \Engine\Form\Input\Select($this->lang['index']['filters']['balancedRows'], 'filters[balancedRows]', $fieldsValues['balancedRows']);
		$balancedRowsArr = [
			"all" => 			$this->lang['index']['filters']['balancedRowsValues']['all'],
			"balanced" => 		$this->lang['index']['filters']['balancedRowsValues']['yes'],
			"notBalanced" => 	$this->lang['index']['filters']['balancedRowsValues']['no'],
		];
		$balancedRows->setPossibleValues($balancedRowsArr);
		$form->addElement($balancedRows);


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

		// form of payment
		$fldFormOfPayement = new \App\Form\Input\Select_FormOfPayment($this->lang['index']['filters']['formOfPayment'], 'filters[formOfPayment]', $fieldsValues['formOfPayment']);
		$fldFormOfPayement->importFromConfig($supplierInvoiceCfg['formOfPayment']);
		$fldFormOfPayement->showDefaultOption(true);
		$form->addElement($fldFormOfPayement);

		// total value filter
		$fld = new \Engine\Form\Input\Text($this->lang['index']['filters']['totalValue'], 'filters[totalValue]', $fieldsValues['totalValue']);
		$fld->setTooltip($this->lang['index']['filters']['totalValueTooltip']);
		$form->addElement($fld);

		// blockedInvoices
		$blockedInvoices = new \Engine\Form\Input\Select($this->lang['index']['filters']['blockedInvoices'], 'filters[blockedInvoices]', $fieldsValues['blockedInvoices']);
		$blockedInvoicesArr = [
			"all" => 		$this->lang['index']['filters']['blockedInvoicesValues']['all'],
			"blocked" => 	$this->lang['index']['filters']['blockedInvoicesValues']['yes'],
			"notBlocked" => $this->lang['index']['filters']['blockedInvoicesValues']['no'],
		];
		$blockedInvoices->setPossibleValues($blockedInvoicesArr);
		$form->addElement($blockedInvoices);

		// order/service id
		$idReservationTypes = [
			$this->lang['index']['filters']['idReservation_group_reservation'] => [
				'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']['idReservation_group_service'] => [
				'idService' =>	$this->lang['index']['filters']['idReservationTypes']['idService'],
			],
		];
		$reservationId = new \Engine\Form\Input\ElementWithElements($this->lang['index']['filters']['idReservation'], 'filters[idReservation]');
		$reservationId->addElement( \Engine\Form\Input\Text::create(null, 'filters[idReservation]', $fieldsValues['idReservation']), 'main' );
		$reservationId->addElement( \Engine\Form\Input\Select::create(null, 'filters[idReservationType]', $fieldsValues['idReservationType'])
				->setPossibleValues($idReservationTypes)
				, 'post');
		$reservationId->setStyle('renderMode', 'inline');
		$form->addElement($reservationId);

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

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

		// blockableInvoices
		$blockableInvoices = new \Engine\Form\Input\Select($this->lang['index']['filters']['blockableInvoices'], 'filters[blockableInvoices]', $fieldsValues['blockableInvoices']);
		$blockableInvoicesArr = [
				"all" => 			$this->lang['index']['filters']['blockableInvoicesValues']['all'],
				"blockable" => 		$this->lang['index']['filters']['blockableInvoicesValues']['yes'],
				"notBlockable" => 	$this->lang['index']['filters']['blockableInvoicesValues']['no'],
		];
		$blockableInvoices->setPossibleValues($blockableInvoicesArr);
		$form->addElement($blockableInvoices);


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

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

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

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


		// SUPPLIER GROUPS ------------------------------------------------------------------------
		$groups = \App\Config\SupplierGroups::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, 'suppliersGroups');
				}
			}
		}

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

	}

}


