<?php
namespace Modules\Admin\Banks;

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 = ['bankName', 'bankCode', 'addTime'];


	protected function insert_createEntityObject($formData) :\Entity\Bank
	{

		$CFG = \Engine\Loader_Cfg::getInstance()->getGlobal();

		$bank = \Entity\Bank::insert();

		if ($CFG['useAlternativeNames']['bankName']) {
			$bank->nameStandard = $formData['name']['standard'];
			$bank->nameLocal = $formData['name']['local'];
		} else {
			$bank->nameStandard = $formData['name'];
		}

		$bank->bankCode = $formData['bankCode'];

		return $bank;
	}

	/**
	 * (non-PHPdoc)
	 * @see \App\Mvc\Controller\Window\PaginatedListing\PaginatedListing::update_getDefaultFormData()
	 */
	protected function update_getDefaultFormData($id) :array
	{
		$CFG = \Engine\Loader_Cfg::getInstance()->getGlobal();

		$bank = \Entity\Bank::select($id);

		$formData = [];
		if ($CFG['useAlternativeNames']['bankName']) {
			$formData['name']['standard'] = $bank->nameStandard;
			$formData['name']['local'] =  	$bank->nameLocal;
		}
		else {
			$formData['name'] = 			$bank->nameStandard;
		}

		$formData['bankCode'] = $bank->code;

		return $formData;
	}

	/**
	 * (non-PHPdoc)
	 * @see \App\Mvc\Controller\Window\PaginatedListing\PaginatedEntityListing::update_createEntityObject()
	 */
	protected function update_createEntityObject($formData)
	{
		$CFG = \Engine\Loader_Cfg::getInstance()->getGlobal();

		$bank = \Entity\Bank::update($this->updateIdRecord);

		if ($CFG['useAlternativeNames']['bankName']) {
			$bank->nameStandard = $formData['name']['standard'];
			$bank->nameLocal = $formData['name']['local'];
		}
		else {
			$bank->nameStandard = $formData['name'];
		}

		$bank->bankCode = $formData['bankCode'];

		return $bank;
	}

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

	/**
	 * (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':
				$option->determineOperationAvailability(\Entity\Bank::insert());
			break;

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

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

			break;

			case 'showOffices':
			break;

			default:
				return false;
		}

		return true;

	}

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