<?php
namespace Modules\Orders\ImportedOrders\ServicesList;

class View_OrderDetails extends \App\Mvc\View_Window_GeneralAbstract
{
	protected $renderingTemplate = 'orderDetails.tpl';

	/**
	 * (non-PHPdoc)
	 */
	public function render()
	{
		// UPDATE IMPORTED ORDER - hide credit card number (if the user does not have access) /////
		$orderHasCcNumbers = false;
		$userHasAccessToCcNumbers = \App\UserAccess\CurrentUserAccessFast::getInstance()->hasAccessToOption('Modules/Orders/ImportedOrders/ServicesList-viewCcNumber');
		foreach ($this->input['importedOrderObj']->services as $impService) {
			/** @var \App\ImportedOrder\Service $impService */

			// hide CCs on the chosen offer
			$impOffer = $impService->chosenOffer;
			if ($impOffer) {
				$paymentCc = $impOffer->creditCard;
				if ($paymentCc and ($paymentCc->number or $paymentCc->encryptedNumberRef)) {
					$orderHasCcNumbers = true;
					// display masked or original number based on user access and cases (is PCI-DSS compliant or not)
					if ($userHasAccessToCcNumbers and $this->output['revealCcNumbers']) {
						$paymentCc->number = $paymentCc->getOriginalNumber();
						$importedOrderEnt = \Entity\ImportedOrder::select($this->input['idOrder']);
						\Log::getInstance()->info(sprintf("[PCI-DSS::importedOrderViewPAN] User `%s` (with proper user access) viewed the credit card number for the imported service `%s` from system `%s`.", \Entity\User::selectCurrent()->name, $impService->idSystem, $importedOrderEnt->system->name));
					} else {
						$paymentCc->number = $paymentCc->getMaskedNumber();
					}
				}
				$cashingCc = $impOffer->cashingCreditCard;
				if ($cashingCc and ($cashingCc->number or $cashingCc->encryptedNumberRef)) {
					$orderHasCcNumbers = true;
					// display masked or original number based on user access and cases (is PCI-DSS compliant or not)
					if ($userHasAccessToCcNumbers and $this->output['revealCcNumbers']) {
						$cashingCc->number = $cashingCc->getOriginalNumber();
						$importedOrderEnt = \Entity\ImportedOrder::select($this->input['idOrder']);
						\Log::getInstance()->info(sprintf("[PCI-DSS::importedOrderViewPAN] User `%s` (with proper user access) viewed the credit card number for the imported service `%s` from system `%s`.", \Entity\User::selectCurrent()->name, $impService->idSystem, $importedOrderEnt->system->name));
					} else {
						$cashingCc->number = $cashingCc->getMaskedNumber();
					}
				}
			}

		}

		// CREATE THE IMPORTED ORDER OBJECT EXPLORER //////////////////////////////////////////////
		$objectExplorer = new \App\Utils\Views\ObjectExplorer($this->input['importedOrderObj']);

		$objectExplorer->addGetStyleCallback(function($key, $parents) {

			// passengers
			if (in_array('passengers', $parents) && count($parents) == 1 ) {
				return [
					'navLabel' => 'passenger',
					'navTarget' => ['passengers', $key]
				];
			}

			// corporates
			if (in_array('corporates', $parents) && count($parents) == 1 ) {
				return [
					'navLabel' => 'corporate',
					'navTarget' => ['corporates', $key]
				];
			}

			// services
			if (in_array('services', $parents) && count($parents) == 1 ) {
				return [
					'navLabel' => 	'service',
					'navTarget' => 	['services', $key],
					'navStyle' =>  	'style-0',
					'style' => 		sprintf('style-%s', $key % 3),
				];
			}

			// departure place
			$elements = ["departurePlace", "destinationPlace", "remarks"];
			if (in_array($key, $elements) && in_array('services', $parents) && count($parents) == 2) {
				array_push($parents, $key);
				return [
					'navLabel' => '&nbsp;'.$key,
					'navTarget' => $parents
				];
			}

			// chosenOffer
			if ((string)$key == "chosenOffer" && in_array('services', $parents) && count($parents) == 2) {
				array_push($parents, $key);
				return [
					'navLabel' => '&nbsp;chosenOffer',
					'navTarget' => $parents
				];
			}

			// refundDetails
			if ((string)$key == "refundDetails" && !count(array_diff(['services'], $parents)) && count($parents) === 2) {
				array_push($parents, $key);
				return [
					'navLabel' => '&nbsp;refundDetails',
					'navTarget' => $parents,
					'navStyle' =>  'style-1',
				];
			}

			// cancelDetails
			if ((string)$key == "cancelDetails" && !count(array_diff(['services'], $parents)) && count($parents) === 2) {
				array_push($parents, $key);
				return [
					'navLabel' => '&nbsp;cancelDetails',
					'navTarget' => $parents,
					'navStyle' =>  'style-1',
				];
			}
		});

		$objectExplorer->setOption('interactive', true);
		$objectExplorer->setOption('showEmptyLeafs', false);
		$objectExplorer->setOption('collapsed', false);
		$objectExplorer->setOption('showPrivateMembers', true);

		$this->setOutputVar('idOrder', $this->input['idOrder']);
		$this->setOutputVar('objectExplorer', $objectExplorer);
		$this->setOutputVar('orderHasCcNumbers', $orderHasCcNumbers);
		$this->setOutputVar('userHasAccessToCcNumbers', $userHasAccessToCcNumbers);

		$optionsView = new \App\Utils\Views\MenuDropdownOptimizedJs($this->getAllDefinedOptions());
		$this->setOutputVar('optionsView', $optionsView);


		return parent::render();
	}

	private function getAllDefinedOptions()
	{
		$options = new \App\Mvc\View\Utils\RowOptions\Container\Simple();

		if (SHOW_DEBUGS) {
			$reportsContainer = \App\Mvc\View\Utils\RowOptions\Container\OptionsMenu::create("settings");

			$reportsContainer
				->append(
					\App\Mvc\View\Utils\RowOptions\Option\Option::create("hideItinerary")
						->setLabel("Hide itinerary")
						->setIcon('view')
						->setJsCallback("hideItinerary(true)")
				)
				->append(
					\App\Mvc\View\Utils\RowOptions\Option\Option::create("hidePassengers")
						->setLabel("Hide passengers")
						->setIcon('view')
						->setJsCallback("hidePassengers(true)")
				)
				->append(
					\App\Mvc\View\Utils\RowOptions\Option\Option::create("hideOffers")
						->setLabel("Hide offers")
						->setIcon('view')
						->setJsCallback("hideOffers(true)")
				)
				->append(
					\App\Mvc\View\Utils\RowOptions\Option\Option::create("hideLinkedServices")
						->setLabel("Hide linked services")
						->setIcon('view')
						->setJsCallback("hideLinkedServices(true)")
				)

				->append(
					\App\Mvc\View\Utils\RowOptions\Option\Separator::create("separator-01")
				)

				->append(
					\App\Mvc\View\Utils\RowOptions\Option\Option::create("highlightPrices")
						->setLabel("Highlight prices")
						->setIcon('view')
						->setJsCallback("highlightPrices(true)")
				)
			;

			$options->append($reportsContainer);
		}


		return $options;
	}

}
