<?php

// TODO: add new info for records
// TODO: not used
// TODO: to be eliminated after service offer module is refactored
namespace App\Form\Input;

class Autocomplete_Data_SupplierAndProduct 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
	{
		global $DBTSuppliers, $DBTSuppliersProducts;

		// COMPOSE THE SQL THAT MUST BE EXECUTED //////////////////////////////////////////////////
		$vars = [];

		// ADD SUPPLIER PRODUCTS ------------------------------------------------------------------
		$sql = "(
			SELECT
				CONCAT('0|', id) AS idElement, productName AS display,
				'0' AS idRealSupplier, 'product' AS type, serviceCode,
				'' AS company, '' AS companySuffix, '' AS companyPrefix, '' AS countryCode, '' AS cityId,
				destinationCountryCode, destinationCityId
			FROM `$DBTSuppliersProducts`
			WHERE deleted=0 AND productName!=''
		";
		if ($this->searchString) {
			if ($this->searchType == 'contains') {
				$sql .= " AND (productName LIKE ? OR productName LIKE ?)";
				$vars[] = $this->searchString.'%';
				$vars[] = '% '.$this->searchString.'%';
			}
			else {
				$sql .= " AND productName LIKE ?";
				$vars[] = $this->searchString.'%';
			}
		}
		$sql.=' ) UNION ALL ';

		// ADD SUPPLIERS --------------------------------------------------------------------------
		$sql .= "(
			SELECT
				CONCAT(id, '|0') AS idElement, alias AS display,
				idRealSupplier, 'supplier' AS type, '' AS serviceCode,
				company, companySuffix, companyPrefix, countryCode, cityId,
				'' AS destinationCountryCode, '' AS destinationCityId
			FROM `$DBTSuppliers`
			WHERE deleted=0
		";
		if ($this->searchString) {
			if ($this->searchType == 'contains') {
				$sql .= " AND (alias LIKE ? OR company LIKE ?)";
				$vars[] = '%'.$this->searchString.'%';
				$vars[] = '%'.$this->searchString.'%';
			}
			else {
				$sql .= " AND (alias LIKE ? OR company LIKE ?)";
				$vars[] = $this->searchString.'%';
				$vars[] = $this->searchString.'%';
			}
		}

		$sql .= ' )';
		$sqlCountTotal = "SELECT COUNT(*) FROM ($sql) AS total_rows";
		$this->executeCountSql($sqlCountTotal, $vars);

		$sql .= " ORDER BY display ASC LIMIT {$this->offset}, {$this->resultsCountPage}";

		// EXECUTE THE SQL ////////////////////////////////////////////////////////////////////////
		return $this->executeSql($sql, $vars);
	}

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

		$list = [];
		foreach($pageResults as $row) {
			$extra = '';
			if ($row['serviceCode']) {
				$extra .= \App\Config\Services_Basic::getInstance()->getServiceName($row['serviceCode']);
			}

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

			$otherFields = [];
			if ($row['type'] == 'product') {
				$otherFields['className'] = 'italic';
			}

			// set info
			$info = '';
			if ($row['type'] == 'product') {
				if ($row['destinationCountryCode']) {
					$info .= "<div><b>{$_LANG['RemoteData']['AutocompleteFieldData']['index']['generalFields']['countryName']}</b>: ".\Entity\Country::select($row['destinationCountryCode'])->name."</div>";
				}

				if ($row['destinationCityId']) {
					$info .= "<div><b>{$_LANG['RemoteData']['AutocompleteFieldData']['index']['generalFields']['cityName']}</b>: ".\Entity\City::select($row['destinationCityId'])->name."</div>";
				}
			}
			else {
				$info .= "<div><b>{$_LANG['RemoteData']['AutocompleteFieldData']['index']['generalFields']['company']}</b>: ".trim($row['companyPrefix'].' '.$row['company'].' '.$row['companySuffix'])."</div>";
				if ($row['countryCode']) {
					$info .= "<div><b>{$_LANG['RemoteData']['AutocompleteFieldData']['index']['generalFields']['countryName']}</b>: ".\Entity\Country::select($row['countryCode'])->name."</div>";
				}

				if ($row['cityId']) {
					$info .= "<div><b>{$_LANG['RemoteData']['AutocompleteFieldData']['index']['generalFields']['cityName']}</b>: ".\Entity\City::select($row['cityId'])->name."</div>";
				}
			}

			$list[] = $this->getCleanItem($row['idElement'], $row['display'], $extra, $info, $otherFields);
		}

		return $list;
	}

}