<?php
// TODO: this input will be renamed after ImportedOrderCustomer will be eliminated

namespace App\Form\Input;

/**
 * Imported customer/passenger autocomplete field.
 */
class Autocomplete_ImportedOrderCustomer extends Autocomplete_Customer
{

	protected $class = "autocompleteImportedOrderCustomer";


	/**
	 * id for imported service
	 * @var Int
	 */
	protected $idImportedService;

	/**
	 *
	 * @var Int
	 */
	protected $passengerIdx;

	/**
	 * if hideSelector is true, the selector used for choosing inserting type will be hidden
	 * @var Boolean
	 */
	protected $hideSelector = false;


	/**
	 * Indicates the insert type operation on this imported passenger: manualSelect, autoInsert or a/c/i/etc - the undefined psg type
	 * @var String
	 */
	protected $insertType='manualSelect';

	/**
	 *
	 * @param Boolean $value
	 */
	public function setHideSelector($value)
	{
		$this->hideSelector = $value;
	}

	/**
	 *
	 * @param Int $index
	 */
	public function setPassengerIdx($index)
	{
		$this->passengerIdx = $index;
	}

	/**
	 *
	 * @param Int $value
	 */
	public function setIdImportedService($value)
	{
		$this->idImportedService = $value;
	}

	/**
	 * Indicates if the checkbox status is true/false.
	 * @param string $insertType
	 */
	public function setInsertType($insertType)
	{
		$CFG = \Engine\Loader_Cfg::getInstance()->getGlobal();
		if ($insertType!='manualSelect' and $insertType!='autoInsert' and !in_array($insertType, $CFG['generalData']['personTypes'])) {
			throw new \Exception("The `insert type` `{$insertType}` is not valid.");
		}
		$this->insertType = $insertType;
	}

	protected function getTemplateParams($templateClass = null, $parentElement = null)
	{
		$CFG = \Engine\Loader_Cfg::getInstance()->getGlobal();
		$_LANG = \App\Loader_Lang::getInstance()->getAll();

		$params = parent::getTemplateParams($templateClass, $parentElement );

		$params['hideSelector'] 	= $this->hideSelector;
		$params['idImportedService']= $this->idImportedService;
		$params['passengerIdx'] 	= $this->passengerIdx;

		$params['insertType'] = $this->insertType;
		$params['passengerTypes'] = [];
		foreach($CFG['generalData']['personTypes'] as $type) {
			$params['passengerTypes'][$type] = $_LANG['generalData']['personTypes'][$type];
		}

		$psgList = (array)\Entity\ImportedService::select($this->idImportedService)->object->passengers;
		$currPsg = $psgList[$this->passengerIdx];

		$params['fullName']  = '';
		if ($currPsg->lastNameStandard) {
			$params['fullName'] = \App\Smarty\Functions\NameFormat::renderStr($currPsg->firstNameStandard, $currPsg->lastNameStandard);
		}
		elseif ($currPsg->lastNameLocal) {
			$params['fullName'] = \App\Smarty\Functions\NameFormat::renderStr($currPsg->firstNameLocal, $currPsg->lastNameLocal);
		}

		return $params;
	}


	/**
	 * (non-PHPdoc)
	 * @see \App\Form\Input\Autocomplete_GeneralAbstract::loadTranslations()
	 */
	protected function loadTranslations($templateClass = null)
	{
		parent::loadTranslations($templateClass);
		\App\Loader_Lang::getInstance()->loadAppFile('_customLoad/formField/inputs/inputAutocompleteImportedOrderCustomer.lang.php');
	}


}
