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

class View_Utils_FormAdmin extends \App\Mvc\View_Utils_FormAbstract
{

	/**
	 * Creates the form for template insert/update.
	 * @see \App\Mvc\View_Utils_FormAbstract::getForm()
	 */
	public function getForm()
	{

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

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


		// GENERAL ////////////////////////////////////////////////////////////////////////////////
		$fieldset = \Engine\Form\Container\Fieldset::create($this->lang['formFieldCat']['template']);
		$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->showDefaultOption();
		$fld->importFromConfig($entCfg['category']);
		if ($this->controller->getAction()=='update' or $this->values['idOriginalTemplate']) {
			$fld->setDisabled();
		}
		$fld->onChange("$('{$this->submitButtonName}').value=0;$('categoryChanged').value='1';this.form.submit();");
		$fieldset->addElement($fld);

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

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


		// FIELDS ///////////////////////////////////////////////////////////////////////////////
		if ($this->values['category']) {

			$listObj = new \App\EntityList\GdsProfilesFields();
			$listObj->setColumns(['fld.category', 'fld.label', 'fld.type', 'fld.inputType', 'fld.description', 'fld.remarkVar', 'fld.tagSabre']);
			$listObj->setFilter('fld.category', '=', $this->values['category']);
			$listObj->setOrderBy('fld.label', 'ASC');
			$fields = $listObj->exportRecords();

			$staticFields = array_filter(array_map(function($elm) {
			    if ($elm['type']=='static') {
			        return $elm;
			    } return null;
			}, $fields));

		    $dynamicFields = array_filter(array_map(function($elm) {
		        if ($elm['type']=='dynamic') {
		            return $elm;
		        } return null;
		    }, $fields));

	        usort($staticFields, function($f1, $f2) {		// reorder alphabetically
	            return strnatcmp($f1["label"], $f2["label"]);
	        });
            usort($dynamicFields, function($f1, $f2) {
                return strnatcmp($f1["label"], $f2["label"]);
            });

			$fieldset2 = \Engine\Form\Container\Fieldset::create($this->lang['formFieldCat']['staticFields']);
			$fieldset2->setCollapsible();
			$form->addContainer($fieldset2);

			foreach ($staticFields as $field) {
				if (!isset($this->values['templateFields'][$field['id']])) {
					$visibility = 'invisible';
				}
				elseif ($this->values['templateFields'][$field['id']]['mandatory']=='1') {
					$visibility = 'mandatory1';
				}
				elseif ($this->values['templateFields'][$field['id']]['mandatory']=='2') {
					$visibility = 'mandatory2';
				}
				else {
					$visibility = 'optional';
				}
				$fld = new \Engine\Form\Input\Select($field['label'], 'field['.$field['id'].']', $visibility);
				//$fld = new \Engine\Form\Input\Radio($field['label'], 'field['.$field['id'].']', $visibility);
				$fld->setPossibleValues([
					'mandatory1' => $this->lang['formFields']['visibility']['mandatory1'],
					'mandatory2' => $this->lang['formFields']['visibility']['mandatory2'],
					'optional'	=> $this->lang['formFields']['visibility']['optional'],
					'invisible'	=> $this->lang['formFields']['visibility']['invisible'],
				]);

				$details = "";
				if ($field['remarkVar']) {
				    $details .= sprintf($this->lang['formFields']['rmVar'], $field['remarkVar'])."\n";
				}
				if ($field['tagSabre']) {
				    $details .= "Sabre: ".$field['tagSabre']."\n";
				}
				$fld->setDetails(nl2br(trim($details)));
				$fld->setTooltip($field['description']);
				//$fld->setCustomHTML("<dfn style=\"display:inline;\">{$field['type']}</dfn>");
				$fieldset2->addElement($fld);
			}

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

			foreach ($dynamicFields as $field) {
			    if (!isset($this->values['templateFields'][$field['id']])) {
			        $visibility = 'invisible';
			    }
			    elseif ($this->values['templateFields'][$field['id']]['mandatory']=='1') {
			        $visibility = 'mandatory1';
			    }
			    elseif ($this->values['templateFields'][$field['id']]['mandatory']=='2') {
			        $visibility = 'mandatory2';
			    }
			    else {
			        $visibility = 'optional';
			    }
			    $fld = new \Engine\Form\Input\Select($field['label'], 'field['.$field['id'].']', $visibility);
			    //$fld = new \Engine\Form\Input\Radio($field['label'], 'field['.$field['id'].']', $visibility);
			    $fld->setPossibleValues([
			        'mandatory1' => $this->lang['formFields']['visibility']['mandatory1'],
			        'mandatory2' => $this->lang['formFields']['visibility']['mandatory2'],
			        'optional'	=> $this->lang['formFields']['visibility']['optional'],
			        'invisible'	=> $this->lang['formFields']['visibility']['invisible'],
			    ]);

			    $details = "";
			    if ($field['remarkVar']) {
			        $details .= sprintf($this->lang['formFields']['rmVar'], $field['remarkVar'])."\n";
			    }
			    if ($field['tagSabre']) {
			        $details .= "Sabre: ".$field['tagSabre']."\n";
			    }
			    $fld->setDetails(nl2br(trim($details)));
			    $fld->setTooltip($field['description']);
                //$fld->setCustomHTML("<dfn style=\"display:inline;\">{$field['type']}</dfn>");
			    $fieldset3->addElement($fld);
			}
		}


		// template 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;
	}

}
