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

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 [
			"systems" => 							(array)unserialize((string)\App\Bsp\Config::getInstance()->get("systems")),
			"services" => 							(array)unserialize((string)\App\Bsp\Config::getInstance()->get("services")),
			"taxManipulationsMatrix" => 			unserialize(@gzdecode(base64_decode((string)\App\Bsp\Config::getInstance()->get("taxManipulationsMatrix")))),
			"supplierInvoiceVat" => 				(array)unserialize((string)\App\Bsp\Config::getInstance()->get("supplierInvoiceVat")),
			"commissionHandling" => 				(string)\App\Bsp\Config::getInstance()->get("commissionHandling"),
		];
	}

	/**
	 * (non-PHPdoc)
	 * @see \App\Mvc\Controller\Form\WindowAbstract::processFormData()
	 */
	protected function processFormData(Array $R, \App\MessagesCollector $msgs)
	{
		// validate the form
		$formIsValid = true;

		foreach ($R['supplierInvoiceVat'] as $srvk=>$srv) {
			$srvCode = null;
			foreach ($srv as $destk=>$destVats) {
				foreach ($destVats as $componentk=>$vat) {
					if ($formIsValid && !$vat) {
						$msgs->addWarning($this->lang['index']['messages']['supplierInvoice_notConfigured']);
						$formIsValid = false;
					}

					list($thisSrvCode, ) = explode("|", $vat);
					if ($srvCode && $srvCode!=$thisSrvCode) {
						$msgs->addWarning(sprintf($this->lang['index']['messages']['supplierInvoice_allVatsShouldBeForSameService'], $srvk, $destk));
						$formIsValid = false;
					}

					$srvCode = $thisSrvCode;
				}
			}
		}

		if ($formIsValid) {
			// save formData
			\App\Bsp\Config::getAdmin()->setAll([
				"taxManipulationsMatrix" => 				base64_encode(gzencode(serialize(array_values(array_filter((array)$R['taxManipulationsMatrix'], function ($row) {
					return $row['service'] && $row['taxCode'];
				}))))),

				"systems" => 								serialize(array_values((array)$R['systems'])),
				"services" => 								serialize(array_values((array)$R['services'])),
				"supplierInvoiceVat" => 					serialize((array)$R['supplierInvoiceVat']),
				'commissionHandling' =>						(string)$R['commissionHandling'],
			]);

			\App\Bsp\Config::getAdmin()->save();

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

			return true;
		}

		return false;
	}

	/**
	 * 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'])]);
	}
}
