<?php
namespace Modules\Admin\Config\Services\Advanced\Offer;

class Controller extends \App\Mvc\Controller\Form\PopupAbstract
{

	/**
	 * {@inheritDoc}
	 * @see \App\Mvc\Controller\Form\PopupAbstract::initialize()
	 */
	protected function initialize()
	{
		parent::initialize();

		$this->actionsCfg['index'] = [ 'checkSection' => 'Modules/Admin/Config/Services/Advanced', 'checkOption' => false ];
	}

	/**
	 * (non-PHPdoc)
	 * @see \App\Mvc\Controller\Form\WindowAbstract::getDefaultFormData()
	 */
	protected function getDefaultFormData($R)
	{
		$formData = [];

		$formData['calculation'] 	= $R['calculation'];
		$formData['serviceCode']	= $R['serviceCode'];
		$formData['location'] 		= $R['location'];

		// GETS CLIENT TYPE DETAILS
		$clientType = \App\Config\Services_Calculations_ClientTypes::getAdmin()->getClientType($R['serviceCode'], $R['location'], $R['calculation']);

		// GETS OFFER DEFAULT DETAILS
		$offerDeafultDetails = \App\Config\Services_Calculations_OfferDefaults::getAdmin()->getCalculationDetails($R['serviceCode'], $R['location'], $R['calculation']);

		// GETS OFFER VAT DETAILS
		$offerVATDetails = \App\Config\Services_Calculations_OfferVat::getAdmin()->getCalculationDetails($R['serviceCode'], $R['location'], $R['calculation']);

		$formData['defaultDetails'] = $offerDeafultDetails;

		// set request for client types
		if ($clientType == 'customers' or $clientType == 'corporates') {
			$formData['clientType'] = [$clientType];
		}
		else {
			$formData['clientType'] = ['customers', 'corporates'];
		}

		// set request for offer vat
		$formData['vatDetails'] = $offerVATDetails;


		return $formData;
	}

	/**
	 * (non-PHPdoc)
	 * @see \App\Mvc\Controller\Form\WindowAbstract::processFormData()
	 */
	protected function processFormData(Array $R, \App\MessagesCollector $msgs)
	{
		$calcCode = $R['serviceCode'].'_'.$R['location'].'_'.$R['calculation'];

		\App\Config\Services_Calculations_OfferDefaults::getAdmin()->set($calcCode, $R['defaultDetails']);

		// client type
		if (is_array($R['clientType']) and count($R['clientType'])==2) {
			$clientType='both';
		}
		elseif($R['clientType']['customers']) {
			$clientType='customers';
		}
		elseif ($R['clientType']['corporates']) {
			$clientType='corporates';
		}
		else {
			$clientType=null;
		}

		$setVatDetails=$R['vatDetails'];

		// offer rows
		foreach (['supplier', 'commission', 'serviceFee', 'discount', 'tax'] as $offRow) {

			// inactive offer rows
			if (!$setVatDetails[$offRow]) {
				$setVatDetails[$offRow]= ['percent' => 0, 'included' => false];
			}
		}

		try {
			// save client type
			\App\Config\Services_Calculations_ClientTypes::getAdmin()->set($calcCode, $clientType);

			// save vat details
			\App\Config\Services_Calculations_OfferVat::getAdmin()->setCalculationDetails($R['serviceCode'], $R['location'], $R['calculation'], $setVatDetails);

			$msgs->addSuccess($this->lang['index']['messages']['updatedOK']);
			return true;
		}
		catch (\App\Config\MessageException $e) {
			$msgs->addError($e->getMessage());
		}

	}

	protected function preRenderOperations(\App\Mvc\ViewAbstract $view, array $formData)
	{
		$_LANG = \App\Loader_Lang::getInstance()->getAll();

		$servName 	= \App\Config\Services_Basic::getInstance()->getServiceName($formData['serviceCode']);
		$servLoc 	= $_LANG['locations']['countriesCriteria']['long'][$formData['location']];
		$servCalc 	= \App\Config\Services_Calculations::getAdmin()->getServiceLocationCalculationName($formData['serviceCode'], $formData['location'], $formData['calculation']);

		$view->setPageTitle(sprintf($this->lang['index']['pageTitle'], $servName, $servLoc, $servCalc));
	}

}
