<?php

namespace App\Form\Input;

class Autocomplete_CorporateAndOffice extends Autocomplete_GeneralAbstract
{

	protected $class = "autocompleteCorporate";

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

		$this->setStyle('minimumTextLength', 3);

	}

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

	/**
	 * (non-PHPdoc)
	 * @see \Engine\Form\Input\Autocomplete::getValue()
	 */
	public function getValue()
	{
		$v = parent::getValue();
		if ($v==='|') {
			$v = '';
		}
		return $v;
	}

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

		if ($displayId) {
			$corporateDet = explode('|', $displayId);
			$displayValue = '';
			if ($corporateDet) {
				$displayValue = trim( (string)\Entity\Corporate::select($corporateDet[0])->alias . ' ' . (string)\Entity\CorporateOffice::select($corporateDet[1])->name );
			}
			return $displayValue;
		}
		return '';
	}

}
