<?php

namespace Modules\RemoteData\InfoBox;


class View_OfferProfileRule extends \App\Mvc\View_RemoteDataInfoBoxEntityAbstract
{

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

		// the output object
		$output = \Engine\RemoteData\InfoBox\Container_Main::create( sprintf($this->lang['offerProfileRule']['title'], $entity->getId()) );


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

		$contGeneral->addField( \Engine\RemoteData\InfoBox\Field_String::create($this->lang['offerProfileRule']['fields']['id'], $entity->getId()) );
		$contGeneral->addField( \Engine\RemoteData\InfoBox\Field_String::create($this->lang['general']['fields']['name'], $entity->name) );
		$contGeneral->addField( \Engine\RemoteData\InfoBox\Field_String::create($this->lang['offerProfileRule']['fields']['active'], $entity->active ? $this->globalLang['yes'] : $this->globalLang['no']) );
		$contGeneral->addField( \Engine\RemoteData\InfoBox\Field_String::create($this->lang['offerProfileRule']['fields']['priority'], $entity->priority) );

		if ($entity->startDate && $entity->startDate !== DATE_MIN) {
			$contGeneral->addField( \Engine\RemoteData\InfoBox\Field_Date::create($this->lang['offerProfileRule']['fields']['startDate'], \Engine\DateTime::createFromIsoDate($entity->startDate)) );
		}
		if ($entity->endDate && $entity->endDate !== DATE_MAX) {
			$contGeneral->addField( \Engine\RemoteData\InfoBox\Field_Date::create($this->lang['offerProfileRule']['fields']['endDate'], \Engine\DateTime::createFromIsoDate($entity->endDate)) );
		}

		// CORPORATES -----------------------------------------------------------------------------
		$corporates = $entity->idCorporatesList;
		if (!empty($corporates)) {
			$contCorps = \Engine\RemoteData\InfoBox\Container_Simple::create($this->lang['offerProfileRule']['cont']['corporates'])->setHidden(true);
			$output->addContainer($contCorps);

			foreach ($corporates as $idCorporate) {
				$corpName = \Entity\Corporate::select($idCorporate)->alias;
				if ($corpName) {
					$contCorps->addField( \App\RemoteData\InfoBox\Field_EntityInfoBox::create(null, $corpName)->setEntity('corporate', $idCorporate) );
				}
			}
		}

		// CRITERIA ------------------------------------------------------------------------------
		$criteria = $entity->criteria;
		if ($criteria) {
			$contCriteria = \Engine\RemoteData\InfoBox\Container_Simple::create($this->lang['offerProfileRule']['cont']['criteria'])->setCollapsible(true);
			$output->addContainer($contCriteria);

			foreach ($criteria as $criterion) {
				$contCriteria->addField( \Engine\RemoteData\InfoBox\Field_String::create(null, $criterion->createDescription()) );
			}
		}

		$contComponents = \Engine\RemoteData\InfoBox\Container_Simple::create($this->lang['offerProfileRule']['cont']['components']);
		$output->addContainer($contComponents);

		// COMMISSION -----------------------------------------------------------------------------
		$commissionDetails = $entity->commissionDetails;
		if ($commissionDetails) {
			$contComponents->addField( \Engine\RemoteData\InfoBox\Field_String::create($this->lang['offerProfileRule']['fields']['commission'], $commissionDetails->getDescription(true)) );
		}

		// DISCOUNT -------------------------------------------------------------------------------
		$discountDetails = $entity->discountDetails;
		if ($discountDetails) {
			$contComponents->addField( \Engine\RemoteData\InfoBox\Field_String::create($this->lang['offerProfileRule']['fields']['discount'], $discountDetails->getDescription(true)) );
		}

		// SERVICE FEE ----------------------------------------------------------------------------
		$serviceFeeDetails = $entity->serviceFeeDetails;
		if ($serviceFeeDetails) {
			$contComponents->addField( \Engine\RemoteData\InfoBox\Field_String::create($this->lang['offerProfileRule']['fields']['serviceFee'], $serviceFeeDetails->getDescription(true)) );
		}

		// REMARKS --------------------------------------------------------------------------------
		if ($entity->remarks) {
			// processed remarks
			$contRms = \Engine\RemoteData\InfoBox\Container_ProcessedRemarks::create($this->lang['general']['fields']['processedRemarks'])->setHidden(true);
			$contRms->addRemarkFields('offerProfileRule', $entity->processedRemarks);
			$output->addContainer($contRms);

			$output->addField( \Engine\RemoteData\InfoBox\Field_MultiLine::create($this->lang['general']['fields']['remarks'], $entity->remarks) );
		}

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

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

		return $output;

	}

}
