<?php
namespace App\Form\Input;

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

	/**
	 * (non-PHPdoc)
	 * @see \Engine\Form\Input\Autocomplete_Data_GeneralAbstract::getPageResultsBulk()
	 */
	protected function getPageResultsBulk() :array
	{
		$importedServicesListObj = new \App\EntityList\ImportedServices();
		$importedServicesListObj->applyUserRestriction(false);
		$importedServicesListObj->setColumns(
		[
			'importedService.id', 'importedService.idServiceExtSys', 'importedService.serviceCode', 'importedService.idSystem',
			'importedTickets.ticketNumber', 'importedTickets.ticketDate', 'importedService.passengerLastName', 'importedService.passengerFirstName',
			'importedService.offerExternalSystemReservationId', 'importedService.processStatus', 'importedService.systemStatus',
			'importedService.startDate', 'importedService.endDate', 'importedService.destinationCountryCode', 'importedService.destinationCityId',
			'importedService.offerTotalValue', 'importedService.offerTotalCurrency', 'importedService.idOfferSupplier'
		]);
		$importedServicesListObj->setFilter('importedTickets.ticketNumber', 'LIKE', "{$this->searchString}%");

		// filter by ticket date (if set)
		if ($this->filters['ticketDate'] and validDate($this->filters['ticketDate'])) {
			$importedServicesListObj->setFilter('importedTickets.ticketDate', 'LIKE', $this->filters['ticketDate']);
		}

		// filter by ticket date (if set)
		if ($this->filters['processStatus']) {
			$importedServicesListObj->setFilter('importedService.processStatus', 'LIKE', $this->filters['processStatus']);
		}

		$importedServicesListObj->setLimit($this->offset, $this->resultsCountPage);

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

	/**
	 * (non-PHPdoc)
	 * @see \Engine\Form\Input\Autocomplete_Data_GeneralAbstract::getPageResultsFormatted()
	 */
	protected function getPageResultsFormatted(Array $pageResults)
	{
		$_LANG = \App\Loader_Lang::getInstance()->getGlobal();

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

			$details = '';
			if ($row['passengerLastName'] or $row['passengerFirstName']) {
				$details = '('.$row['passengerLastName'].' '.$row['passengerFirstName'].')';
			}
			$details = trim($details.' '.$row['offerExternalSystemReservationId']);

			// set info
			$info = '';
			$info.="<div><b>{$_LANG['RemoteData']['AutocompleteFieldData']['index']['generalFields']['ID']}</b>: ".$row['id']."</div>";
			$info.="<div><b>{$_LANG['RemoteData']['AutocompleteFieldData']['index']['generalFields']['extId']}</b>: ".$row['idServiceExtSys']."</div>";
			$info.="<div><b>{$_LANG['RemoteData']['AutocompleteFieldData']['index']['generalFields']['system']}</b>: ".\Entity\ExtResSystem::select($row['idSystem'])->name."</div>";
			$info.="<div><b>{$_LANG['RemoteData']['AutocompleteFieldData']['index']['generalFields']['status']}</b>: ".$row['processStatus']."</div>";
			$info.="<div><b>{$_LANG['RemoteData']['AutocompleteFieldData']['index']['generalFields']['systemStatus']}</b>: ".$row['systemStatus']."</div>";

			if ($row['startDate'] or $row['endDate']) {
				$info.="<div><b>{$_LANG['RemoteData']['AutocompleteFieldData']['index']['generalFields']['period']}</b>: ".\Engine\DateTime::isoToStd($row['startDate']).' - '.\Engine\DateTime::isoToStd($row['endDate'])."</div>";
			}

			if ($row['serviceCode']) {
				$info.="<div><b>{$_LANG['RemoteData']['AutocompleteFieldData']['index']['generalFields']['serviceType']}</b>: ".\App\Config\Services_Basic::getInstance()->getServiceName($row['serviceCode'])."</div>";
			}

			if ($row['destinationCityId'] or $row['destinationCountryCode']) {
				$dest = \Entity\City::select($row['destinationCityId'])->name;

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

			if ($row['idOfferSupplier']) {
				$info.="<div><b>{$_LANG['RemoteData']['AutocompleteFieldData']['index']['generalFields']['supplier']}</b>: ".\Entity\Supplier::select($row['idOfferSupplier'])->alias."</div>";
			}

			if ($row['offerTotalValue']) {
				$info.="<div><b>{$_LANG['RemoteData']['AutocompleteFieldData']['index']['generalFields']['price']}</b>: ".$row['offerTotalValue'].' '.$row['offerTotalCurrency']."</div>";
			}

			if ($row['ticketDate']) {
				$info.="<div><b>{$_LANG['RemoteData']['AutocompleteFieldData']['index']['generalFields']['tktDate']}</b>: ".\Engine\DateTime::isoToStd($row['ticketDate'])."</div>";
			}

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

			$list[] = $this->getCleanItem($row['id'], $value, $details, $info);
		}

		return $list;
	}

}