<?php

namespace Modules\Admin\Offices;

class View_Utils_FormAdmin extends \App\Mvc\View_Utils_FormAbstract
{

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

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

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

		// NEW CONTAINER ----------------------------------------------------------------------------------
		$c1 = \Engine\Form\Container\Fieldset::create($this->lang['formFields']['officeDetailsData']);
		$form->addContainer($c1);

		// OFFICE NAME
		$fld_name = new \Engine\Form\Input\Text($this->lang['formFields']['name'], 'name', $this->values['name']);
		$fld_name->setMandatory($cfg['name']['mandatory']);
		$fld_name->setInvisible($cfg['name']['invisible']);
		$fld_name->importFromConfig($cfg['name']);
		$c1->addElement($fld_name);

		// OFFICE IATA CODE
		$fld_iataCode = new \Engine\Form\Input\Cloned_Text($this->lang['formFields']['iataCode'], 'iataCode', $this->values['iataCode']);
		$fld_iataCode->setMandatory($cfg['iataCode']['mandatory']);
		$fld_iataCode->setInvisible($cfg['iataCode']['invisible']);
		$fld_iataCode->importFromConfig($cfg['iataCode']);
		$c1->addElement($fld_iataCode);

		// REMARKS
		$fld_remarks = new \App\Form\Input\Remarks($this->lang['formFields']['remarks'], 'remarks', $this->values['remarks']);
		$fld_remarks->setMandatory($cfg['remarks']['mandatory']);
		$fld_remarks->setInvisible($cfg['remarks']['invisible']);
		$fld_remarks->importFromConfig($cfg['remarks']);
		$fld_remarks->setRemarksType('office');
		$c1->addElement($fld_remarks);

		// COMMENTS
		$fld_comments = new \Engine\Form\Input\Textarea($this->lang['formFields']['comments'], 'comments', $this->values['comments']);
		$fld_comments->setMandatory($cfg['comments']['mandatory']);
		$fld_comments->setInvisible($cfg['comments']['invisible']);
		$fld_comments->importFromConfig($cfg['comments']);
		$c1->addElement($fld_comments);

		// NEW CONTAINER ----------------------------------------------------------------------------------
		$c2 = \Engine\Form\Container\Fieldset::create($this->lang['formFields']['officeContactsData']);
		$form->addContainer($c2);

		// LOCATION
		$fld_location = new \App\Form\Input\Autocomplete_CountryCity($this->lang['formFields']['location'], 'location', $this->values['location']);
		$fld_location->setMandatory($cfg['countryCode']['mandatory']);
		$fld_location->setInvisible($cfg['countryCode']['invisible']);
		$c2->addElement($fld_location);

		// ADDRESS
		$fld_address = new \Engine\Form\Input\Textarea($this->lang['formFields']['address'], 'address', $this->values['address']);
		$fld_address->setMandatory($cfg['address']['mandatory']);
		$fld_address->setInvisible($cfg['address']['invisible']);
		$fld_address->importFromConfig($cfg['address']);
		$c2->addElement($fld_address);

		// DELIVERY ADDRESS
		$fld_deliveryAddress = new \Engine\Form\Input\Textarea($this->lang['formFields']['deliveryAddress'], 'deliveryAddress', $this->values['deliveryAddress']);
		$fld_deliveryAddress->setMandatory($cfg['deliveryAddress']['mandatory']);
		$fld_deliveryAddress->setInvisible($cfg['deliveryAddress']['invisible']);
		$fld_deliveryAddress->importFromConfig($cfg['deliveryAddress']);
		$c2->addElement($fld_deliveryAddress);

		// ZIP CODE
		$fld_zipCode = new \Engine\Form\Input\Text($this->lang['formFields']['zipCode'], 'zipCode', $this->values['zipCode']);
		$fld_zipCode->setMandatory($cfg['zipCode']['mandatory']);
		$fld_zipCode->setInvisible($cfg['zipCode']['invisible']);
		$fld_zipCode->importFromConfig($cfg['zipCode']);
		$c2->addElement($fld_zipCode);

