<?php
namespace Modules\Admin\Config\Gds\Sabre;

class Controller extends \App\Mvc\Controller\Form\WindowAbstract
{
	protected function initialize()
	{
		parent::initialize();
		$this->actionsCfg['generateInputMatrixRow'] = [];
	}

	/**
	 * (non-PHPdoc)
	 * @see \App\Mvc\Controller\Form\WindowAbstract::getDefaultFormData()
	 */
	protected function getDefaultFormData($R)
	{
		return [
			'importPassengerEmail' => 							\App\Sabre\Config::getInstance()->get("importPassengerEmail"),
			'importPassengerEmail_ignoredAddresses' => 			unserialize((string)\App\Sabre\Config::getInstance()->get("importPassengerEmail_ignoredAddresses")),
			'importPassengerTelephone' => 						\App\Sabre\Config::getInstance()->get("importPassengerTelephone"),
			'importPassengerTelephone_ignoredPhoneNumbers' => 	unserialize((string)\App\Sabre\Config::getInstance()->get("importPassengerTelephone_ignoredPhoneNumbers")),
			'importCurrency' => 								\App\Sabre\Config::getInstance()->get("importCurrency"),
			'importServiceFeeForService' => 					\App\Sabre\Config::getInstance()->get("importServiceFeeForService"),
			"importServiceFeeForServiceCurrency" => 			\App\Sabre\Config::getInstance()->get("importServiceFeeForServiceCurrency"),
			"importDiscountForService" => 						\App\Sabre\Config::getInstance()->get("importDiscountForService"),
			"importDiscountForServiceCurrency" => 				\App\Sabre\Config::getInstance()->get("importDiscountForServiceCurrency"),
			"importCommissionForService" => 					\App\Sabre\Config::getInstance()->get("importCommissionForService"),
			"importCommissionForServiceCurrency" => 			\App\Sabre\Config::getInstance()->get("importCommissionForServiceCurrency"),
			'ignoreSupplierOnDocuments' => 						unserialize((string)\App\Sabre\Config::getInstance()->get("ignoreSupplierOnDocuments")),
			'generalRemarksAssociatedWith' =>					\App\Sabre\Config::getInstance()->get("generalRemarksAssociatedWith"),
			'onLoadPriceFromRemarkStrategy' => 					\App\Sabre\Config::getInstance()->get("onLoadPriceFromRemarkStrategy"),
			'ignoredCompanyIds' => 								unserialize((string)\App\Sabre\Config::getInstance()->get("ignoredCompanyIds")),
			'taxManipulationsMatrix' => 						unserialize(@gzdecode(base64_decode((string)\App\Sabre\Config::getInstance()->get("taxManipulationsMatrix")))),
			'vatPercent' =>										unserialize((string)\App\Sabre\Config::getInstance()->get("vatPercent")),
		];
	}

	/**
	 * (non-PHPdoc)
	 * @see \App\Mvc\Controller\Form\WindowAbstract::processFormData()
	 */
	protected function processFormData(Array $R, \App\MessagesCollector $msgs)
	{

		//////////////////////////////////////////////////////////////////////////////////////
		// SAVE DATA ON SABRE CONFIG
		\App\Sabre\Config::getAdmin()->setAll([
			"importPassengerEmail" => 							$R['importPassengerEmail'],
			"importPassengerEmail_ignoredAddresses" => 			serialize(array_values(array_filter((array)$R['importPassengerEmail_ignoredAddresses']))),
			"importCurrency" => 								$R['importCurrency'],
			'importServiceFeeForService' => 					$R['importServiceFeeForService'],
			"importServiceFeeForServiceCurrency" => 			$R['importServiceFeeForServiceCurrency'],
			"importDiscountForService" => 						$R['importDiscountForService'],
			"importDiscountForServiceCurrency" => 				$R['importDiscountForServiceCurrency'],
			"importCommissionForService" => 					$R['importCommissionForService'],
			"importCommissionForServiceCurrency" => 			$R['importCommissionForServiceCurrency'],
			"importPassengerTelephone" => 						$R['importPassengerTelephone'],
			"importPassengerTelephone_ignoredPhoneNumbers" => 	serialize(array_values(array_filter((array)$R['importPassengerTelephone_ignoredPhoneNumbers']))),
			"ignoreSupplierOnDocuments" => 						serialize(array_filter((array)$R['ignoreSupplierOnDocuments'])),
			"generalRemarksAssociatedWith" => 					$R['generalRemarksAssociatedWith'],
			"onLoadPriceFromRemarkStrategy" => 					$R['onLoadPriceFromRemarkStrategy'],
			"ignoredCompanyIds" => 								serialize(array_values(array_filter((array)$R['ignoredCompanyIds']))),
			"taxManipulationsMatrix" => 						base64_encode(gzencode(serialize(array_values(array_filter((array)$R['taxManipulationsMatrix'], function ($row) {
				return ($row['service'] || $row['offerLocationCriterion']) && $row['taxCode'];
			}))))),
			'vatPercent' =>								serialize((array)$R['vatPercent']),
		]);
		\App\Sabre\Config::getAdmin()->save();

		$msgs->addSuccess($this->lang['index']['saveOk']);

		return true;
	}


	/**
	 * Render a new matrix row
	 * @param array $R
	 * @return \Engine\Mvc\Response\Html
	 */
	protected function actionGenerateInputMatrixRow($R)
	{
		$matrix = \Engine\Cache\General::getInstance('disk')->get($R['inputMatrixStorageId']);
		if (!$matrix) {
			throw new \Engine\Mvc\ControllerException("Matrix not found");
		}

		$view = $this->buildViewObjectFromCurrentModuleForAction('index');

		return new \Engine\Mvc\Response\Json(['responseRow'=>$view->renderInputMatrixRowOnAjaxRequest($matrix, $R['index'], $R['counter'])]);
	}
}
