<?php
namespace App\Form\Input;

class Autocomplete_Data_SupplierBankAccount 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
	{
		$suppliersBankAccountsListObj = new \App\EntityList\SupplierBankAccounts();
		$suppliersBankAccountsListObj->applyUserRestriction(false);
		$suppliersBankAccountsListObj->setColumns(['suppBankAccounts.account', 'suppBankAccounts.currency', 'banks.bankName']);
		$suppliersBankAccountsListObj->setFilter('suppBankAccounts.account', 'LIKE', "{$this->searchString}%");

		if ($this->filters['idSupplier']) {
			$suppliersBankAccountsListObj->setFilter('suppBankAccounts.idSupplier', 'LIKE', $this->filters['idSupplier']);
		}

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

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

	/**
	 * (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;
	}

}