<?php

namespace Modules\Admin\CurrencyExchange\HistoryRates;

/**
 * This module is used for processing multiple services at once.
 */
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/CurrencyExchange', 'checkOption' => false ];
	}


	/**
	 * {@inheritDoc}
	 * @see \App\Mvc\Controller\Form\WindowAbstract::getDefaultFormData()
	 */
	protected function getDefaultFormData($R)
	{
		$formData = $R;
		$formData['currencyExchDate'] = TODAY_ISO;

		return $formData;
	}

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

	protected function preRenderOperations($view, $formData)
	{
		$formData = $view->getInput('formData');
		$rates = \App\CurrencyExchange\Date::getInstance($formData['currencyExchDate']);

		$lastDefinedDate = $rates->getLastDefinedDate();
		if ($formData['currencyExchDate'] != $lastDefinedDate) {
			$view->getMessagesCollector()->addWarning(sprintf($this->lang['index']['currFromPreviousDate'], \Engine\DateTime::createFromIsoDate($lastDefinedDate)->formatDateStd(), \Engine\DateTime::createFromIsoDate($formData['currencyExchDate'])->formatDateStd()));
		}
	}
}