		// TELEPHONE
		$fld_telephone = new \Engine\Form\Input\Text($this->lang['formFields']['telephone'], 'telephone', $this->values['telephone']);
		$fld_telephone->setMandatory($cfg['telephone']['mandatory']);
		$fld_telephone->setInvisible($cfg['telephone']['invisible']);
		$fld_telephone->importFromConfig($cfg['telephone']);
		$c2->addElement($fld_telephone);

		// FAX
		$fld_fax = new \Engine\Form\Input\Text($this->lang['formFields']['fax'], 'fax', $this->values['fax']);
		$fld_fax->setMandatory($cfg['fax']['mandatory']);
		$fld_fax->setInvisible($cfg['fax']['invisible']);
		$fld_fax->importFromConfig($cfg['fax']);
		$c2->addElement($fld_fax);

		// EMAIL
		$fld_email = new \Engine\Form\Input\Text($this->lang['formFields']['email'], 'email', $this->values['email']);
		$fld_email->setMandatory($cfg['email']['mandatory']);
		$fld_email->setInvisible($cfg['email']['invisible']);
		$fld_email->importFromConfig($cfg['email']);
		$c2->addElement($fld_email);

		// WEB
		$fld_web = new \Engine\Form\Input\Text($this->lang['formFields']['web'], 'web', $this->values['web']);
		$fld_web->setMandatory($cfg['web']['mandatory']);
		$fld_web->setInvisible($cfg['web']['invisible']);
		$fld_web->importFromConfig($cfg['web']);
		$c2->addElement($fld_web);

		// NEW CONTAINER
		$c3 = \Engine\Form\Container\Fieldset::create($this->lang['formFields']['officeBankAccounts']);
		$form->addContainer($c3);

		// list of agency bank accounts
		$fld_bankAccounts = new \Engine\Form\Input\Checkboxes($this->lang['formFields']['bankAccounts'], 'bankAccountsIds', $this->values['bankAccountsIds']);
		$agencyBankAccountsObj = new \App\EntityList\AgencyBankAccounts();
		$agencyBankAccountsObj->setColumns(['idBank', 'account', 'currency', 'description']);
		$agencyBankAccountsObj->setOrderBy('id', 'ASC');
		$agencyBankAccountsArray = $agencyBankAccountsObj->exportRecords();

		$bankAccountsList = [];

		foreach ($agencyBankAccountsArray as $bankAccount) {
			$baDet = $bankAccount['account'];

			if ($bankAccount['currency']) {
				$baDet.= ' ['.$bankAccount['currency'].']';
			}

			if ($bankAccount['idBank']) {
				$baDet.= ' - '.\Entity\Bank::select($bankAccount['idBank'])->name;
			}

			if ($bankAccount['description']) {
				$baDet.= ' - '.$bankAccount['description'];
			}

			$bankAccountsList[$bankAccount['id']] = $baDet;
		}

		$fld_bankAccounts->setPossibleValues($bankAccountsList);
		$fld_bankAccounts->setStyle('renderMode', 'block');
		$fld_bankAccounts->setMandatory($cfg['bankAccountsIds']['mandatory']);
		$fld_bankAccounts->setInvisible($cfg['bankAccountsIds']['invisible']);
		$fld_bankAccounts->importFromConfig($cfg['bankAccountsIds']);
		$c3->addElement($fld_bankAccounts);

		// default bank accounts
		$fld_defaultBankAccount = new \App\Form\Input\Select_AgencyBankAccount($this->lang['formFields']['defaultBankAccount'], 'defaultBankAccount', $this->values['defaultBankAccount']);
		$fld_defaultBankAccount->showDefaultOption(true);
		$fld_defaultBankAccount->setMandatory($cfg['defaultBankAccount']['mandatory']);
		$fld_defaultBankAccount->setInvisible($cfg['defaultBankAccount']['invisible']);
		$fld_defaultBankAccount->importFromConfig($cfg['defaultBankAccount']);
		$c3->addElement($fld_defaultBankAccount);

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

		return $form;
	}
}