<?php
namespace Modules\Admin\Config\SupplierProducts\Fields;

class Controller extends \App\Mvc\Controller\Window\PaginatedListing\ListingAbstract
{
	/**
	 * Store submit button name
	 * @var String
	 */
	protected $formSubmitButtonName  = 'save';

	/**
	 * The list of columns which support ordering.
	 * @var array
	 */
	protected $listOrderingColumns = ['addTime'];

	/**
	 * (non-PHPdoc)
	 * @see \App\Mvc\Controller\Window\PaginatedListing\PaginatedListing::initialize()
	 */
	protected function initialize()
	{
		parent::initialize();

		$this->actionsCfg['index'] =       ['checkSection'=>true, 'checkOption'=>false];
		$this->actionsCfg['list'] =        ['checkSection'=>true, 'checkOption'=>false];
		$this->actionsCfg['edit'] =        ['checkSection'=>true, 'checkOption'=>false];
	}

	protected function optionDefineParams(\App\Mvc\View\Utils\RowOptions\Option\Option $option, $selection)
	{
		switch($option->getId()){
			case 'edit':
				foreach ($selection as $selectedItem) {
					$model = new \Modules\Admin\Config\SupplierProducts\Fields\Model();
					if (!$model->isGroup($selectedItem)) {
						$option->setDisabled(true);
						$option->addMessage('warning', $this->lang['index']['options']['reasons']['edit']['notAllowedGroup']);
					}
				}
			break;
			default:
				return false;
		}

		return true;
	}

	/**
	 * (non-PHPdoc)
	 * @see \App\Mvc\Controller\Window\PaginatedListing\ListingAbstract::optionGetEntityName()
	 */
	protected function optionGetEntityName($id)
	{
		return NULL;
	}


	/**
	 * set the type of fields for the service selected
	 * @param array $R
	 */
	public function actionEdit($R)
	{
		// instantiate message collector
		$msgs = \App\MessagesCollector::create();

		$formData = [];

		$modelOutput = null;

		// if submit buttons has been pressed
		if ($R[$this->formSubmitButtonName]) {
			$modelOutput = $this->editProcessFormData($R, $msgs);
		}

		// INSTANTIATE VIEW OBJECT
		$view = new View_Edit($this);

		$serviceName = \App\Config\Services_Basic::getInstance()->getServiceName($R['id']);
		$view->setPageTitle(sprintf($this->lang['edit']['pageTitle'], $serviceName));

		if (!$R[$this->formSubmitButtonName]) {
			$formData = $this->editGetDefaultFormData($R);
		}
		else {
			$formData = $R;

		}

		// CLOSE POPUP
		if ($modelOutput) {
			$view->setClosePopup(true);
		}

		// ATTACH INFORMATION TO VIEW
		$view->addInput('formSubmitButtonName', $this->formSubmitButtonName);
		$view->addInput('formData', $formData);
		$view->getMessagesCollector()->appendMessages($msgs);

		// RENDER VIEW
		return new \Engine\Mvc\Response\Html($view->render());
	}

	/**
	 * populate default form data
	 * @param array $R
	 */
	private function editGetDefaultFormData($R)
	{

		$suppProductFields = \App\Config\SupplierProductFields::getInstance();
		$formData = [];
		$formData['fields']['category'] = $suppProductFields->getFieldType($R['id'], 'category');
		$formData['fields']['productName'] = $suppProductFields->getFieldType($R['id'], 'productName');
		$formData['fields']['localization'] = $suppProductFields->getFieldType($R['id'], 'localization');
		$formData['fields']['chain'] = $suppProductFields->getFieldType($R['id'], 'chain');
		$formData['fields']['address'] = $suppProductFields->getFieldType($R['id'], 'address');
		$formData['fields']['zipCode'] = $suppProductFields->getFieldType($R['id'], 'zipCode');
		$formData['fields']['departure'] = $suppProductFields->getFieldType($R['id'], 'departure');
		$formData['fields']['destination'] = $suppProductFields->getFieldType($R['id'], 'destination');

		$formData['categoryDetails']= \App\Config\Services_GeneralAttributes::getInstance()->getServiceCategory($R['id']);
		$formData['productName'] 	= \App\Config\Services_GeneralAttributes::getInstance()->getServiceProductName($R['id']);

		return $formData;
	}

	/**
	 *
	 * @param array $R
	 * @param \App\MessagesCollector $msgs
	 */
	private function editProcessFormData($R, $msgs)
	{

		$serviceCode = $R['id'];
		$supplierProductFields = \App\Config\SupplierProductFields::getAdmin();

		try {
			$supplierProductFields->set($serviceCode, $R['fields']);
			$supplierProductFields->save();
			$msgs->addSuccess($this->lang['edit']['messages']['success']);
			return true;
		}
		catch (\App\Config\MessageException $e)  {
			$msgs->addError($e->getMessage());
		}
	}

}