<?php

namespace Modules\Admin\DocumentTemplates;

class View_Utils_FormAdmin extends \App\Mvc\View_Utils_FormAbstract
{

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

		// load globals
		$CFG   = \Engine\Loader_Cfg::getInstance()->getGlobal();

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

		$form = \Engine\Form\General::create();
		$fieldset = \Engine\Form\Container\Fieldset::create();
		$form->addContainer($fieldset);

		// document type
		$docType = new \App\Form\Input\Select_DocumentType($this->lang['formFields']['docType'], 'docType', $this->values['docType']);
		$docType->importFromConfig($entCfg['docType']);
		if ($this->controller->getAction() == 'insert') {
			$docType->addEvent('onChange', " $('fileName').value='';");
		}
		$docType->setDefaultOptionText($this->lang['formFields']['docType_default']);
		$form->addElement($docType);

		// document status
		$activeArr = ['1'=> $this->lang['formFields']['activeYes'], '0'=> $this->lang['formFields']['activeNo']];
		$active = new \Engine\Form\Input\Radio($this->lang['formFields']['active'], 'active', $this->values['active']);
		$active->setPossibleValues($activeArr);
		$form->addElement($active);

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

		// document format
		$formatArr[''] = $this->lang['formFields']['format_default'];
		if ($CFG['documents']['formats']) {
			foreach ($CFG['documents']['formats'] as $type=>$arr) {
				if ($arr['active']) {
					$formatArr[$type] = strtoupper($type);
				}
			}
		}
		$format = new \Engine\Form\Input\Select($this->lang['formFields']['format'], 'format', $this->values['format']);
		$format->importFromConfig($entCfg['format']);
		$format->addEvent('onChange', " $('extension').value =$('format').value;");
		$format->setPossibleValues($formatArr);
		$form->addElement($format);

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

		// document download name
		$fileName = new \Engine\Form\Input\Text($this->lang['formFields']['fileName'], 'fileName', $this->values['fileName']);
		$fileName->setTooltip($this->lang['formFields']['tooltipFileName']);
		$form->addElement($fileName);


		///////////////////////////////////////////////////////////////////////////////////////////
		// get active pivots (and associate its documents)
		$pivotsList = [];
		foreach (\App\Config\IssueDocuments::getInstance()->getAll() as $dt=>$docDet) {
			if ($docDet['active']) {
				$pivotsList[$docDet['pivot']][$dt] = $docDet['name'];
			}
		}

		// get suggestions for vars depending on pivot
		foreach ($pivotsList as $pivot=>$docsList) {

			$docCfgObj=\App\GenDocument\Vars\PivotBuilderConfig::fromPivot($pivot);
			$componentsList = [];
			foreach ($docCfgObj->getAll() as $docFieldId => $docFieldData){
				if ($docFieldData['disabled']) continue;

				$category = $docCfgObj->getCategoryName($docFieldData['category']);

				if (!isset($componentsList[$category]))
					$componentsList[$category] = [];

				if (!isset($componentsList[$category][$docFieldId]))
					$componentsList[$category][$docFieldId] = $docCfgObj->getName($docFieldId);
			}
			$elementList[$pivot] = $componentsList;
		}
		///////////////////////////////////////////////////////////////////////////////////////////
		if ($pivotsList['invoice']) {
			$inputInvoice = new \Engine\Form\Input\Select(null, 'fileNameInvoice', $this->values['fileNameAddInvoice']);
			$inputInvoice->setDefaultOptionText($this->lang['formFields']['defaultFileNameAdd']);
			$inputInvoice->addEvent('onChange', " $('fileName').value +='%'+$('fileNameInvoice').value+'%'; $('fileNameInvoice').value='';");
			$inputInvoice->showDefaultOption();
			$inputInvoice->setPossibleValues($elementList['invoice']);
			$form->addElement($inputInvoice);
		}

		if ($pivotsList['service']) {
			$inputService = new \Engine\Form\Input\Select(null, 'fileNameAddService', $this->values['fileNameAddService']);
			$inputService->setDefaultOptionText($this->lang['formFields']['defaultFileNameAdd']);
			$inputService->addEvent('onChange', " $('fileName').value +='%'+$('fileNameAddService').value+'%'; $('fileNameAddService').value='';");
			$inputService->showDefaultOption();
			$inputService->setPossibleValues($elementList['service']);
			$form->addElement($inputService);
		}

