<?php

namespace Modules\RemoteData\InfoBox;


class View_Order 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['order']['title'], ($entity->reservationNumber?$entity->reservationNumber:'')) )
			->setShortTitle( ($entity->reservationNumber?$entity->reservationNumber:'?') )
			->setId(sprintf("%s.%s", __CLASS__, $entity->getId()));

		// dashboard
		if (\App\UserAccess\CurrentUserAccessFast::getInstance()->hasAccessToSection('Modules/Orders/Dashboard')) {
			$icon = \Engine\RemoteData\InfoBox\Icon\Icon::create('quickNav');
			$icon->setTooltip($this->lang['order']['option']['dashboard']);
			$icon->setJsCallback("window.open(MVC.appUrl('Orders/Dashboard', 'index', { 'idEntity': '{$entity->getId()}' }), '_blank');");
			$icon->setIcon("quickNav");
			$output->addIcon($icon);
		}

		// services list on this order
		if (\App\UserAccess\CurrentUserAccessFast::getInstance()->hasAccessToSection('Modules/Orders/ServicesList')) {
			$icon = \Engine\RemoteData\InfoBox\Icon\Icon::create('services');
			$icon->setJsCallback(
				"window.open(
					MVC.appUrl(
						'Orders/ServicesList', null, {
							'filters[orderId]': {$entity->getId()}, 'filters[idOrdersTypes]': 'id',
							'listing[reset]': '1' , 'listing[paramsSlot]':'1'
						}
					), '_blank'
				);"
			);
			$icon->setTooltip($this->lang['order']['options']['filterServices']);
			$output->addIcon($icon);
		}

		// filter by this order
		if (\App\UserAccess\CurrentUserAccessFast::getInstance()->hasAccessToSection('Modules/Orders/OrdersList')) {
			$icon = \Engine\RemoteData\InfoBox\Icon\Icon::create('filter');
			$icon->setJsCallback("window.open(MVC.appUrl('Orders/OrdersList', null, {'filters[idReservation]': {$entity->getId()}, 'filters[idReservationType]': 'id', 'listing[reset]': '1' , 'listing[paramsSlot]':'1'} ), '_blank');");
			$icon->setTooltip($this->lang['order']['options']['filter']);
			$output->addIcon($icon);
		}

		if (\App\UserAccess\CurrentUserAccessFast::getInstance()->hasAccessToOption('Modules/Orders/Details/OrderNotes')) {
			if ($entity->notes) {
				$icon = \Engine\RemoteData\InfoBox\Icon\Icon::create('notesCompleted');
				$icon->setJsCallback("orderNotes({$entity->getId()})");
				$icon->setTooltip($this->lang['order']['options']['orderNotes']);
				$output->addIcon($icon);
			}
			else {
				$icon = \Engine\RemoteData\InfoBox\Icon\Icon::create('notesEmpty');
				$icon->setJsCallback("orderNotes({$entity->getId()})");
				$icon->setTooltip($this->lang['order']['options']['orderNotes']);
				$output->addIcon($icon);

			}
		}


		// 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'], sprintf("#%s (%s)", $entity->getId(), $entity->convertedId)) );
		$cont1->addField( \Engine\RemoteData\InfoBox\Field_String::create($this->lang['order']['fields']['reservationCode'], $entity->reservationCode) );
		$cont1->addField( \Engine\RemoteData\InfoBox\Field_String::create($this->lang['order']['fields']['reservationNumber'], $entity->reservationNumber) );
		$cont1->addField( \Engine\RemoteData\InfoBox\Field_Date::create($this->lang['order']['fields']['date'], \Engine\DateTime::create($entity->date)) );
		$cont1->addField( \App\RemoteData\InfoBox\Field_EntityInfoBox::create($this->lang['order']['fields']['autoProcessRule'], $entity->autoProcessRule->name?:'#'.$entity->idAutoProcessRule)->setEntity('autoProcessRule', $entity->idAutoProcessRule) );

		// extra info
		$cont1 = \Engine\RemoteData\InfoBox\Container_Simple::create(null);
		$output->addContainer($cont1);

		$cont1->addField( \Engine\RemoteData\InfoBox\Field_String::create($this->lang['order']['fields']['resType'], $entity->resTypeName) );
		$cont1->addField( \Engine\RemoteData\InfoBox\Field_String::create($this->lang['order']['fields']['servicesType'], $entity->servicesTypeName) );
		$cont1->addField( \Engine\RemoteData\InfoBox\Field_String::create($this->lang['order']['fields']['flag'], $entity->flagName) );
		$cont1->addField( \Engine\RemoteData\InfoBox\Field_Date::create($this->lang['order']['fields']['startDate'], \Engine\DateTime::create($entity->startDate)) );
		$cont1->addField( \Engine\RemoteData\InfoBox\Field_Date::create($this->lang['order']['fields']['endDate'], \Engine\DateTime::create($entity->endDate)) );


		// CUSTOMERS -----------------------------------------------------------------------------
		$cont2 = \Engine\RemoteData\InfoBox\Container_Simple::create($this->lang['order']['titles']['customers']);
		$output->addContainer($cont2);

		$cont2->addField( \App\RemoteData\InfoBox\Field_EntityInfoBox::create($this->lang['order']['fields']['idCustomerOwner'], $entity->customerOwner->name)->setEntity('customer', $entity->idCustomerOwner) );

		$cont21 = \Engine\RemoteData\InfoBox\Container_Simple::create($this->lang['order']['titles']['customersList'])->setHidden(true);
		$cont2->addContainer($cont21);
		foreach ($entity->idCustomersList as $idCustomer) {
			$cont21->addField( \App\RemoteData\InfoBox\Field_EntityInfoBox::create(null, \Entity\Customer::select($idCustomer)->name)->setEntity('customer', $idCustomer) );
		}
		$cont2->addField( \Engine\RemoteData\InfoBox\Field_String::create($this->lang['order']['fields']['pax'], $entity->pax) );
		$cont2->addField( \Engine\RemoteData\InfoBox\Field_String::create($this->lang['order']['fields']['paxDetails'], $entity->paxDetailsReadable) );


		// CORPORATIONS -------------------------------------------------------------------------
		$cont3 = \Engine\RemoteData\InfoBox\Container_Simple::create($this->lang['order']['titles']['corporations']);
		$output->addContainer($cont3);

		$cont3->addField( \App\RemoteData\InfoBox\Field_EntityInfoBox::create($this->lang['order']['fields']['idCorporateOwner'], $entity->corporateOwner->alias)->setEntity('corporate', $entity->idCorporateOwner) );
		$cont3->addField( \Engine\RemoteData\InfoBox\Field_String::create($this->lang['order']['fields']['corporateOwnerOfficeCostCenter'], $entity->corporateOwnerOfficeCostCenter) );


		$cont31 = \Engine\RemoteData\InfoBox\Container_Simple::create($this->lang['order']['titles']['corporationsList'])->setHidden(true);
		$cont3->addContainer($cont31);
		foreach ($entity->idCorporatesList as $idCorporate) {
			$cont31->addField( \App\RemoteData\InfoBox\Field_EntityInfoBox::create(null, \Entity\Corporate::select($idCorporate)->alias)->setEntity('corporate', $idCorporate) );
		}
		if ($entity->idContactPerson) {
			$cont3->addField( \App\RemoteData\InfoBox\Field_EntityInfoBox::create($this->lang['order']['fields']['idContactPerson'], $entity->contactPerson->name)->setEntity('contactPerson', $entity->idContactPerson) );
		}
		$cont3->addField( \Engine\RemoteData\InfoBox\Field_String::create($this->lang['order']['fields']['miceProject'], $entity->miceProject->name) );


		// STATUSES -----------------------------------------------------------------------------
		$cont7 = \Engine\RemoteData\InfoBox\Container_Simple::create($this->lang['order']['titles']['statuses']);
		$output->addContainer($cont7);

		$cont7->addField( \Engine\RemoteData\InfoBox\Field_String::create($this->lang['order']['fields']['statusOp'], $entity->statusOpName) );
		$cont7->addField( \Engine\RemoteData\InfoBox\Field_String::create($this->lang['order']['fields']['statusFin'], $entity->statusFinName) );


		// COMMENTS
		$cont8 = \Engine\RemoteData\InfoBox\Container_Simple::create($this->lang['order']['titles']['comments']);
		$output->addContainer($cont8);

		$cont8->addField( \Engine\RemoteData\InfoBox\Field_String::create($this->lang['order']['fields']['description'], $entity->description) );
		$cont8->addField( \Engine\RemoteData\InfoBox\Field_String::create($this->lang['order']['fields']['comments'], $entity->comments) );

		// NOTES
		$cont9 = \Engine\RemoteData\InfoBox\Container_Simple::create($this->lang['order']['titles']['notes']);
		$cont9->setHidden(true);
		$output->addContainer($cont9);
		$noteCount = 1;
		foreach (explode("\r\n", $entity->notes) as $note) {
			$cont9->addField( \Engine\RemoteData\InfoBox\Field_String::create(sprintf($this->lang['order']['fields']['notes'], $noteCount), $note) );
			$noteCount++;
		}


		// RESPONSIBLE USER AND OFFICE -----------------------------------------------------------
		$cont10 = \Engine\RemoteData\InfoBox\Container_Simple::create($this->lang['general']['titles']['responsible']);
		$output->addContainer($cont10);

		$cont10->addField( \App\RemoteData\InfoBox\Field_EntityInfoBox::create($this->lang['general']['fields']['responsibleUser'], $entity->user->name)->setEntity('user', $entity->idUser) );
		$cont10->addField( \App\RemoteData\InfoBox\Field_EntityInfoBox::create($this->lang['general']['fields']['responsibleOffice'], $entity->office->name)->setEntity('office', $entity->idOffice) );
		$cont10->addField( \Engine\RemoteData\InfoBox\Field_String::create($this->lang['order']['fields']['responsibleDepartment'], $entity->department->name) );


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

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

		return $output;
	}

}
