<?php

namespace Modules\Partners\Corporates;

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

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

		$actions = [
			'update' => $this->lang['bulkUpdate']['actionValues']['update'],
			'delete' => $this->lang['bulkUpdate']['actionValues']['delete'],
		];

		// KAM UPDATE FIELDSET --------------------------------------------------------------------
		$fieldset = \Engine\Form\Container\Fieldset::create($this->lang['bulkUpdate']['fieldset']['kam']);
		$form->addContainer($fieldset);

		// select kam action
		$selectKamAction = new \Engine\Form\Input\Select($this->lang['bulkUpdate']['formFields']['kamAction'], 'kamAction', $this->values['kamAction']);
		$selectKamAction->setPossibleValues($actions);
		$selectKamAction->setDefaultOptionText($this->lang['bulkUpdate']['actionValues']['default']);
		$selectKamAction->showDefaultOption(true);
		$fieldset->addElement($selectKamAction);

		// user autocomplete
		$user = new \App\Form\Input\Autocomplete_User($this->lang['bulkUpdate']['formFields']['kam'], 'idUser' , $this->values['idUser']);
		$user->setMandatory();
		$fieldset->addElement($user);

		$selectKamAction->setControlledElements(['update'=> [$user]]);


		// SEGMENT UPDATE FIELDSET ----------------------------------------------------------------
		$fieldset = \Engine\Form\Container\Fieldset::create($this->lang['bulkUpdate']['fieldset']['segments']);
		$form->addContainer($fieldset);

		// select segment action
		$selectSegmentAction = new \Engine\Form\Input\Select($this->lang['bulkUpdate']['formFields']['segmentAction'], 'segmentAction', $this->values['segmentAction']);
		$selectSegmentAction->setPossibleValues($actions);
		$selectSegmentAction->setDefaultOptionText($this->lang['bulkUpdate']['actionValues']['default']);
		$selectSegmentAction->showDefaultOption(true);
		$fieldset->addElement($selectSegmentAction);

		// segments
		$segment= new \App\Form\Input\Select_CorporateSegment($this->lang['bulkUpdate']['formFields']['segment'], 'idSegment', $this->values['idSegment']);
		$segment->setMandatory();
		$segment->showDefaultOption();
		$fieldset->addElement($segment);

		$selectSegmentAction->setControlledElements(['update'=> [$segment]]);


		// ACTIVITY DOMAIN UPDATE FIELDSET --------------------------------------------------------
		$fieldset = \Engine\Form\Container\Fieldset::create($this->lang['bulkUpdate']['fieldset']['activityCategoryDomain']);
		$form->addContainer($fieldset);

		$selectActivityCategoryDomainAction = new \Engine\Form\Input\Select($this->lang['bulkUpdate']['formFields']['activityCategoryDomainAction'], 'activityCategoryDomainAction', $this->values['activityCategoryDomainAction']);
		$selectActivityCategoryDomainAction->setPossibleValues($actions);
		$selectActivityCategoryDomainAction->setDefaultOptionText($this->lang['bulkUpdate']['actionValues']['default']);
		$selectActivityCategoryDomainAction->showDefaultOption(true);
		$fieldset->addElement($selectActivityCategoryDomainAction);
		// activity domain
		$fld_activityDomain = new \App\Form\Input\Autocomplete_ClientActivityCategoryAndDomain($this->lang['formFields']['activityCategoryDomain'], 'activityCategoryDomain', $this->values['activityCategoryDomain']);
		$fld_activityDomain->setMandatory();
		$fieldset->addElement($fld_activityDomain);
		$selectActivityCategoryDomainAction->setControlledElements( ['update' => [$fld_activityDomain] ] );


		// GDS PROFILE TEMPLATE UPDATE FIELDSET ---------------------------------------------------
		$fieldset = \Engine\Form\Container\Fieldset::create($this->lang['bulkUpdate']['fieldset']['gdsProfileTemplate']);
		$form->addContainer($fieldset);

		$selectGdsProfileTemplateAction = new \Engine\Form\Input\Select($this->lang['bulkUpdate']['formFields']['gdsProfileTemplateAction'], 'gdsProfileTemplateAction', $this->values['gdsProfileTemplateAction']);
		$selectGdsProfileTemplateAction->setPossibleValues($actions);
		$selectGdsProfileTemplateAction->setDefaultOptionText($this->lang['bulkUpdate']['actionValues']['default']);
		$selectGdsProfileTemplateAction->showDefaultOption(true);
		$fieldset->addElement($selectGdsProfileTemplateAction);

		// gds profile template
		$listObj = new \App\EntityList\GdsProfilesTemplates();
		$listObj->setFilter('category', '=', 'corporate');
		$listObj->setColumns(['name']);
		$tpls = $listObj->exportRecords();

		if ($tpls) {
			$fld_gdsProfileTemp = new \Engine\Form\Input\Select($this->lang['formFields']['idGdsProfileTemplate'], 'idGdsProfileTemplate', $this->values['idGdsProfileTemplate']);
			foreach ($tpls as $tpl) {
				$fld_gdsProfileTemp->setPossibleValue($tpl['id'], $tpl['name']);
			}
			$fld_gdsProfileTemp->showDefaultOption();
			$fld_gdsProfileTemp->setMandatory();
			$fieldset->addElement($fld_gdsProfileTemp);
			$selectGdsProfileTemplateAction->setControlledElements(['update'=> [$fld_gdsProfileTemp]]);
		}


		// CORPORATE COLLECTION PROFILE UPDATE FIELDSET -------------------------------------------
		$fieldset = \Engine\Form\Container\Fieldset::create($this->lang['bulkUpdate']['fieldset']['collectionProfile']);
		$form->addContainer($fieldset);

		$selectCollectionProfileAction = new \Engine\Form\Input\Select($this->lang['bulkUpdate']['formFields']['collectionProfileAction'], 'collectionProfileAction', $this->values['collectionProfileAction']);
		$selectCollectionProfileAction->setPossibleValues($actions);
		$selectCollectionProfileAction->setDefaultOptionText($this->lang['bulkUpdate']['actionValues']['default']);
		$selectCollectionProfileAction->showDefaultOption(true);
		$fieldset->addElement($selectCollectionProfileAction);

		// collection profile template
		$fld_collectionProfile = new \App\Form\Input\Select_CorporateCollectionProfile($this->lang['bulkUpdate']['formFields']['collectionProfileCode'], 'collectionProfileCode', $this->values['collectionProfileCode']);
		$fld_collectionProfile->showDefaultOption();
		$fld_collectionProfile->setMandatory();
		$fld_collectionProfile->setTooltip($this->lang['bulkUpdate']['formFields']['collectionProfileTooltip']);
		$fieldset->addElement($fld_collectionProfile);
		$selectCollectionProfileAction->setControlledElements( [ 'update' => [$fld_collectionProfile] ] );


		// COLLECTION REPONSIBLE UPDATE FIELDSET --------------------------------------------------
		$fieldset = \Engine\Form\Container\Fieldset::create($this->lang['bulkUpdate']['fieldset']['collectionResponsible']);
		$form->addContainer($fieldset);

		// select collection responsible action
		$selectCollectionResponsibleAction = new \Engine\Form\Input\Select($this->lang['bulkUpdate']['formFields']['collectionResponsibleAction'], 'collectionResponsibleAction', $this->values['collectionResponsibleAction']);
		$selectCollectionResponsibleAction->setPossibleValues($actions);
		$selectCollectionResponsibleAction->setDefaultOptionText($this->lang['bulkUpdate']['actionValues']['default']);
		$selectCollectionResponsibleAction->showDefaultOption(true);
		$fieldset->addElement($selectCollectionResponsibleAction);

		// collection responsible autocomplete
		$user = new \App\Form\Input\Autocomplete_User($this->lang['bulkUpdate']['formFields']['collectionResponsible'], 'idCollectionResponsible' , $this->values['idCollectionResponsible']);
		$user->setMandatory();
		$fieldset->addElement($user);

		$selectCollectionResponsibleAction->setControlledElements( [ 'update' => [$user] ] );

		// INVOICE RESPONSIBLE UPDATE FIELDSET ----------------------------------------------------
		$fieldset = \Engine\Form\Container\Fieldset::create($this->lang['bulkUpdate']['fieldset']['invoiceResponsible']);
		$form->addContainer($fieldset);

		// select invoice responsible action
		$selectInvoiceResponsibleAction = new \Engine\Form\Input\Select($this->lang['bulkUpdate']['formFields']['invoiceResponsibleAction'], 'invoiceResponsibleAction', $this->values['invoiceResponsibleAction']);
		$selectInvoiceResponsibleAction->setPossibleValues($actions);
		$selectInvoiceResponsibleAction->setDefaultOptionText($this->lang['bulkUpdate']['actionValues']['default']);
		$selectInvoiceResponsibleAction->showDefaultOption(true);
		$fieldset->addElement($selectInvoiceResponsibleAction);

		// invoice responsible autocomplete
		$invoiceUser = new \App\Form\Input\Autocomplete_User($this->lang['bulkUpdate']['formFields']['invoiceResponsible'], 'idInvoiceResponsible' , $this->values['idInvoiceResponsible']);
		$invoiceUser->setMandatory();
		$fieldset->addElement($invoiceUser);

		$selectInvoiceResponsibleAction->setControlledElements( [ 'update' => [$invoiceUser] ] );


		// CORPORATE COMPENSATION PROFILE ---------------------------------------------------------
		$fieldset = \Engine\Form\Container\Fieldset::create($this->lang['bulkUpdate']['fieldset']['compensation']);
		$form->addContainer($fieldset);

		// select copensation action
		$selectCompensationAction = new \Engine\Form\Input\Select($this->lang['bulkUpdate']['formFields']['compensationAction'], 'autoCompensationAction', $this->values['autoCompensationAction']);
		$selectCompensationAction->setPossibleValues($actions);
		$selectCompensationAction->setDefaultOptionText($this->lang['bulkUpdate']['actionValues']['default']);
		$selectCompensationAction->showDefaultOption(true);
		$fieldset->addElement($selectCompensationAction);

		// compensation selector
		$compensationType = new \Engine\Form\Input\Select($this->lang['bulkUpdate']['formFields']['compensationType'], 'autoCompensationType' , $this->values['autoCompensationType']);
		$compensationType->setMandatory();
		$compensationType->showDefaultOption();
		$compensationType->setPossibleValues([
			'onlyExactCreditNotes' => $this->lang['bulkUpdate']['formFields']['compensateType_onlyExactCreditNotes'],
			'bulk' => $this->lang['bulkUpdate']['formFields']['compensateType_bulk'],
		]);
		$fieldset->addElement($compensationType);

		$selectCompensationAction->setControlledElements( [ 'update'=>[$compensationType] ] );


		// CORPORATE GROUPS UPDATE FIELDSET -------------------------------------------------------
		$fieldset = \Engine\Form\Container\Fieldset::create($this->lang['bulkUpdate']['fieldset']['corporateGroups']);
		$form->addContainer($fieldset);

		// Groups
		$groups = \App\Config\CorporateGroups::getInstance()->getAll();
		if ($groups) {
			foreach ($groups as $idGroup => $groupDetails) {
				// select corporate group action
				$selectGroupAction = new \Engine\Form\Input\Select(sprintf($this->lang['bulkUpdate']['formFields']['groupAction'], $groupDetails['groupName']), "groupAction[{$idGroup}]", $this->values['groupAction'][$idGroup]);
				$selectGroupAction->setPossibleValues($actions);
				$selectGroupAction->setDefaultOptionText($this->lang['bulkUpdate']['actionValues']['default']);
				$selectGroupAction->showDefaultOption(true);
				$fieldset->addElement($selectGroupAction);

				if ($groupDetails['groupElements']) {
					$groupElement = new \Engine\Form\Input\Select(sprintf($this->lang['bulkUpdate']['formFields']['groupElements'], $groupDetails['groupName']) , "idGroupElements[{$idGroup}]", $this->values['idGroupElements']);
					$groupElement->setPossibleValues($groupDetails['groupElements']);
					$groupElement->setMandatory();
					$groupElement->showDefaultOption(true);
					$fieldset->addElement($groupElement);
					$selectGroupAction->setControlledElements(['update' => [$groupElement]]);
				}
			}
		}

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

		return $form;
	}
}