<?php
namespace App\Form\Input;

class Autocomplete_Data_CorporateBankAccount extends \Engine\Form\Input\Autocomplete_Data_GeneralAbstract
{

	/**
	 * Indicates which is the minimum length for the search to be performed.
	 * @var Int
	 */
	protected $minLength = 0;

	/**
	 * (non-PHPdoc)
	 * @see \Engine\Form\Input\Autocomplete_Data_GeneralAbstract::getPageResultsBulk()
	 */
	protected function getPageResultsBulk() :array
	{
		$corporatesOfficesBankAccountsListObj = new \App\EntityList\CorporateOfficeBankAccounts();
		$corporatesOfficesBankAccountsListObj->applyUserRestriction(false);
		$corporatesOfficesBankAccountsListObj->setColumns(['corpOffBankAccounts.account', 'corpOffBankAccounts.currency', 'banks.bankName']);
		$corporatesOfficesBankAccountsListObj->setFilter('corpOffBankAccounts.account', 'LIKE', "{$this->searchString}%");

		if ($this->filters['idCompany']) {
			$corporatesOfficesBankAccountsListObj->setFilter('corpOffices.idCorporate', 'LIKE', $this->filters['idCompany']);
		}

		$corporatesOfficesBankAccountsListObj->setOrderBy('corpOffBankAccounts.account', 'ASC');
		$corporatesOfficesBankAccountsListObj->setLimit($this->offset, $this->resultsCountPage);

		return $this->executeSqlEntityList($corporatesOfficesBankAccountsListObj);
	}

	/**
	 * (non-PHPdoc)
	 * @see \Engine\Form\Input\Autocomplete_Data_GeneralAbstract::getPageResultsFormatted()
	 */
	protected function getPageResultsFormatted(Array $pageResults)
	{
		$list = [];
		foreach ($pageResults as $row) {

			// set extra
			$extra = $row['bankName'];
			if ($row['currency']) {
				$extra.=" [".$row['currency']."]";
			}

			// set the new record
			$list[] = $this->getCleanItem($row['id'], $row['account'], $extra, '');
		}

		return $list;
	}

}