<?php
namespace Modules\Admin\UserProfile\Options;

class View_Utils_FormAdmin extends \App\Mvc\View_Utils_FormAbstract
{
	public function getForm()
	{
		// global cfgs
		$_LANG = \App\Loader_Lang::getInstance()->getAll();

		// FORM
		$form = \Engine\Form\General::create();

		// TABS
		$tabs = \Engine\Form\Container\Tabs::create();
		$form->addContainer($tabs);

		// ORDER AND SERVICES TAB /////////////////////////////////////////////////////////////////
		$tab1 = \Engine\Form\Container\Tabs_Tab::create($this->lang['index']['tabs']['orderAndServices']);
		$tabs->addContainer($tab1);

		// FIRST FIELDSET -------------------------------------------------------------------------
		$f11 = new \Engine\Form\Container\Fieldset($this->lang['index']['fieldset']['orderAndServices1']);
		$tab1->addContainer($f11);

		// addServOnOrder_defaultServiceCode
		$addServOnOrder_defaultServiceCode = new \App\Form\Input\Select_Service($this->lang['index']['formFields']['addServOnOrder_defaultServiceCode'], 'addServOnOrder_defaultServiceCode', $this->values['addServOnOrder_defaultServiceCode']);
		$addServOnOrder_defaultServiceCode->setTooltip($this->lang['index']['tooltips']['addServOnOrder_defaultServiceCode']);
		$addServOnOrder_defaultServiceCode->setWidth(200);
		$f11->addElement($addServOnOrder_defaultServiceCode);

		// addServOnOrder_defaultRemarks
		$addServOnOrder_defaultRemarks = new \App\Form\Input\Remarks($this->lang['index']['formFields']['addServOnOrder_defaultRemarks'], 'addServOnOrder_defaultRemarks', $this->values['addServOnOrder_defaultRemarks']);
		$addServOnOrder_defaultRemarks->setRemarksType('service');
		$addServOnOrder_defaultRemarks->setTooltip($this->lang['index']['tooltips']['addServOnOrder_defaultRemarks']);
		$addServOnOrder_defaultRemarks->setWidth(200);
		$f11->addElement($addServOnOrder_defaultRemarks);

		// SECOND FIELDSET ------------------------------------------------------------------------
		$f12 = new \Engine\Form\Container\Fieldset($this->lang['index']['fieldset']['orderAndServices2']);
		$tab1->addContainer($f12);

		// addServOnRes_defaultServiceCode
		$addServOnRes_defaultServiceCode = new \App\Form\Input\Select_Service($this->lang['index']['formFields']['addServOnRes_defaultServiceCode'], 'addServOnRes_defaultServiceCode', $this->values['addServOnRes_defaultServiceCode']);
		$addServOnRes_defaultServiceCode->setTooltip($this->lang['index']['tooltips']['addServOnRes_defaultServiceCode']);
		$addServOnRes_defaultServiceCode->setWidth(200);
		$f12->addElement($addServOnRes_defaultServiceCode);

		// addServOnRes_paxSelection
		$paxArr = [
			'' => 		$this->lang['index']['pax']['default'],
			'all' => 	$this->lang['index']['pax']['all'],
			'none' => 	$this->lang['index']['pax']['none'],
		];
		$addServOnRes_paxSelection = new \Engine\Form\Input\Select($this->lang['index']['formFields']['addServOnRes_paxSelection'], 'addServOnRes_paxSelection', $this->values['addServOnRes_paxSelection']);
		$addServOnRes_paxSelection->setTooltip($this->lang['index']['tooltips']['addServOnRes_paxSelection']);
		$addServOnRes_paxSelection->setPossibleValues($paxArr);
		$addServOnRes_paxSelection->setWidth(200);
		$f12->addElement($addServOnRes_paxSelection);

		// addServOnRes_defaultRemarks
		$addServOnRes_defaultRemarks = new \App\Form\Input\Remarks($this->lang['index']['formFields']['addServOnRes_defaultRemarks'], 'addServOnRes_defaultRemarks', $this->values['addServOnRes_defaultRemarks']);
		$addServOnRes_defaultRemarks->setRemarksType('service');
		$addServOnRes_defaultRemarks->setTooltip($this->lang['index']['tooltips']['addServOnRes_defaultRemarks']);
		$addServOnRes_defaultRemarks->setWidth(200);
		$f12->addElement($addServOnRes_defaultRemarks);

		// defaultPackageServiceCode
		$defaultPackageServiceCode = new \App\Form\Input\Select_Service($this->lang['index']['formFields']['defaultPackageServiceCode'], 'defaultPackageServiceCode', $this->values['defaultPackageServiceCode']);
		$defaultPackageServiceCode->setTooltip($this->lang['index']['tooltips']['defaultPackageServiceCode']);
		$defaultPackageServiceCode->setWidth(200);
		$f12->addElement($defaultPackageServiceCode);

		// QUICK-INVOICING FROM ORDER FORM --------------------------------------------------------
		$f13 = new \Engine\Form\Container\Fieldset($this->lang['index']['fieldset']['quickInvoiceInOrderForm']);
		$tab1->addContainer($f13);

		$quickInvInfo = new \Engine\Form\Input\Label();
		$quickInvInfo->setStyle('noLabel', true);
		$quickInvInfo->setDetails($this->lang['index']['fieldsetLabel']['quickInvoiceInOrderForm']);
		$f13->addElement($quickInvInfo);

		// invoice series
		$invSeries = new \App\Form\Input\Select_DocumentSeries($this->lang['index']['formFields']['invoiceOnOrderForm_idSeries'], 'invoiceOnOrderForm_idSeries', $this->values['invoiceOnOrderForm_idSeries']);
		$issueDocsCfgObj = \App\Config\IssueDocuments::getInstance();
		$invoiceDocsList = array_filter(
			array_keys($issueDocsCfgObj->getInvoiceDocsList()),
			function ($docType) use ($issueDocsCfgObj) {
				return $issueDocsCfgObj->isMainDocOnPivot($docType);
			}
		) ;
		$invSeries->setDocTypesFilter($invoiceDocsList);
		$invSeries->setIdUser(CURRENT_ID_USER);
		$invSeries->showDefaultOption();
		$f13->addElement($invSeries);

		// issue doc
		$invIssueDoc = new \App\Form\Input\Select_DocumentType($this->lang['index']['formFields']['invoiceOnOrderForm_invoiceDocType'], 'invoiceOnOrderForm_invoiceDocType', $this->values['invoiceOnOrderForm_invoiceDocType']);
		$invIssueDoc->setPivot('invoice');
		$invIssueDoc->showDefaultOption();
		$f13->addElement($invIssueDoc);


		// invoice template
		$invTemplate = new \App\Form\Input\Select_DocumentTemplate($this->lang['index']['formFields']['invoiceOnOrderForm_idTemplate'], 'invoiceOnOrderForm_idTemplate', $this->values['invoiceOnOrderForm_idTemplate']);
		$invTemplate->setDocTypeFilter($invoiceDocsList);
		$invTemplate->showDefaultOption();
		$f13->addElement($invTemplate);


		// TEMPLATE DOC TAB ///////////////////////////////////////////////////////////////////////
		$tab2 = \Engine\Form\Container\Tabs_Tab::create($this->lang['index']['tabs']['docTemplates']);
		$tabs->addContainer($tab2);

		foreach (\App\Config\IssueDocuments::getInstance()->getDocsListByPivot() as $pivot=>$docList) {
			$pivotContainer = \Engine\Form\Container\Fieldset::create(sprintf($this->lang['index']['fieldset']['field_docTplPivotFieldset'], $_LANG['documentPivotName'][$pivot]));
			$tab2->addContainer($pivotContainer);
			// add each doc type
			foreach($docList as $docType=>$docName) {

				$fld=new \Engine\Form\Input\Select($docName, "docTemplate[$docType]", $this->values['docTemplate'][$docType]);
				$fld->setWidth(400);
				$possibVals= [''=>$this->lang['index']['docTemplate']['default']];
				$docTmplObj = new \App\EntityList\DocumentTemplates();
				$docTmplObj->setColumns(['tpls.name', 'tpls.format', 'tpls.extension', 'tpls.fieldsDetails', 'tpls.customFieldsDetails', 'accessUser.idUser']);
				$docTmplObj->setFilter('tpls.docType', '=', $docType);
				$docTmplObj->setFilter('tpls.active', '=', 1);
				$docTmplObj->setCustomFilter('accessUser.idUser = ? OR tpls.allUsers = ? ', [CURRENT_ID_USER, 1]);
				$docTmplObj->setOrderBy('tpls.id', 'ASC');
				$docTmplArray = $docTmplObj->exportRecords();

				$templateArray = [];
				if ($docTmplArray) {
					foreach ($docTmplArray as $docTmplInfos) {
						$templateArray[$docTmplInfos['id']] = [
							'name' => 					$docTmplInfos['name'],
							'format'	=> 				$docTmplInfos['format'],
							'extension' =>				$docTmplInfos['extension'],
							'fieldsDetails'=> 			$docTmplInfos['fieldsDetails'],
							'customFieldsDetails' => 	$docTmplInfos['customFieldsDetails'],
						];
					}
				}
				foreach ($templateArray as $idTpl => $tplDet) {
					$possibVals[$idTpl]=$tplDet['name']." [{$tplDet['format']}]";
				}
				$fld->setPossibleValues($possibVals);
				$pivotContainer->addElement($fld);

			}
		}



		// DOCUMENT SERIES TAB ////////////////////////////////////////////////////////////////////
		$tab3 = \Engine\Form\Container\Tabs_Tab::create($this->lang['index']['tabs']['docSeries']);
		$tabs->addContainer($tab3);

		// get series grouped by doc type
		$serListObj=new \App\EntityList\DocumentSeries();
		$serListObj->setColumns(['seriesDocType.documentType', 'series.code', 'series.idOffice', 'series.nextNumber']);
		$serListObj->setCustomFilter('series.idOffice=? OR series.idOffice=?', [CURRENT_ID_OFFICE, 0]);
		$serListObj->setFilter('series.status', '=', 'WRK');
		$serListObj->setFilter('series.validEndDate', '>=', TODAY_ISO);
		$serListObj->setOrderBy('series.code', 'ASC');
		$serList=$serListObj->getExecution();
		$docSeriesAssoc=[];
		foreach($serList as $ser) {
			$docSeriesAssoc[$ser['documentType']][$ser['id']]=$ser['code'].' ['.$ser['nextNumber'].']';
		}

		// go throgh each document type and list its doc series
		foreach (\App\Config\IssueDocuments::getInstance()->getDocsListByPivot() as $pivot=>$docList) {

			$pivotContainer = \Engine\Form\Container\Fieldset::create(sprintf($this->lang['index']['fieldset']['field_docSeriesPivotFieldset'], $_LANG['documentPivotName'][$pivot]));
			$tab3->addContainer($pivotContainer);

			// add each doc type
			foreach($docList as $docType=>$docName) {
				// doc is active and it is configured to have series
				$docNumberType = \App\Config\IssueDocuments::getInstance()->getDocNumberType($docType);
				if ($docNumberType=='chooseSeries' or $docNumberType=='pivotSeries') {
					$fld=new \Engine\Form\Input\Select($docName, "docSeries[$docType]", $this->values['docSeries'][$docType]);
					$fld->showDefaultOption()->setDefaultOptionText($this->lang['index']['docSeries']['default']);
					$fld->setWidth(300);
					// set the current doc type series and merge the list with the series associated to all doc types
					$series=(array)$docSeriesAssoc[$docType];
					asort($series);
					$fld->setPossibleValues($series);
					$pivotContainer->addElement($fld);
				}
			}

		}

		// DOCUMENT EMAIL TAB /////////////////////////////////////////////////////////////////////
		$tab4 = \Engine\Form\Container\Tabs_Tab::create($this->lang['index']['tabs']['docEmail']);
		$tabs->addContainer($tab4);

		// fieldset
		$f41 = new \Engine\Form\Container\Fieldset();
		$tab4->addContainer($f41);

		// BCC
		$emailDocs_selfBcc = new \Engine\Form\Input\Checkbox($this->lang['index']['formFields']['emailDocs_selfBcc'], 'emailDocs_selfBcc', $this->values['emailDocs_selfBcc']);
		$emailDocs_selfBcc->setTooltip($this->lang['index']['tooltips']['emailDocs_selfBcc']);
		$f41->addElement($emailDocs_selfBcc);

		// confirmation
		$emailDocs_readReceipt = new \Engine\Form\Input\Checkbox($this->lang['index']['formFields']['emailDocs_readReceipt'], 'emailDocs_readReceipt', $this->values['emailDocs_readReceipt']);
		$emailDocs_readReceipt->setTooltip($this->lang['index']['tooltips']['emailDocs_readReceipt']);
		$f41->addElement($emailDocs_readReceipt);

		// order email subject
		$docEmail_order_defaultSubject = new \Engine\Form\Input\Text($this->lang['index']['formFields']['docEmail_order_defaultSubject'], 'docEmail_order_defaultSubject', $this->values['docEmail_order_defaultSubject']);
		$f41->addElement($docEmail_order_defaultSubject);

		// order email body
		$docEmail_order_defaultBody = new \App\Form\Input\HtmlArea($this->lang['index']['formFields']['docEmail_order_defaultBody'], 'docEmail_order_defaultBody', $this->values['docEmail_order_defaultBody']);
		$docEmail_order_defaultBody->setHeight('100');
		$f41->addElement($docEmail_order_defaultBody);

		// service email subject
		$docEmail_service_defaultSubject = new \Engine\Form\Input\Text($this->lang['index']['formFields']['docEmail_service_defaultSubject'], 'docEmail_service_defaultSubject', $this->values['docEmail_service_defaultSubject']);
		$f41->addElement($docEmail_service_defaultSubject);

		// service email body
		$docEmail_service_defaultBody = new \App\Form\Input\HtmlArea($this->lang['index']['formFields']['docEmail_service_defaultBody'], 'docEmail_service_defaultBody', $this->values['docEmail_service_defaultBody']);
		$docEmail_service_defaultBody->setHeight('100');
		$f41->addElement($docEmail_service_defaultBody);

		// invoice email subject
		$docEmail_invoice_defaultSubject = new \Engine\Form\Input\Text($this->lang['index']['formFields']['docEmail_invoice_defaultSubject'], 'docEmail_invoice_defaultSubject', $this->values['docEmail_invoice_defaultSubject']);
		$f41->addElement($docEmail_invoice_defaultSubject);

		// invoice email body
		$docEmail_invoice_defaultBody = new \App\Form\Input\HtmlArea($this->lang['index']['formFields']['docEmail_invoice_defaultBody'], 'docEmail_invoice_defaultBody', $this->values['docEmail_invoice_defaultBody']);
		$docEmail_invoice_defaultBody->setHeight('100');
		$f41->addElement($docEmail_invoice_defaultBody);

		// cashing email subject
		$docEmail_cashing_defaultSubject = new \Engine\Form\Input\Text($this->lang['index']['formFields']['docEmail_cashing_defaultSubject'], 'docEmail_cashing_defaultSubject', $this->values['docEmail_cashing_defaultSubject']);
		$f41->addElement($docEmail_cashing_defaultSubject);

		// cashing email body
		$docEmail_cashing_defaultBody = new \App\Form\Input\HtmlArea($this->lang['index']['formFields']['docEmail_cashing_defaultBody'], 'docEmail_cashing_defaultBody', $this->values['docEmail_cashing_defaultBody']);
		$docEmail_cashing_defaultBody->setHeight('100');
		$f41->addElement($docEmail_cashing_defaultBody);

		// payment email subject
		$docEmail_payment_defaultSubject = new \Engine\Form\Input\Text($this->lang['index']['formFields']['docEmail_payment_defaultSubject'], 'docEmail_payment_defaultSubject', $this->values['docEmail_payment_defaultSubject']);
		$f41->addElement($docEmail_payment_defaultSubject);

		// payment email body
		$docEmail_payment_defaultBody = new \App\Form\Input\HtmlArea($this->lang['index']['formFields']['docEmail_payment_defaultBody'], 'docEmail_payment_defaultBody', $this->values['docEmail_payment_defaultBody']);
		$docEmail_payment_defaultBody->setHeight('100');
		$f41->addElement($docEmail_payment_defaultBody);

		// cashbook email subject
		$docEmail_cashBookDoc_defaultSubject = new \Engine\Form\Input\Text($this->lang['index']['formFields']['docEmail_cashBookDoc_defaultSubject'], 'docEmail_cashBookDoc_defaultSubject', $this->values['docEmail_cashBookDoc_defaultSubject']);
		$f41->addElement($docEmail_cashBookDoc_defaultSubject);

		// cashbook email body
		$docEmail_cashBookDoc_defaultBody = new \App\Form\Input\HtmlArea($this->lang['index']['formFields']['docEmail_cashBookDoc_defaultBody'], 'docEmail_cashBookDoc_defaultBody', $this->values['docEmail_cashBookDoc_defaultBody']);
		$docEmail_cashBookDoc_defaultBody->setHeight('100');
		$f41->addElement($docEmail_cashBookDoc_defaultBody);

		// imported service email subject
		$docEmail_importedService_defaultSubject = new \Engine\Form\Input\Text($this->lang['index']['formFields']['docEmail_importedService_defaultSubject'], 'docEmail_importedService_defaultSubject', $this->values['docEmail_importedService_defaultSubject']);
		$f41->addElement($docEmail_importedService_defaultSubject);

		// imported service email body
		$docEmail_importedService_defaultBody = new \App\Form\Input\HtmlArea($this->lang['index']['formFields']['docEmail_importedService_defaultBody'], 'docEmail_importedService_defaultBody', $this->values['docEmail_importedService_defaultBody']);
		$docEmail_importedService_defaultBody->setHeight('100');
		$f41->addElement($docEmail_importedService_defaultBody);


		// CORPORATION TAB ////////////////////////////////////////////////////////////////////////
		$tab5 = \Engine\Form\Container\Tabs_Tab::create($this->lang['index']['tabs']['corporation']);
		$tabs->addContainer($tab5);

		$f51 = new \Engine\Form\Container\Fieldset();
		$tab5->addContainer($f51);

		// corporate remarks
		$corporate_defaultRemarks = new \App\Form\Input\Remarks($this->lang['index']['formFields']['corporate_defaultRemarks'], 'corporate_defaultRemarks', $this->values['corporate_defaultRemarks']);
		$corporate_defaultRemarks->setRemarksType('corporate');
		$f51->addElement($corporate_defaultRemarks);


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

		return $form;
	}

}