<?php

namespace App\Form\Input;

class Autocomplete_Data_SupplierProduct extends \Engine\Form\Input\Autocomplete_Data_GeneralAbstract
{

	/**
	 * Indicates which is the minimum length for the search to be performed.
	 * @var Int
	 */
	protected $minLength = 2;

	/**
	 * (non-PHPdoc)
	 * @see \Engine\Form\Input\Autocomplete_Data_GeneralAbstract::getPageResultsBulk()
	 */
	protected function getPageResultsBulk() :array
	{
		$destCountryCode = '';
		$destCityId = 0;
		if ($this->filters['destinationPlaceCode']) {
			list($destCountryCode, $destCityId) = explode('|', $this->filters['destinationPlaceCode']);
		}

		$supplierProductsListObj = new \App\EntityList\SupplierProducts();
		$supplierProductsListObj->applyUserRestriction(false);
		$supplierProductsListObj->setColumns(['suppPr.productName', 'suppPr.serviceCode', 'suppPr.destinationCountryCode', 'suppPr.destinationCityId', 'suppPr.category', 'suppPr.idRealSupplierProduct']);

		$supplierProductsListObj->setFilter('suppPr.productName', '!=', '');

		if ($this->searchType == 'contains') {
			$supplierProductsListObj->setCustomFilter('suppPr.productName LIKE ? OR suppPr.productName LIKE ?', ["{$this->searchString}%", "%{$this->searchString}%"]);
		}
		else {
			$supplierProductsListObj->setFilter('suppPr.productName', 'LIKE', "{$this->searchString}%");
		}

		if ($this->filters['serviceCode']) {
			$supplierProductsListObj->setFilter('suppPr.serviceCode', 'LIKE', $this->filters['serviceCode']);
		}

		if ($this->filters['hideDuplicates']) {
			$supplierProductsListObj->setFilter('suppPr.idRealSupplierProduct', '=', 0);
		}

		if ($destCityId) {
			$supplierProductsListObj->setFilter('suppPr.destinationCityId', '=', "{$destCityId}");
		}
		elseif ($destCountryCode) {
			$supplierProductsListObj->setFilter('suppPr.destinationCountryCode', 'LIKE', "{$destCountryCode}");
		}

		$supplierProductsListObj->setOrderBy('suppPr.productName', 'ASC');
		$supplierProductsListObj->setLimit($this->offset, $this->resultsCountPage);

		return $this->executeSqlEntityList($supplierProductsListObj);
	}

	/**
	 * (non-PHPdoc)
	 * @see \Engine\Form\Input\Autocomplete_Data_GeneralAbstract::getPageResultsFormatted()
	 */
	protected function getPageResultsFormatted(Array $pageResults)
	{
		global $_LANG;

		$list = [];
		foreach($pageResults as $row) {

			// other fields
			$otherFields = [
				'destinationCountryCode' =>		$row['destinationCountryCode'],
				'destinationCountryName' =>		\Entity\Country::select($row['destinationCountryCode'])->name,
				'destinationCityId' =>			$row['destinationCityId'],
				'destinationCityName' =>		\Entity\City::select($row['destinationCityId'])->name,
				'category' =>					$row['category'],
				'serviceCode' =>				$row['serviceCode'],
				'serviceName' =>				\App\Config\Services_Basic::getInstance()->getServiceName($row['serviceCode']),
			];

			// set info
			$info = '';
			if ($otherFields['serviceName']) {
				$info .= "<div><b>{$_LANG['RemoteData']['AutocompleteFieldData']['index']['generalFields']['serviceType']}</b>: {$otherFields['serviceName']}</div>";
			}

			if ($row['destinationCountryCode']) {
				$info .= "<div><b>{$_LANG['RemoteData']['AutocompleteFieldData']['index']['generalFields']['countryName']}</b>: {$otherFields['destinationCountryName']}</div>";
			}

			if ($row['destinationCityId']) {
				$info .= "<div><b>{$_LANG['RemoteData']['AutocompleteFieldData']['index']['generalFields']['cityName']}</b>: {$otherFields['destinationCityName']}</div>";
			}

			// set extra
			if ($otherFields['destinationCityName']) {
				$extra = $otherFields['destinationCityName'];
			}
			else {
				$extra = $otherFields['destinationCountryName'];
			}

			if ($row['idRealSupplierProduct']) {
				$extra = "[{$_LANG['RemoteData']['AutocompleteFieldData']['index']['generalFields']['duplicate']}] ".$extra;
			}

			// attach record
			$list[] = $this->getCleanItem($row['id'], $row['productName'], $extra, $info, $otherFields);

		}

		return $list;
	}

}