<?php
namespace Modules\Admin\BankAccounts;

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

	/**
	 * Indicates if the entity execution should use or not a transaction.
	 * @var Boolean
	 */
	protected $useSqlTransaction=true;

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

		$this->actionsCfg['insert'] = [];
		$this->actionsCfg['update'] = [];
		$this->actionsCfg['delete'] = [];

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

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


	/**
	 * (non-PHPdoc)
	 * @see \App\Mvc\Controller\Window\PaginatedListing\PaginatedListing::update_getDefaultFormData()
	 */
	protected function update_getDefaultFormData($idRecord)
	{
		$bankAcc = \Entity\AgencyBankAccount::select($idRecord);

		$formData = [];
		$formData['idBank'] = ['id'=>sprintf("%s|%s", $bankAcc->idBank,$bankAcc->idBankOffice)];
		$formData['account'] = $bankAcc->account;
		$formData['description'] = $bankAcc->description;
		$formData['currency'] = $bankAcc->currency;

		return $formData;
	}

	/**
	 * (non-PHPdoc)
	 * @see \App\Mvc\Controller\Window\PaginatedListing\PaginatedEntityListing::insert_createEntityObject()
	 */
	protected function insert_createEntityObject($formData)
	{
		$bankAcc = \Entity\AgencyBankAccount::insert();

		list($idBank, $idBankOffice) = explode("|", $formData['idBank']['id']);

		// associate fields
		$bankAcc->idBank		= $idBank;
		$bankAcc->idBankOffice	= $idBankOffice;
		$bankAcc->account		= $formData['account'];
		$bankAcc->description 	= $formData['description'];
		$bankAcc->currency		= $formData['currency'];

		return $bankAcc;
	}

	/**
	 * (non-PHPdoc)
	 * @see \App\Mvc\Controller\Window\PaginatedListing\PaginatedEntityListing::update_createEntityObject()
	 */
	protected function update_createEntityObject($formData)
	{
		$bankAcc = \Entity\AgencyBankAccount::update($this->updateIdRecord);

		list($idBank, $idBankOffice) = explode("|", $formData['idBank']['id']);

		// associate fields
		$bankAcc->idBank		= $idBank;
		$bankAcc->idBankOffice	= $idBankOffice;
		$bankAcc->account		= $formData['account'];
		$bankAcc->description 	= $formData['description'];
		$bankAcc->currency		= $formData['currency'];

		return $bankAcc;
	}

	/**
	 * (non-PHPdoc)
	 * @see \App\Mvc\Controller\Window\PaginatedListing\PaginatedEntityListing::delete_createEntityObject()
	 */
	protected function delete_createEntityObject($idRecord)
	{
		return \Entity\AgencyBankAccount::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':
				// do nothing
			break;

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

			break;

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

			break;

			case 'delete':
				foreach ($selection as $selectedItem) {
					if (!$option->determineOperationAvailability(\Entity\AgencyBankAccount::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\AgencyBankAccount::select($id)->account;
	}

	/**
	 * Executes the "set default" operation.
	 * @param array $recordsIds
	 * @param \App\MessagesCollector $msgs
	 * @param array $R
	 */
	protected function bulkActionSetDefault(array $recordsIds, \App\MessagesCollector $msgs, array $R)
	{
		// loop selected records, delete them and remove from selection
		foreach($recordsIds as $id) {
			$setDefaultAgencyBankAccountEnt = \Entity\AgencyBankAccount::setDefault($id);
			\App\Mvc\Controller\Helpers\EntityOperations::executeSave($setDefaultAgencyBankAccountEnt, $msgs, $this->useSqlTransaction);
		}
	}
}
