<?php

namespace Modules\Partners\Suppliers;


class View_Utils_FormFilters extends \App\Mvc\View_Utils_FormAbstract
{

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

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

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

		// supplier alias/name
		$fld = new \Engine\Form\Input\TextWithElements($this->lang['index']['filters']['name'], "filters[nameSearchText]", $fieldsValues['nameSearchText']);
		$fld->addElement( \Engine\Form\Input\Select::create(null, "filters[nameSearchMode]", $fieldsValues['nameSearchMode'], [
			'starts' 	=> $this->lang['index']['filters']['searchMode']['starts'],
			'contains' 	=> $this->lang['index']['filters']['searchMode']['contains'],
			'exact' 	=> $this->lang['index']['filters']['searchMode']['exact'],
		]), 'post');
		$form->addElement($fld);

		// category
		$fld = new \Engine\Form\Input\Select($this->lang['index']['filters']['category'], 'filters[category]', $fieldsValues['category']);
		$possibleValues = [];
		foreach ($this->globalCfg['suppliers']['possibleCategories'] as $category) {
			$possibleValues[$category] = ($this->globalLang['generalData']['suppliers']['possibleCategories'][$category]?:$category);
		}
		$fld->setPossibleValues($possibleValues)
			->showDefaultOption();
		$form->addElement($fld);

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

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

		// ------------

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

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

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

		// duplicate status
		$fld = new \Engine\Form\Input\Select($this->lang['index']['filters']['duplicateStatus'], 'filters[duplicateStatus]', $fieldsValues['duplicateStatus']);
		$fld->setPossibleValues([
			'all' => 				$this->lang['index']['filters']['duplicate']['all'],
			'duplicated' => 		$this->lang['index']['filters']['duplicate']['yes'],
			'notDuplicated' => 		$this->lang['index']['filters']['duplicate']['no']
		]);
		$fld->setTooltip($this->lang['index']['filters']['duplicate']['tooltip']);
		$form->addElement($fld);

		// ------------

		// vatRegistered
		$fld = new \Engine\Form\Input\Select($this->lang['index']['filters']['vatRegistered'], 'filters[vatRegistered]', $fieldsValues['vatRegistered']);
		$fld->showDefaultOption();
		$fld->setPossibleValues([
			'n' => $_LANG['no'],
			'y' => $_LANG['yes'],
		]);
		$form->addElement($fld);

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

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

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

		// ------------

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


		// 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;
	}
}
