<?php

namespace Modules\RemoteData\InfoBox;


class View_Task extends \App\Mvc\View_RemoteDataInfoBoxEntityAbstract
{

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

		// load global config
		$_LANG = \App\Loader_Lang::getInstance()->getGlobal();

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

		if (\App\UserAccess\CurrentUserAccessFast::getInstance()->hasAccessToSection('Modules/TaskManagement/Tasks')) {
			$icon = \Engine\RemoteData\InfoBox\Icon\Icon::create('edit');
			$icon->setTooltip($this->lang['task']['options']['edit']);
			$icon->setJsCallback("Popup2.create().setId('taskUpdate').setUrl(MVC.appUrl('TaskManagement/Tasks', 'update', { 'id': '{$entity->getId()}' })).setDimensions(650, 630).show();");
			$output->addIcon($icon);

			$icon = \Engine\RemoteData\InfoBox\Icon\Icon::create('history');
			$icon->setTooltip($this->lang['task']['options']['view']);
			$icon->setJsCallback("Popup2.create().setId('taskHistory').setUrl(MVC.appUrl('TaskManagement/Tasks', 'taskHistory', { 'id': '{$entity->getId()}' })).setDimensions(950, 400).show();");
			$output->addIcon($icon);

			$icon = \Engine\RemoteData\InfoBox\Icon\Icon::create('reload');
			$icon->setTooltip($this->lang['task']['options']['refreshStatus']);
			$icon->setJsCallback("Popup2.create().setId('taskChangeStatus').setUrl(MVC.appUrl('TaskManagement/Tasks', 'changeStatus', { 'id': '{$entity->getId()}' })).setDimensions(700, 400).show();");
			$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'], $entity->getId()) );
		$cont1->addField( \Engine\RemoteData\InfoBox\Field_Date::create($this->lang['task']['fields']['taskDate'], \Engine\DateTime::createFromIsoDate($entity->taskDate)) );
		$cont1->addField( \Engine\RemoteData\InfoBox\Field_String::create($this->lang['task']['fields']['status'], $_LANG['tasks']['statusName'][$entity->status]) );
		$cont1->addField( \Engine\RemoteData\InfoBox\Field_String::create($this->lang['task']['fields']['idCategory'], \Entity\TaskCategory::select($entity->idCategory)->name) );
		$cont1->addField( \Engine\RemoteData\InfoBox\Field_String::create($this->lang['task']['fields']['description'], $entity->description) );
		$cont1->addField( \Engine\RemoteData\InfoBox\Field_String::create($this->lang['task']['fields']['priority'], $_LANG['tasks']['priorities'][$entity->priority]) );
		$cont1->addField( \Engine\RemoteData\InfoBox\Field_String::create($this->lang['task']['fields']['free'], ($entity->free?$_LANG['yes']:$_LANG['no'])) );
		$cont1->addField( \Engine\RemoteData\InfoBox\Field_DateTime::create($this->lang['task']['fields']['deadline'], \Engine\DateTime::createFromTimestamp($entity->deadline))->setStyle('bold red') );
		$cont1->addField( \Engine\RemoteData\InfoBox\Field_String::create($this->lang['task']['fields']['idOperationType'], \Entity\TaskOperationType::select($entity->idOperationType)->description) );
		$cont1->addField( \Engine\RemoteData\InfoBox\Field_String::create($this->lang['task']['fields']['comments'], $entity->comments) );

		// USERS / DEPT / OFFICE -----------------------------------------------------------------------------------
		$cont2 = \Engine\RemoteData\InfoBox\Container_Simple::create($this->lang['task']['titles']['users']);
		$output->addContainer($cont2);

		$cont2->addField( \App\RemoteData\InfoBox\Field_EntityInfoBox::create($this->lang['task']['fields']['idAssignorUser'], $entity->assignorUser->name)->setEntity('user', $entity->idAssignorUser) );
		$cont2->addField( \App\RemoteData\InfoBox\Field_EntityInfoBox::create($this->lang['task']['fields']['idResponsibleUser'], $entity->responsibleUser->name)->setEntity('user', $entity->idResponsibleUser) );
		$cont2->addField( \Engine\RemoteData\InfoBox\Field_String::create($this->lang['task']['fields']['idDepartment'], \Entity\Department::select($entity->idDepartment)->name) );
		$cont2->addField( \App\RemoteData\InfoBox\Field_EntityInfoBox::create($this->lang['task']['fields']['idOffice'], $entity->office->name)->setEntity('office', $entity->idOffice) );


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

		if ($entity->pivot) {
			if ($entity->pivot=='order' && $entity->idPivot) {
				$cont3->addField( \App\RemoteData\InfoBox\Field_EntityInfoBox::create($_LANG['tasks']['pivotsArr'][$entity->pivot], sprintf("#%s", $entity->idPivot))->setEntity('order', $entity->idPivot) );
			}
			elseif ($entity->pivot=='service' && $entity->idPivot) {
				$cont3->addField( \App\RemoteData\InfoBox\Field_EntityInfoBox::create($_LANG['tasks']['pivotsArr'][$entity->pivot], sprintf("#%s", $entity->idPivot))->setEntity('service', $entity->idPivot) );
			}
			elseif ($entity->pivot=='invoice' && $entity->idPivot) {
				$cont3->addField( \App\RemoteData\InfoBox\Field_EntityInfoBox::create($_LANG['tasks']['pivotsArr'][$entity->pivot], sprintf("#%s", $entity->idPivot))->setEntity('invoice', $entity->idPivot) );
			}
			else {
				$cont3->addField( \Engine\RemoteData\InfoBox\Field_String::create($_LANG['tasks']['pivotsArr'][$entity->pivot], sprintf("#%s", $entity->idPivot)) );
			}
		}


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

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

		return $output;
	}

}
