<?php

namespace Modules\Orders\Services\ProcessBulk;

class View_Index extends \App\Mvc\View_Popup_GeneralAbstract
{

	/**
	 * The rendering Smarty template file.
	 * @var String
	 */
	protected $renderingTemplate = null;

	/**
	 * these 3 are used for determine if discount, commission or service price components are shown in `copy` container
	 * boolean unknown
	 */
	protected $showDiscount = null;
	protected $showCommission = null;
	protected $showServiceFee = null;


	public function render()
	{

		$this->setOutputVar('importedServicesList', $this->input['formData']['importedServicesList']);
		$this->setOutputVar('idImportedServicesList',$this->input['formData']['idImportedServicesList']);

		// go throught each service
		if ($this->input['formData']['importedServicesList']) {

			$form = [];

			foreach($this->input['formData']['importedServicesList'] as $idImportedService=>$serviceDetails) {
				// passenger(s) container
				$form[$idImportedService]['passContainer'] 	= 		$this->getPassengersForm($idImportedService, $serviceDetails['object']);

				// extra info container
				$form[$idImportedService]['extraInfo'] = 			$this->getHiddenInput($idImportedService);

				if ($serviceDetails['hasOffer']) {
					// price container
					$form[$idImportedService]['priceContainer'] = 	$this->getPriceForm($idImportedService);

					// supplier container
					$form[$idImportedService]['supplierContainer']= $this->getSupplierForm($idImportedService,  $serviceDetails['object']);
				}

			}
			$this->setOutputVar('form', $form);
			$this->setOutputVar('showCommission', $this->showCommission);
			$this->setOutputVar('showDiscount', $this->showDiscount);
			$this->setOutputVar('showServiceFee', $this->showServiceFee);
		}

		return parent::render();

	}

	/**
	 * 'draw' the hidden inputs of this form
	 * @param int $idImportedService
	 */
	private function getHiddenInput($idImportedService)
	{
		$container = new \Engine\Form\Container\Table();

		$impServFld = new \Engine\Form\Input\Hidden(null, "", $idImportedService);
		$impServFld->setId('idImportedServiceStructure_'.$idImportedService);
		$container->addElement($impServFld);

		if ($this->input['formData']['importedServicesList'][$idImportedService]['hasOffer']) {
			$container->addElement(new \Engine\Form\Input\Hidden('', "hasOffer[$idImportedService]" , 1));
		}
		else {
			$container->addElement(new \Engine\Form\Input\Hidden('', "hasOffer[$idImportedService]" , 0));
		}

		$container->addElement(new \Engine\Form\Input\Hidden(null, 'idImportedServicesList[]', $idImportedService));

		$container->addElement(new \Engine\Form\Input\Hidden('', 'save' , 1));

		return $container;
	}

	/**
	 * `draw` the prices form
	 * @param int $idImportedService
	 */
	private function getPriceForm($idImportedService)
	{
		// get the prices components config
		$priceCfg = \App\Config\Services_OfferRowTypes::getInstance()->get($this->input['formData']['importedServicesList'][$idImportedService]['details']['serviceCode']);

		$container = new \Engine\Form\Container\Table();

		// service price
		$servicePrice = new \App\Form\Input\Price($this->lang['index']['price']['servicePrice'], "prices[servicePrice][$idImportedService]");
		$servicePrice->setValue($this->input['formData']['importedServicesList'][$idImportedService]['prices']['servicePrice']);
		$servicePrice->setDisabled(true);
		$container->addElement($servicePrice);

		// commission
		$commission = new \App\Form\Input\Price_ActiveOfferProfileCommission($this->lang['index']['price']['commission'], "prices[commission][$idImportedService]");
		$commission->setValue($this->input['formData']['importedServicesList'][$idImportedService]['prices']['commission']);
		if (!$priceCfg['commission']) {
			$commission->setInvisible(true);
		}
		else {
			$this->showCommission = true;
			$commission->addEvent("onChange", "refreshOfferTotal($idImportedService)");
		}
		$container->addElement($commission);

		// discount
		$discount = new \App\Form\Input\Price_ActiveOfferProfile($this->lang['index']['price']['discount'], "prices[discount][{$idImportedService}]");
		$discount->setValue($this->input['formData']['importedServicesList'][$idImportedService]['prices']['discount']);
		if (!$priceCfg['discount']) {
			$discount->setInvisible(true);
		}
		else {
			$this->showDiscount = true;
			$discount->addEvent("onChange", "refreshOfferTotal($idImportedService)");
		}
		$container->addElement($discount);

		// service fee
		$serviceFee = new \App\Form\Input\Price_ActiveOfferProfile($this->lang['index']['price']['serviceFee'], "prices[serviceFee][{$idImportedService}]");
		$serviceFee->setValue($this->input['formData']['importedServicesList'][$idImportedService]['prices']['serviceFee']);
		if (!$priceCfg['serviceFee']) {
			$serviceFee->setInvisible(true);
		}
		else {
			$this->showServiceFee = true;
			$serviceFee->addEvent("onChange", "refreshOfferTotal($idImportedService)");
		}
		$container->addElement($serviceFee);

		// tax
		$tax = new \App\Form\Input\Price($this->lang['index']['price']['tax'], "prices[tax][{$idImportedService}]");
		$tax->setValue($this->input['formData']['importedServicesList'][$idImportedService]['prices']['tax']);
		if (!$priceCfg['tax']) {
		}
		$tax->setDisabled(true);
		$container->addElement($tax);

		// total
		$total = new \App\Form\Input\Price($this->lang['index']['price']['total'], "prices[total][{$idImportedService}]");
		$total->setDisabled(true);
		$container->addElement($total);

		return $container;
	}

