<?php
namespace Modules\Admin\Config\Gds\Profiles\Fields;

class View_Utils_FormAdmin extends \App\Mvc\View_Utils_FormAbstract
{

	/**
	 * Creates the form for field insert/update.
	 * @see \App\Mvc\View_Utils_FormAbstract::getForm()
	 */
	public function getForm()
	{
		// load globals
		$CFG   = \Engine\Loader_Cfg::getInstance()->getGlobal();
		//$_LANG = \App\Loader_Lang::getInstance()->getAll();

		// load the configurations for this form
		$entCfg = \Entity\GdsProfileField::loadCFG();

		$form = \Engine\Form\General::create();
		$form->setStyle('width', '300');

		// GENERAL ////////////////////////////////////////////////////////////////////////////////
		$fieldset = \Engine\Form\Container\Fieldset::create($this->lang['formFieldCat']['field']);
		$form->addContainer($fieldset);

		// category
		$fld = new \Engine\Form\Input\Select($this->lang['formFields']['category'], 'category', $this->values['category']);
		$fld->setPossibleValues([
			'corporate' => $this->lang['formFields']['categories']['corporate'],
			'customer'  => $this->lang['formFields']['categories']['customer'],
		]);
		$fld->onChange("$('{$this->submitButtonName}').value=0;$('categoryChanged').value='1';this.form.submit();");
		$fld->showDefaultOption();
		$fld->importFromConfig($entCfg['category']);
		if ($this->controller->getAction()=='update') {
			$fld->setDisabled();
		}
		$fieldset->addElement($fld);

		// hidden category id (for update operation)
		if ($this->controller->getAction()=='update') {
			$fieldset->addElement(new \Engine\Form\Input\Hidden('', 'category', $this->values['category']));
		}

		// type
		$fldFieldType = new \Engine\Form\Input\Radio($this->lang['formFields']['type'], 'type', $this->values['type']);
		$fldFieldType->importFromConfig($entCfg['type']);
		$fldFieldType->setPossibleValues([
			'static'  => $this->lang['formFields']['types']['static'],
			'dynamic' => $this->lang['formFields']['types']['dynamic'],
		]);
		$fieldset->addElement($fldFieldType);

		// label
		$fld = new \Engine\Form\Input\Text($this->lang['formFields']['label'], 'label', $this->values['label']);
		//$fld->setTooltip($this->lang['formFields']['tooltips']['label']);
		$fld->importFromConfig($entCfg['label']);
		$fieldset->addElement($fld);

		// description
		$fld = new \Engine\Form\Input\Textarea($this->lang['formFields']['description'], 'description', $this->values['description']);
		//$fld->setTooltip($this->lang['formFields']['tooltips']['description']);
		$fld->importFromConfig($entCfg['description']);
		$fieldset->addElement($fld);

		// INPUT ////////////////////////////////////////////////////////////////////////////////////////////////////////
		$fieldset3 = \Engine\Form\Container\Fieldset::create($this->lang['formFieldCat']['input']);
		$form->addContainer($fieldset3);

		// inputType
		$fldInputType = new \Engine\Form\Input\Select($this->lang['formFields']['inputType'], 'inputType', $this->values['inputType']);
		$fldInputType->setPossibleValues([
			'text' 		=> $this->lang['formFields']['inputTypes']['text'],
			'textarea' 	=> $this->lang['formFields']['inputTypes']['textarea'],
			'radio' 	=> $this->lang['formFields']['inputTypes']['radio'],
			'select' 	=> $this->lang['formFields']['inputTypes']['select'],
			//'checkbox' 	=> $this->lang['formFields']['inputTypes']['checkbox'],
			//'date' 		=> $this->lang['formFields']['inputTypes']['date'],
		]);
		$fldInputType->showDefaultOption();
		$fldInputType->importFromConfig($entCfg['inputType']);
		$fieldset3->addElement($fldInputType);

		// possible values
		$fldPossVal = new \App\Form\Input\Cloned_KeyValue($this->lang['formFields']['possibleValues'], 'possibleValues', $this->values['possibleValues']);
		$fldPossVal->setMandatory();
		$fldPossVal->setTooltip('Insert code and description for each possible value');
		$fieldset3->addElement($fldPossVal);

		$fldInputType->setControlledElements([
			'radio'   => [$fldPossVal],
			'select'  => [$fldPossVal],
		]);
		$fldFieldType->setControlledElements([
			'static'   => [$fldPossVal],
		], 'disable');

		// VALIDATOR ////////////////////////////////////////////////////////////////////////////////////////////////////////
		$fieldset4 = \Engine\Form\Container\Fieldset::create($this->lang['formFieldCat']['validator']);
		$form->addContainer($fieldset4);

		$fldInputType = new \Engine\Form\Input\Select($this->lang['formFields']['validator']['type'], 'validator[type]', $this->values['validator']['type']??null);
		$fldInputType->setPossibleValues([
			'digits' 	=> $this->lang['formFields']['validator']['types']['digits'],
			'int' 		=> $this->lang['formFields']['validator']['types']['int'],
			'float' 	=> $this->lang['formFields']['validator']['types']['float'],
			'alnum' 	=> $this->lang['formFields']['validator']['types']['alnum'],
			'string' 	=> $this->lang['formFields']['validator']['types']['string'],
 			'date' 		=> $this->lang['formFields']['validator']['types']['date'],
// 			'time' 		=> $this->lang['formFields']['valueTypes']['time'],
// 			'datetime' 	=> $this->lang['formFields']['valueTypes']['datetime'],
			'email' 	=> $this->lang['formFields']['validator']['types']['email'],
			//'url' 		=> $this->lang['formFields']['valueTypes']['url'],
		]);
		$fldInputType->showDefaultOption();
		//$fldInputType->importFromConfig($entCfg['inputType']);
		$fieldset4->addElement($fldInputType);

		$fld = new \Engine\Form\Input\Text($this->lang['formFields']['validator']['minValue'], 'validator[minValue]', $this->values['validator']['minValue']??'');
		$fieldset4->addElement($fld);

		$fld = new \Engine\Form\Input\Text($this->lang['formFields']['validator']['maxValue'], 'validator[maxValue]', $this->values['validator']['maxValue']??'');
		$fieldset4->addElement($fld);

		$fld = new \Engine\Form\Input\Text($this->lang['formFields']['validator']['minLength'], 'validator[minLength]', $this->values['validator']['minLength']??'');
		$fieldset4->addElement($fld);

		$fld = new \Engine\Form\Input\Text($this->lang['formFields']['validator']['maxLength'], 'validator[maxLength]', $this->values['validator']['maxLength']??'');
		$fieldset4->addElement($fld);

		$fld = new \Engine\Form\Input\Text($this->lang['formFields']['validator']['fixLength'], 'validator[fixLength]', $this->values['validator']['fixLength']??'');
		$fieldset4->addElement($fld);

		// GDS TAGS ////////////////////////////////////////////////////////////////////////////////////////////////////////
		$fieldset2 = \Engine\Form\Container\Fieldset::create($this->lang['formFieldCat']['tags']);
		$form->addContainer($fieldset2);

		// remarkVar
		$fld = new \Engine\Form\Input\Text($this->lang['formFields']['remarkVar'], 'remarkVar', $this->values['remarkVar']);
		$fld->importFromConfig($entCfg['remarkVar']);
		$fld->setTooltip($this->lang['formFields']['remarkVarTooltip']);
		$fieldset2->addElement($fld);

		// tagSabre
		$fldSabre = new \Engine\Form\Input\Select($this->lang['formFields']['tagSabre'], 'tagSabre', $this->values['tagSabre']);
		if ($this->values['category']) {
			foreach ($CFG['sabre']['profiles']['sabreTags'][$this->values['category']] as $tag) {
				$fldSabre->setPossibleValue($tag, $tag);
			}
		}
		$fldSabre->showDefaultOption();
		$fldSabre->importFromConfig($entCfg['tagSabre']);
		$fldSabre->setTooltip($this->lang['formFields']['tagSabreTooltip']);
		$fieldset2->addElement($fldSabre);

		$fldSabreExtra = new \Engine\Form\Input\Text(null, 'tagSabreExtra', $this->values['tagSabreExtra']);
		$fieldset2->addElement($fldSabreExtra);

		$fldSabre->setControlledElements(['RemarkStartsWith'=>[$fldSabreExtra]], 'hide');

		// tagAmadeus
// 		$fld = new \Engine\Form\Input\Text($this->lang['formFields']['tagAmadeus'], 'tagAmadeus', $this->values['tagAmadeus']);
// 		$fld->importFromConfig($entCfg['tagAmadeus']);
// 		$fieldset2->addElement($fld);

		// tagWorldspan
		//$fld = new \Engine\Form\Input\Text($this->lang['formFields']['tagWorldspan'], 'tagWorldspan', $this->values['tagWorldspan']);
		//$fld->importFromConfig($entCfg['tagWorldspan']);
		//$fieldset2->addElement($fld);

		// tagGalileo
		//$fld = new \Engine\Form\Input\Text($this->lang['formFields']['tagGalileo'], 'tagGalileo', $this->values['tagGalileo']);
		//$fld->importFromConfig($entCfg['tagGalileo']);
		//$fieldset2->addElement($fld);

		// tagApollo
		//$fld = new \Engine\Form\Input\Text($this->lang['formFields']['tagApollo'], 'tagApollo', $this->values['tagApollo']);
		//$fld->importFromConfig($entCfg['tagApollo']);
		//$fieldset2->addElement($fld);


		// field id (for update operation)
		if ($this->controller->getAction()=='update') {
			$form->addElement(new \Engine\Form\Input\Hidden('', 'id', $this->values['id']));
		}

		// submit button
		$form->addElement(new \Engine\Form\Input\Hidden('', $this->submitButtonName, 1));
		$form->addElement(new \Engine\Form\Input\Hidden('', 'categoryChanged', 0));

		return $form;
	}

}
