<?php
namespace Modules\Orders\ImportedOrders\ProcessServicesList;

class Controller extends \App\Mvc\Controller\Popup\PaginatedListing\ListingAbstract
{

	/**
	 * The list of columns which support ordering.
	 * @var array
	 */
	protected $listOrderingColumns = ['addTime'];

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

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


	/**
	 * (non-PHPdoc)
	 * @see \App\Mvc\Controller\Window\PaginatedListing\ListingAbstract::optionDefineParams()
	 */
	protected function optionDefineParams(\App\Mvc\View\Utils\RowOptions\Option\Option $option, $selection)
	{
		switch($option->getId()){
			case 'addServiceOnOrder':
				// get list with imported services ids which have been already added on current order
				$alreadyImportedService = \Modules\Orders\OrderForm\Model_Handler::getInstance()->getImportedServiceIds();

				foreach ($selection as $id) {
					// get imported service entity
					$importedServiceEnt = \Entity\ImportedService::select($id);

					// check if service is not already imported
					if (in_array($id, $alreadyImportedService)) {
						$option->setDisabled();
						$option->addMessage('warning', sprintf($this->lang['index']['options']['reasons']['addServiceOnOrder']['alreadyImported'], ($importedServiceEnt->offerExternalSystemReservationId?$importedServiceEnt->offerExternalSystemReservationId:$importedServiceEnt->idServiceExtSys)));
						break;
					}

					if ($importedServiceEnt->isPackage) {
						if (count($selection)>1) {
							$option->setDisabled(); // only one package can be imported at a time
							$option->addMessage('info', $this->lang['index']['options']['reasons']['addServiceOnOrder']['onlyOnePackageCanBeImportedAtATime']);
							break;
						}

						foreach ($importedServiceEnt->packageSubservicesList as $subserviceDetails) {
							if ($subserviceDetails['processStatus'] != 'new') {
								$option->setDisabled();
								$option->addMessage('warning', sprintf($this->lang['index']['options']['reasons']['addServiceOnOrder']['subserviceAlreadyImported'], $subserviceDetails['idServiceTina']));
								break;
							}
						}
					}
				}
			break;

			case 'autoAddServiceOnOrder':
				// get list with imported services ids which have been already added on current order
				$alreadyImportedService = \Modules\Orders\OrderForm\Model_Handler::getInstance()->getImportedServiceIds();

				foreach ($selection as $id) {
					// get imported service entity
					$importedServiceEnt = \Entity\ImportedService::select($id);

					// check if service is not already imported
					if (in_array($id, $alreadyImportedService)) {
						$option->setDisabled();
						$option->addMessage('warning', sprintf($this->lang['index']['options']['reasons']['addServiceOnOrder']['alreadyImported'], ($importedServiceEnt->offerExternalSystemReservationId?$importedServiceEnt->offerExternalSystemReservationId:$importedServiceEnt->idServiceExtSys)));
						break;
					}

					if ($importedServiceEnt->isPackage) {
						if (count($selection)>1) {
							$option->setDisabled(); // only one package can be imported at a time
							$option->addMessage('info', $this->lang['index']['options']['reasons']['addServiceOnOrder']['onlyOnePackageCanBeImportedAtATime']);
							break;
						}

						foreach ($importedServiceEnt->packageSubservicesList as $subserviceDetails) {
							if ($subserviceDetails['processStatus'] != 'new') {
								$option->setDisabled();
								$option->addMessage('warning', sprintf($this->lang['index']['options']['reasons']['addServiceOnOrder']['subserviceAlreadyImported'], $subserviceDetails['idServiceTina']));
								break;
							}
						}
					}
				}
			break;

			case 'filterOrder' :
				foreach ($selection as $selectedId) {
					if (!\Entity\ImportedService::select($selectedId)->offerExternalSystemReservationId) {
						$option->setDisabled();
						break;
					}
				}
			break;

			case 'remoteLoad':
			break;
		}
		return true;
	}

	protected function actionIndex_preRender(\App\Mvc\ModelAbstract $model, \App\Mvc\ViewAbstract $view, Array $R)
	{
		$view->setOutputVar('idOrder', $R['idOrder']);
	}

	/**
	 * (non-PHPdoc)
	 * @see \App\Mvc\Controller\Window\PaginatedListing\ListingAbstract::optionGetEntityName()
	 */
	protected function optionGetEntityName($id)
	{
		return \Entity\ImportedService::select($id)->getId();
	}
}