<?php

namespace Modules\RemoteData\InfoBox;

class View_CorporateOffice extends \App\Mvc\View_RemoteDataInfoBoxEntityAbstract
{

	/**
	 * (non-PHPdoc)
	 * @see \App\Mvc\View_RemoteDataInfoBoxEntityAbstract::getEntityRemoteDetails()
	 */
	protected function getEntityRemoteDetails(\App\Entity\SelectAbstract $entity)
	{

		$_LANG = \App\Loader_Lang::getInstance()->getGlobal();

		// the output object
		$output = \Engine\RemoteData\InfoBox\Container_Main::create( sprintf($this->lang['corporateOffice']['title'], $entity->name) )
			->setShortTitle($entity->name)
			->setId(sprintf("%s.%s", __CLASS__, $entity->getId()));

		// check access to remote details option
		if (!\App\UserAccess\CurrentUserAccessFast::getInstance()->hasAccessToOption('Modules/Partners/Corporates-remoteDetails')) {
			$output->addField(\Engine\RemoteData\InfoBox\Field_String::create(null, $_LANG['noAccessToOption']));
			return $output;
		}
		// BASIC INFO -----------------------------------------------------------------------------
		$cont1 = \Engine\RemoteData\InfoBox\Container_Simple::create($this->lang['general']['titles']['general']);
		$output->addContainer($cont1);

		$cont1->addField( \Engine\RemoteData\InfoBox\Field_String::create($this->lang['general']['fields']['id'], $entity->getId()) );
		$cont1->addField( \Engine\RemoteData\InfoBox\Field_String::create($this->lang['corporateOffice']['fields']['name'], $entity->name) );
		$cont1->addField( \App\RemoteData\InfoBox\Field_EntityInfoBox::create($this->lang['corporateOffice']['fields']['idCorporate'], \Entity\Corporate::select($entity->idCorporate)->alias)->setEntity('corporate', $entity->idCorporate) );


		// CONTACT INFO
		$cont2 = \Engine\RemoteData\InfoBox\Container_Simple::create($this->lang['general']['titles']['contact']);
		$output->addContainer($cont2);

		$cont2->addField( \Engine\RemoteData\InfoBox\Field_String::create($this->lang['corporateOffice']['fields']['address'], $entity->address) );
		// composedAddress
		if ($entity->composedAddress) {
			$cont2->addField( \Engine\RemoteData\InfoBox\Field_String::create($this->lang['corporateOffice']['fields']['mainAddressDetails'], $entity->composedAddress));
		}
		$cont2->addField( \Engine\RemoteData\InfoBox\Field_String::create($this->lang['corporateOffice']['fields']['deliveryAddress'], $entity->deliveryAddress) );
		// composedDeliveryAddress
		if ($entity->composedDeliveryAddress) {
			$cont2->addField( \Engine\RemoteData\InfoBox\Field_String::create($this->lang['corporateOffice']['fields']['addressDeliveryDetails'], $entity->composedDeliveryAddress));
		}
		$cont2->addField( \Engine\RemoteData\InfoBox\Field_String::create($this->lang['corporateOffice']['fields']['zipCode'], $entity->zipCode) );
		if ($entity->cityId) {
			$location = sprintf("%s (%s)", \Entity\City::select($entity->cityId)->name, \Entity\Country::select($entity->countryCode)->name);
			$cont2->addField( \App\RemoteData\InfoBox\Field_EntityInfoBox::create($this->lang['corporateOffice']['fields']['location'], $location)->setEntity('city', $entity->cityId) );
		}
		$cont2->addField( \Engine\RemoteData\InfoBox\Field_Telephone::create($this->lang['corporateOffice']['fields']['telephone'], $entity->telephone) );
		$cont2->addField( \Engine\RemoteData\InfoBox\Field_Email::create($this->lang['corporateOffice']['fields']['email'], $entity->email) );

		// FISCAL INFO
		$cont3 = \Engine\RemoteData\InfoBox\Container_Simple::create($this->lang['corporateOffice']['titles']['fiscalInfo']);
		$output->addContainer($cont3);

		if ($entity->paymentPossibilities) {
			$cont3->addField( \Engine\RemoteData\InfoBox\Field_String::create($this->lang['corporateOffice']['fields']['paymentPossibilities'], implode(", ", $entity->paymentPossibilitiesNames)) );
		}

		if ($entity->bankAccounts) {
			$c3 = \Engine\RemoteData\InfoBox\Container_Simple::create($this->lang['corporateOffice']['fields']['bankAccounts']);
			$cont3->addContainer($c3);

			$c3->setCollapsible(false);
			$c3->setType('section');

			foreach ($entity->bankAccounts as $acc) {
				$bank = 	($acc['idBank']) 		? sprintf("%s", \Entity\Bank::select($acc['idBank'])->name) : "";
				$bankOffice = ($acc['idBankOffice'])? sprintf("(%s)", \Entity\BankOffice::select($acc['idBankOffice'])->name) : "";
				$currency = ($acc['currency'])  	? sprintf("[%s]", $acc['currency']) : "";

				$bankAccounts = sprintf("%s / %s %s %s\n", $acc['account'], $bank, $bankOffice, $currency);
				$c3->addField(  \Engine\RemoteData\InfoBox\Field_String::create(NULL, $bankAccounts )->setStyle(($acc['defaultAccount'])?"bold":""));
			}
		}


		// MISC INFO ------------------------------------------------------------------------------
		$cont4 = \Engine\RemoteData\InfoBox\Container_Simple::create($this->lang['general']['titles']['misc'])->setHidden(true);
		$output->addContainer($cont4);
		$cont4->addField( \Engine\RemoteData\InfoBox\Field_String::create($this->lang['corporateOffice']['fields']['comments'], $entity->comments) );
		$cont4->addField( \Engine\RemoteData\InfoBox\Field_Remarks::create($this->lang['general']['fields']['remarks'], $entity->remarks) );
		$cont4->addField( \Engine\RemoteData\InfoBox\Field_Remarks::create($this->lang['corporateOffice']['fields']['costCenters'], implode(', ', $entity->costCentersList)) );

		// PROCESSED REMARKS
		$cRmk = \Engine\RemoteData\InfoBox\Container_ProcessedRemarks::create($this->lang['general']['fields']['processedRemarks'])->setHidden(true);
		$cRmk->addRemarkFields('corporateOffice', $entity->processedRemarks);
		$output->addContainer($cRmk);


		// UPDATE INFO
		$cont6 = \Engine\RemoteData\InfoBox\Container_Simple::create($this->lang['general']['titles']['updates'])->setHidden(true);
		$output->addContainer($cont6);

		$cont6->addField( \Engine\RemoteData\InfoBox\Field_DateTime::create($this->lang['general']['fields']['addTime'], \Engine\DateTime::createFromTimestamp($entity->addTime)) );
		$cont6->addField( \App\RemoteData\InfoBox\Field_EntityInfoBox::create($this->lang['general']['fields']['addUser'], $entity->addUser->name)->setEntity('user', $entity->idAddUser) );
		$cont6->addField( \Engine\RemoteData\InfoBox\Field_DateTime::create($this->lang['general']['fields']['updateTime'], \Engine\DateTime::createFromTimestamp($entity->lastUpdateTime)) );
		$cont6->addField( \App\RemoteData\InfoBox\Field_EntityInfoBox::create($this->lang['general']['fields']['updateUser'], $entity->updateUser->name)->setEntity('user', $entity->idUpdateUser) );
		if ($entity->updatesCount) {
			$cont6->addField( \Engine\RemoteData\InfoBox\Field_String::create($this->lang['general']['fields']['updatesCount'], $entity->updatesCount) );
		}

		return $output;

	}

}
