<?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();

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

		$fld = new \Engine\Form\Input\TextWithElements($this->lang['index']['filters']['text'], "filters[text]", $fieldsValues['text']);
		$fld->addElement($f1 = \Engine\Form\Input\Select::create("", "filters[searchField]", $fieldsValues['searchField'], [
			'supp.alias' => $this->lang['index']['filters']['searchFields']['alias'],
			'supp.company' => $this->lang['index']['filters']['searchFields']['company'],
			'supp.fiscalCode' => $this->lang['index']['filters']['searchFields']['fiscalCode'],
			'supp.regNo' => $this->lang['index']['filters']['searchFields']['regNo'],
			'supp.id' => $this->lang['index']['filters']['searchFields']['id'],
			'codes.value' => $this->lang['index']['filters']['searchFields']['code'],
		]), 'post');
		$fld->addElement($f2 = \Engine\Form\Input\Select::create("", "filters[searchMode]", $fieldsValues['searchMode'], [
			'starts' => $this->lang['index']['filters']['searchMode']['starts'],
			'contains' => $this->lang['index']['filters']['searchMode']['contains'],
			'exact' => $this->lang['index']['filters']['searchMode']['exact'],
		]), 'post');
		$f1->addEvent("onChange", "if(this.value=='supp.id'||this.value=='codes.value'){FormField.setValue(null, 'Engine/Form/Input/Select', null, '{$f2->getId()}', 'exact');FormField.highlightChangedElement('{$f2->getId()}');}");
		$form->addElement($fld);

		// trusts filter
		$fld = new \App\Form\Input\Autocomplete_SupplierTrust($this->lang['index']['filters']['idTrust'], 'filters[idTrust]', $fieldsValues['idTrust']);
		$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);

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

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

        // location filter
        $fld = new \App\Form\Input\Autocomplete_CountryCity($this->lang['index']['filters']['location'], 'filters[location]', $fieldsValues['location']);
        $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;
	}
}