	/**
	 * `draw` passenger(s) container
	 * @param int $idImportedService
	 * @param \Entity\ImportedService->object $importedServiceObj
	 */
	private function getPassengersForm($idImportedService, $importedServiceObj)
	{

		$container = new \Engine\Form\Container\Table();

		$label=new \Engine\Form\Input\Label();
		$label->setDetails($this->lang['index']['passSupp']['passengers']);
		$label->setStyle("noLabel", true);
		$container->addElement($label);

		if ($importedServiceObj and $importedServiceObj->passengers) {

			foreach ($importedServiceObj->passengers as $psgIdx=>$importedPassengerObj) {
				$pass = new \App\Form\Input\Autocomplete_ImportedOrderCustomer(null, "importedServicesList[{$idImportedService}][passengers][{$psgIdx}]");
				$pass->setMandatory(true);
				$pass->setValue($this->input['formData']['importedServicesList'][$idImportedService]['passengers'][$psgIdx]);
				$pass->setStyle("noLabel", true);
				$pass->setStyle("width", 180);
				$pass->setFilters('hideDuplicates', 1);
				$pass->setIdImportedService($idImportedService);
				$pass->setPassengerIdx($psgIdx);
				$pass->setHideSelector(true);
				if ($this->input['formData']['importedServicesList'][$idImportedService]['hasOffer']) {
					$pass->onChange("refreshOfferTotal($idImportedService)");
				}
				$container->addElement($pass);

				$container->addElement(new \Engine\Form\Input\Hidden('', "importedServicesList[{$idImportedService}][passengers][{$psgIdx}][tickets]" , $this->input['formData']['importedServicesList'][$idImportedService]['passengers'][$psgIdx]['tickets']));
			}
		}

		return $container;
	}

	/**
	 * `draw` supplier container
	 * @param int $idImportedService
	 * @param \Entity\ImportedService->object $importedServiceObj
	 */
	private function getSupplierForm($idImportedService, \App\ImportedOrder\Service $importedServiceObj)
	{

		$impOffObj = $importedServiceObj->chosenOffer;

		$container = new \Engine\Form\Container\Table();

		if ($impOffObj and $impOffObj->supplier) {

			$label=new \Engine\Form\Input\Label();
			$label->setDetails($this->lang['index']['passSupp']['supplier']);
			$label->setStyle("noLabel", true);
			$container->addElement($label);

			$supplier = new \App\Form\Input\Autocomplete_ImportedOrderSupplier(null, "importedServicesList[$idImportedService][supplier]");
			$supplier->setMandatory(true);
			$supplier->setValue($this->input['formData']['importedServicesList'][$idImportedService]['supplier']);
			$supplier->setStyle('noLabel', true);
			$supplier->setStyle("width", 180);
			$supplier->setFilters('hideDuplicates', 1);
			$supplier->setIdImportedService($idImportedService);
			$supplier->onChange("refreshOfferTotal($idImportedService)");
			$supplier->setImportedSupplier($impOffObj->supplier);
			$container->addElement($supplier);
		}

		return $container;
	}

}
