<?php
namespace App\Form\Input;

class Select_AgencyBankAccount extends \Engine\Form\Input\Select
{

	/**
	 * Indicates the id of office to be displayed in the list
	 * @var String
	 */
	protected $idOffice = NULL;

	/**
	 * The option that means "no value"
	 * @var array
	 */
	protected $notFilledOption = [];

	/**
	 * Sets the id office
	 * @param String $idOffice
	 */
	public function setIdOffice($idOffice)
	{
		$this->idOffice = $idOffice;
	}

	/**
	 * Sets the option that means "no value", and that will be displayed first in the list
	 * @param string $key
	 * @param string $val
	 */
	public function setNotFilledValue($key, $name)
	{
	    $this->notFilledOption[$key] = $name;
	}

	/**
	 * (non-PHPdoc)
	 * @see \Engine\Form\Input\Select::determinePossibleValues()
	 */
	protected function determinePossibleValues()
	{
		// get bank accounts from db
		$basObj = new \App\EntityList\AgencyBankAccounts();
		$basObj->setColumns(['idBank', 'account', 'currency', 'description']);
		$basObj->setOrderBy('id', 'ASC');

		// filter by id office
		if ($this->idOffice) {
			$basObj->setFilter('off.idOffice', '=', $this->idOffice);
		}

		$basEx = $basObj->exportRecords();

		// populate the possible values
		$vals = [];

		// the "no value" option
		if ($this->notFilledOption) {
		    $vals = array_merge($vals, $this->notFilledOption);
		}

		// the bank accounts list
		foreach ($basEx as $ba) {
			$baDet = $ba['account'];
			if ($ba['currency']) {
				$baDet.= ' ['.$ba['currency'].']';
			}
			if ($ba['idBank']) {
				$baDet.= ' - '.\Entity\Bank::select($ba['idBank'])->name;
			}

			if ($ba['description']) {
				$baDet.= ' - '.$ba['description'];
			}
			$vals[$ba['id']] = $baDet;
		}

		return $vals;
	}


}
