<?php

namespace Modules\Admin\Stations;

class Controller extends \App\Mvc\Controller\Window\PaginatedListing\PaginatedEntityListing
{

	/**
	 * Indicates if the entity execution should use or not a transaction. It left to 'null' the
	 * class won't work correctly - the concrete class must set it to true/false.
	 * @var Boolean
	 */
	protected $useSqlTransaction = true;

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

	/**
	 * (non-PHPdoc)
	 * @see \App\Mvc\Controller\Window\PaginatedListing\PaginatedEntityListing::insert_createEntityObject()
	 */
	protected function insert_createEntityObject($formData)
	{
		$location = explode('|', $formData['locationName']['id']);

		// create entity station
		$entityObj = \Entity\Station::insert();
		$entityObj->code    = $formData['code'];
		$entityObj->type    = $formData['type'];
		if ($formData['codeLocal']) {
			$entityObj->codeLocal = $formData['codeLocal'];
		}
		$entityObj->name    = $formData['name'];
		if ($formData['nameLocal']) {
			$entityObj->nameLocal = $formData['nameLocal'];
		}

		if ($location[1]) {
			$entityObj->cityId = $location[1];
		}
		else {
			$entityObj->cityId = 0;
		}

		if ($location[0]) {
			$entityObj->countryCode = $location[0];
		}
		else {
			$entityObj->countryCode = '';
		}

		$entityObj->timezone = $formData['timezone'];

		return $entityObj;
	}

	/**
	 * (non-PHPdoc)
	 * @see \App\Mvc\Controller\Window\PaginatedListing\PaginatedListing::update_getDefaultFormData()
	 */
	protected function update_getDefaultFormData($idEntity)
	{
		// select entity station
		$station = \Entity\Station::select($idEntity);
		if (!$station->recordExists()) {
			return null;
		}

		$formData = [];
		$formData['code'] 	 	  = $station->code;
		$formData['type']	 	  = $station->type;
		$formData['codeLocal'] 	  = $station->codeLocal;
		$formData['name'] 	 	  = $station->name;
		$formData['nameLocal'] 	  = $station->nameLocal;
		$formData['locationName'] = ['id' => $station->countryCode.'|'.$station->cityId];
		$formData['timezone']	  = $station->timezone;


		return $formData;
	}

	/**
	 * (non-PHPdoc)
	 * @see \App\Mvc\Controller\Window\PaginatedListing\PaginatedEntityListing::update_createEntityObject()
	 */
	protected function update_createEntityObject($formData)
	{
		$location = explode('|', $formData['locationName']['id']);

		// create entity station
		$entityObj = \Entity\Station::update($this->updateIdRecord);

		$entityObj->codeLocal = $formData['codeLocal'];
		$entityObj->name    = $formData['name'];
		$entityObj->nameLocal = $formData['nameLocal'];

		if ($location[1]) {
			$entityObj->cityId = $location[1];
		}
		else {
			$entityObj->cityId = 0;
		}

		if ($location[0]) {
			$entityObj->countryCode = $location[0];
		}
		else {
			$entityObj->countryCode = '';
		}

		$entityObj->timezone = $formData['timezone'];

		return $entityObj;
	}

	/**
	 * (non-PHPdoc)
	 * @see \App\Mvc\Controller\Window\PaginatedListing\PaginatedEntityListing::delete_createEntityObject()
	 */
	protected function delete_createEntityObject($idRecord)
	{
		return \Entity\Station::delete($idRecord);
	}

	/**
	 * (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 'insert':
				if (!$option->determineOperationAvailability(\Entity\Station::insert())) {
					$option->setDisabled();
				}
			break;

			case 'update':
				foreach ($selection as $selectedItem) {
					if (!$option->determineOperationAvailability(\Entity\Station::update($selectedItem))) {
						break;
					}
				}
			break;

			case 'delete':
				foreach ($selection as $selectedItem) {
					if (!$option->determineOperationAvailability(\Entity\Station::delete($selectedItem))) {
						break;
					}
				}
			break;

			default:
				return false;
		}

		return true;

	}

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