<?php

namespace App\Form\Input;

/**
 * Base input remarks field class.
 * @author Gabi
 */
class Remarks extends \Engine\Form\Input\Textarea
{

	protected $class = "remarks";

	/**
	 * Indicate the type of remarks.
	 * @var String
	 */
	protected $remarksType = null;

	/**
	 * Indicates if the GDS profile button should be displayed.
	 * @var boolean
	 */
	protected $gdsProfileButton = null;

	/**
	 * The current order id - used to determine the owner company.
	 * @var int
	 */
	protected $gdsProfileIdOrder = null;


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


	/**
	 * Sets the current remarks type
	 *
	 * @param String $rmType
	 * @return self
	 */
	public function setRemarksType($rmType)
	{
		$this->remarksType=$rmType;
		return $this;
	}

	/**
	 * Returns the current remarks type
	 *
	 * @return String
	 */
	public function getRemarksType()
	{
		return $this->remarksType;
	}

	/**
	 * Indicate that we want the button for GDS profile.
	 * @throws \Exception
	 */
	public function addGdsProfileButton($idOrder)
	{
		if ($this->remarksType!='service') {
			throw new \Exception('You must set the remarks type as `service` before calling '.__METHOD__);
		}

		$this->gdsProfileButton = true;
		$this->gdsProfileIdOrder = $idOrder;

	}

	/**
	 * Returns a list of collected parameters used in the html template
	 */
	protected function getTemplateParams($templateClass = null, $parentElement = null)
	{
		$params = parent::getTemplateParams($templateClass, $parentElement);
		$params['remarksType'] = $this->getRemarksType();
		// check if assisted input should be displayed
		$params['assistedInputButton'] = \App\Config\Remarks::getInstance()->remarkTypeHasAssistedRemarks($this->remarksType);

		$_LANG = \App\Loader_Lang::getInstance()->getGlobal();
		$optionsObj = $this->getOptionsMenu();

		// add gds profile button
		if ($this->gdsProfileButton) {
			$optionsObj->append(
				\App\Mvc\View\Utils\RowOptions\Option\Popup::create("gdsProfile")
					->setIcon('profile')
					->setDescription('define GDS profile')
					->setDimensions(800, 500)
					->setModule('Modules/Utils/GdsProfileFill')
					->setAction('index')
					->addParamFromJs('idOrder', $this->gdsProfileIdOrder)
					->addParamFromJs('idCustomer', "getFirstDefinedPax()")
					->addParamFromJs('remarksFieldId', "'{$params['id']}'")
					->addParamFromJs('remarks', "\$F('{$params['id']}')")
				);
		}

		$optionsObj->append(
			\App\Mvc\View\Utils\RowOptions\Option\Option::create("view")
				->setIcon('view')
				->setDescription($_LANG['globalEntityMessages']['viewRemarks'])
				->setJsCallback("RemoteLoad.load(this, MVC.appUrl('RemoteData/ProcessRemarks', 'viewRemarks', { 'type': '{$params['remarksType']}' }), { 'text': \$F('{$params['id']}') }).attach(this);" )
		);
		$optionsObj->append(
			\App\Mvc\View\Utils\RowOptions\Option\Option::create("edit")
				->setIcon('edit')
				->setDisabled(!$params['assistedInputButton'])
				->setDescription($_LANG['globalEntityMessages']['inputRemarks'])
				->setJsCallback("remarksAssistedInputs(\$F('{$params['id']}'), '{$params['remarksType']}', '{$params['id']}');")
		);

		$params['optionsMenu'] = $this->optionsMenu;

		return $params;
	}

}
