<?php
namespace Modules\RemoteData\InfoBox;

class View_DocumentSeries extends \App\Mvc\View_RemoteDataInfoBoxEntityAbstract
{
	protected function getEntityRemoteDetails(\App\Entity\SelectAbstract $entity)
	{

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

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

		$cont3 = \Engine\RemoteData\InfoBox\Container_Simple::create($this->lang['series']['titles']['restrictions'])->setHidden(true);
		$output->addContainer($cont3);

		$cont4 = \Engine\RemoteData\InfoBox\Container_Simple::create($this->lang['series']['titles']['alert'])->setHidden(true);
		$output->addContainer($cont4);

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

		// container general
		$cont1->addField( \Engine\RemoteData\InfoBox\Field_String::create($this->lang['series']['fields']['id'], $entity->getId()));
		$cont1->addField( \Engine\RemoteData\InfoBox\Field_String::create($this->lang['series']['fields']['code'], $entity->code));

		$documentTypesList = [];
		foreach ($entity->documentTypes as $documentType) {
			$documentTypesList[] = \App\Config\IssueDocuments::getInstance()->getDocTypeName($documentType).' ('.\App\Config\IssueDocuments::getInstance()->getDocPivot($documentType).')';
		}

		$cont1->addField( \Engine\RemoteData\InfoBox\Field_String::create($this->lang['series']['fields']['status'], $this->lang['series']['fields'][$entity->status]));
		$cont1->addField( \Engine\RemoteData\InfoBox\Field_InlineList::create($this->lang['series']['fields']['documentType'], array_map("trim", $documentTypesList)) );
		$cont1->addField( \App\RemoteData\InfoBox\Field_EntityInfoBox::create($this->lang['series']['fields']['idOffice'],\Entity\Office::select($entity->idOffice)->name)->setEntity('office', $entity->idOffice) );
		$cont1->addField( \Engine\RemoteData\InfoBox\Field_String::create($this->lang['series']['fields']['interval'], "{$entity->start} - {$entity->end}"));
		$cont1->addField( \Engine\RemoteData\InfoBox\Field_String::create($this->lang['series']['fields']['nextNumber'], $entity->nextNumber));
		if ($entity->length) {
			$cont1->addField( \Engine\RemoteData\InfoBox\Field_String::create($this->lang['series']['fields']['length'], $entity->length));
		}

		if ($entity->lastDate != '0000-00-00'){
			$cont1->addField( \Engine\RemoteData\InfoBox\Field_Date::create($this->lang['series']['fields']['lastDate'], \Engine\DateTime::createFromIsoDate($entity->lastDate)));
		}

		// valability
		if($entity->validStartDate != '0000-00-00' && $entity->validEndDate != '9999-12-31') {
			$startDate = \Engine\DateTime::createFromIsoDate($entity->validStartDate)->formatDateStd();
			$endDate = \Engine\DateTime::createFromIsoDate($entity->validEndDate)->formatDateStd();
			$cont1->addField( \Engine\RemoteData\InfoBox\Field_String::create($this->lang['series']['fields']['validDate'], "{$startDate} - {$endDate}"));
		}

		elseif ($entity->validStartDate == '0000-00-00' && $entity->validEndDate != '9999-12-31') {
			$endDate = \Engine\DateTime::createFromIsoDate($entity->validEndDate)->formatDateStd();
			$cont1->addField( \Engine\RemoteData\InfoBox\Field_String::create($this->lang['series']['fields']['validDate'], "... - {$endDate}"));
		}
		elseif ($entity->validStartDate != '0000-00-00' && $entity->validEndDate == '9999-12-31') {
			$startDate = \Engine\DateTime::createFromIsoDate($entity->validStartDate)->formatDateStd();
			$cont1->addField( \Engine\RemoteData\InfoBox\Field_String::create($this->lang['series']['fields']['validDate'], "{$startDate} - ..."));
		}


		// container history
		$cont2->addField( \Engine\RemoteData\InfoBox\Field_DateTime::create($this->lang['general']['fields']['addTime'], \Engine\DateTime::createFromTimestamp($entity->addTime)));
		$cont2->addField( \App\RemoteData\InfoBox\Field_EntityInfoBox::create($this->lang['general']['fields']['addUser'],\Entity\User::select($entity->idAddUser)->name)->setEntity('user', $entity->idAddUser) );
		$cont2->addField( \Engine\RemoteData\InfoBox\Field_DateTime::create($this->lang['general']['fields']['updateTime'], \Engine\DateTime::createFromTimestamp($entity->lastUpdateTime)));
		$cont2->addField( \App\RemoteData\InfoBox\Field_EntityInfoBox::create($this->lang['general']['fields']['updateUser'],\Entity\User::select($entity->idUpdateUser)->name)->setEntity('user', $entity->idUpdateUser) );
		$cont2->addField( \Engine\RemoteData\InfoBox\Field_String::create($this->lang['general']['fields']['updatesCount'], $entity->updatesCount));


		// DOC SERIES PREFERENCES /////////////////////////////////////////////////////////////////
		// currencies
		if ($entity->allCurrencies) {
			$cont3->addField( \Engine\RemoteData\InfoBox\Field_String::create($this->lang['series']['fields']['currencies'], $this->lang['series']['currencies']['all']));
		}
		elseif ($entity->validCurrenciesList)  {
			$contCurrencies =  \Engine\RemoteData\InfoBox\Container_Simple::create($this->lang['series']['fields']['currencies']);
			$cont3->addContainer($contCurrencies);
			$currencies = [];
			foreach ($entity->validCurrenciesList as $curr) {
				$currencies[] = $curr;
			}
			$contCurrencies->addField( \Engine\RemoteData\InfoBox\Field_InlineList::create(null, $currencies) );
		}
		else {
			$cont3->addField( \Engine\RemoteData\InfoBox\Field_String::create($this->lang['series']['fields']['currencies'], $this->lang['series']['currencies']['none']));
		}

		// users
		if ($entity->allUsers) {
			$cont3->addField( \Engine\RemoteData\InfoBox\Field_String::create($this->lang['series']['fields']['users'], $this->lang['series']['users']['all']));
		}
		elseif ($entity->validUsersList) {
			$contUsers =  \Engine\RemoteData\InfoBox\Container_Simple::create($this->lang['series']['fields']['users']);
			$cont3->addContainer($contUsers);
			foreach ($entity->validUsersList as $user) {
				 $contUsers->addField(\App\RemoteData\InfoBox\Field_EntityInfoBox::create("",\Entity\User::select($user)->name)->setEntity('user', $user));
			}
		}
		else {
			$cont3->addField( \Engine\RemoteData\InfoBox\Field_String::create($this->lang['series']['fields']['users'], $this->lang['series']['users']['none']));
		}

		// templates
		if ($entity->allTemplates) {
			$cont3->addField( \Engine\RemoteData\InfoBox\Field_String::create($this->lang['series']['fields']['templates'],$this->lang['series']['templates']['all']));
		}
		elseif ($entity->validTemplatesList) {
			$contTemp =  \Engine\RemoteData\InfoBox\Container_Simple::create($this->lang['series']['fields']['templates']);
			$cont3->addContainer($contTemp);
			foreach ($entity->validTemplatesList as $temp) {
				$contTemp->addField(\Engine\RemoteData\InfoBox\Field_String::create("",\Entity\DocumentTemplate::select($temp)->name));
			}
		}
		else {
			$cont3->addField( \Engine\RemoteData\InfoBox\Field_String::create($this->lang['series']['fields']['templates'], $this->lang['series']['templates']['none']));
		}

		// document series alert
		if ($entity->alertNumber>0) {
			$cont4->addField(\Engine\RemoteData\InfoBox\Field_String::create($this->lang['series']['fields']['alertNumber'], $entity->alertNumber));
		}
		$cont4->addField(\App\RemoteData\InfoBox\Field_EntityInfoBox::create($this->lang['series']['fields']['alertUser'],\Entity\User::select($entity->idUser)->name)->setEntity('user', $entity->idUser) );

		return $output;
	}
}