<?php

namespace Modules\RemoteData\AutocompleteFieldData\ImportedSupplierSuggestions;


class Controller extends \App\Mvc\ControllerAbstract
{
	/**
	 * (non-PHPdoc)
	 * @see \App\Mvc\Controller\Window\PaginatedListing\PaginatedListing::initialize()
	 */
	protected function initialize()
	{
		parent::initialize();

		$this->actionsCfg['index'] =     ['checkSection' => false, 'checkOption' => false,];
	}

	/**
	 * Returns the json results for the current autocomplete listing.
	 * @return string
	 */
	protected function actionIndex($R)
	{

		// GET SUGGESTIONS ////////////////////////////////////////////////////////////////////////

		$impServEnt = \Entity\ImportedService::select($R['idImportedService']);
		$impServObj = $impServEnt->object;

		$impOffObj = $impServObj->chosenOffer;
		$impOrderSupplier = $impOffObj->supplier;

		$matches = $impOrderSupplier->getPopulatedMatchObject($impServObj->idSystem);
		$matches->search();
		$suggestionsArr = $matches->getAll();

		$suppliersSuggestion = [];
		foreach ($suggestionsArr as $id=>$results) {
			$supplier = \Entity\Supplier::select($id);

			$suppliersSuggestion[] = [
				'alias' 	=> $supplier->alias,
				'id' 		=> $supplier->getId(),
				'codes'	 	=> $supplier->codeIata,
			];
		}

		// instantiate the view object
		$view = new View_Index($this);
		$view->addInput('suggestionsList', $suppliersSuggestion);
		$view->setOutputVar('target', $R['targetElement']);
		$view->setOutputVar('idImportedService', $R['idImportedService']);
		$view->setOutputVar('idSupplier', $R['idSupplier']);

		return new \Engine\Mvc\Response\Html($view->render());
	}
}