		if ($pivotsList['cashing']) {
			$inputCashing = new \Engine\Form\Input\Select(null, 'fileNameAddCashing', $this->values['fileNameAddCashing']);
			$inputCashing->setDefaultOptionText($this->lang['formFields']['defaultFileNameAdd']);
			$inputCashing->addEvent('onChange', " $('fileName').value +='%'+$('fileNameAddCashing').value+'%'; $('fileNameAddCashing').value='';");
			$inputCashing->showDefaultOption();
			$inputCashing->setPossibleValues($elementList['cashing']);
			$form->addElement($inputCashing);
		}

		if ($pivotsList['payment']) {
			$inputPayment = new \Engine\Form\Input\Select(null, 'fileNameAddPayment', $this->values['fileNameAddPayment']);
			$inputPayment->setDefaultOptionText($this->lang['formFields']['defaultFileNameAdd']);
			$inputPayment->addEvent('onChange', " $('fileName').value +='%'+$('fileNameAddPayment').value+'%'; $('fileNameAddPayment').value='';");
			$inputPayment->showDefaultOption();
			$inputPayment->setPossibleValues($elementList['payment']);
			$form->addElement($inputPayment);
		}

		if ($pivotsList['cashBookDoc']) {
			$inputCashbookDoc = new \Engine\Form\Input\Select(null, 'fileNameAddCashbookDoc', $this->values['fileNameAddCashbookDoc']);
			$inputCashbookDoc->setDefaultOptionText($this->lang['formFields']['defaultFileNameAdd']);
			$inputCashbookDoc->addEvent('onChange', " $('fileName').value +='%'+$('fileNameAddCashbookDoc').value+'%'; $('fileNameAddCashbookDoc').value='';");
			$inputCashbookDoc->showDefaultOption();
			$inputCashbookDoc->setPossibleValues($elementList['cashBookDoc']);
			$form->addElement($inputCashbookDoc);
		}

		if ($pivotsList['order']) {
			$inputOrder = new \Engine\Form\Input\Select(null, 'fileNameAddOrder', $this->values['fileNameAddOrder']);
			$inputOrder->setDefaultOptionText($this->lang['formFields']['defaultFileNameAdd']);
			$inputOrder->addEvent('onChange', " $('fileName').value +='%'+$('fileNameAddOrder').value+'%'; $('fileNameAddOrder').value='';");
			$inputOrder->showDefaultOption();
			$inputOrder->setPossibleValues($elementList['order']);
			$form->addElement($inputOrder);
		}

		if ($pivotsList['balance']) {
			$inputBalance = new \Engine\Form\Input\Select(null, 'fileNameAddBalance', $this->values['fileNameAddBalance']);
			$inputBalance->setDefaultOptionText($this->lang['formFields']['defaultFileNameAdd']);
			$inputBalance->addEvent('onChange', " $('fileName').value +='%'+$('fileNameAddBalance').value+'%'; $('fileNameAddBalance').value='';");
			$inputBalance->showDefaultOption();
			$inputBalance->setPossibleValues($elementList['balance']);
			$form->addElement($inputBalance);
		}

		$controlledElems = [];
		foreach($pivotsList as $pivot=>$docsList) {
			switch($pivot){
				case 'invoice':
					foreach($docsList as $dt=>$docName) {
						$controlledElems[$dt] = [$inputInvoice];
					}
				break;

				case 'service':
					foreach($docsList as $dt=>$docName) {
						$controlledElems[$dt] = [$inputService];
					}
				break;

				case 'order':
					foreach($docsList as $dt=>$docName) {
						$controlledElems[$dt] = [$inputOrder];
					}
				break;

				case 'cashing':
					foreach($docsList as $dt=>$docName) {
						$controlledElems[$dt] = [$inputCashing];
					}
				break;

				case 'payment':
					foreach($docsList as $dt=>$docName) {
						$controlledElems[$dt] = [$inputPayment];
					}
				break;

				case 'cashBookDoc':
					foreach($docsList as $dt=>$docName) {
						$controlledElems[$dt] = [$inputCashbookDoc];
					}
				break;

				case 'balance':
					foreach($docsList as $dt=>$docName) {
						$controlledElems[$dt] = [$inputBalance];
					}
				break;
			}
		}

		$docType->setControlledElements($controlledElems, 'hide');

		$form->addElement(new \Engine\Form\Input\Hidden('', $this->submitButtonName, 1));

		return $form;
	}
}