<?php

namespace App\Form\Input;

/**
 * Supplier bank accounts autocomplete field
 *
 */
class Autocomplete_SupplierBankAccount extends Autocomplete_GeneralAbstract
{

	public function __construct($label=null, $name=null, $value=null)
	{
		parent::__construct($label, $name, $value);

	}

	/**
	 * (non-PHPdoc)
	 * @see \Engine\Form\Input\Autocomplete::getUrl()
	 */
	public function getUrl()
	{
		return ($this->url?:\Engine\Mvc\Dispatcher::getInstance()->getAppUrl('Modules/RemoteData/AutocompleteFieldData', '', ['class'=>'supplierBankAccount']));
	}

	/**
	 * (non-PHPdoc)
	 * @see \Engine\Form\Input\Autocomplete::determineDisplayValue()
	 */
	public function determineDisplayValue($displayId) :string
	{
		global $DBTSuppliersBankAccounts;

		$sql = "SELECT account FROM `$DBTSuppliersBankAccounts` WHERE `id`=?";

		$sqlEx = \DB::getInstance()->Execute($sql, [$displayId]);

		$results = [];
		foreach ($sqlEx as $bankAccount) {
			$results[] = $bankAccount['account'];
		}

		if (count($results)==1) {
			return $results[0];
		} else {
			return '';
		}

	}

}
