<?php
namespace Modules\Admin\CompanyData;

class Controller extends \App\Mvc\ControllerAbstract
{

	protected $formSubmitButtonName='save';


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

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

	}


	/**
	 * Action index
	 * @param Array $R
	 * @return \Engine\Mvc\Response\Html
	 */
	public function actionIndex($R)
	{
		$CFG = \Engine\Loader_Cfg::getInstance()->getGlobal();

		$msgs = \App\MessagesCollector::create();

		if (!$R[$this->formSubmitButtonName]) {
			$model = \App\Config\AgencyData::getInstance();
			$formData = [];
			if ($CFG['useAlternativeNames']['companyName']) {
				$formData['company'] = [
					'standard'=> [
						'prefix'=>$model->get('companyPrefix'),
						'company'=>$model->get('companyName'),
						'suffix'=>$model->get('companySuffix'),
					],
					'local'=> [
						'prefix'=>$model->get('companyPrefix2'),
						'company'=>$model->get('companyName2'),
						'suffix'=>$model->get('companySuffix2'),
					],
				];
			} else {
				$formData['company'] = [
					'standard'=> [
						'prefix'=>$model->get('companyPrefix'),
						'company'=>$model->get('companyName'),
						'suffix'=>$model->get('companySuffix'),
					],
				];
			}
			$formData['fiscalCode'] = $model->get('fiscalCode');
			$formData['vatNumber']= $model->get('vatNumber');
			$formData['regNo'] 		= $model->get('regNo');
			$formData['regCapital'] = $model->get('regCapital');
			if ($CFG['useAlternativeNames']['companyLiablePerson']) {
				$formData['liablePerson'] = [
					'standard'=>$model->get('liablePerson'),
					'local'=>$model->get('liablePerson2'),
				];
			} else {
				$formData['liablePerson'] = [
					'standard'=>$model->get('liablePerson'),
				];
			}
			if ($CFG['useAlternativeNames']['companyRegistryAddress']) {
				$formData['registryAddress'] = [
					'standard'=>$model->get('registryAddress'),
					'local'=>$model->get('registryAddress2'),
				];
			} else {
				$formData['registryAddress'] = [
					'standard'=>$model->get('registryAddress'),
				];
			}
			$formData['travelLicenseNo'] 	= $model->get('travelLicenseNo');
			$formData['telephone'] 	= $model->get('telephone');
			$formData['fax'] 		= $model->get('fax');
			$formData['email'] 		= $model->get('email');
		} else {
			$formData = $R;
			$model = \App\Config\AgencyData::getAdmin();
			$model->set('companyName', $formData['company']['standard']['company']);
			$model->set('companyPrefix', $formData['company']['standard']['prefix']);
			$model->set('companySuffix', $formData['company']['standard']['suffix']);
			if ($formData['company']['local']) {
				$model->set('companyName2', $formData['company']['local']['company']);
				$model->set('companyPrefix2', $formData['company']['local']['prefix']);
				$model->set('companySuffix2', $formData['company']['local']['suffix']);
			}
			$model->set('liablePerson', $formData['liablePerson']['standard']);
			if ($formData['liablePerson']['local']) {
				$model->set('liablePerson2', $formData['liablePerson']['local']);
			}
			$model->set('fiscalCode', $formData['fiscalCode']);
			$model->set('vatNumber', $formData['vatNumber']);
			$model->set('regNo', $formData['regNo']);
			$model->set('regCapital', $formData['regCapital']);
			$model->set('registryAddress', $formData['registryAddress']['standard']);
			if ($formData['registryAddress']['local']) {
				$model->set('registryAddress2', $formData['registryAddress']['local']);
			}
			$model->set('travelLicenseNo', $formData['travelLicenseNo']);
			$model->set('telephone', $formData['telephone']);
			$model->set('fax', $formData['fax']);
			$model->set('email', $formData['email']);
			$model->save();

			$msgs->addSuccess($this->lang['messages']['success']);
		}

		// instantiate the view
		$view = new View_Index($this);

		// send data to the view
		$view->addInput('formData', $formData);

		$view->getMessagesCollector()->appendMessages($msgs);
		$view->addInput('formSubmitButtonName', $this->formSubmitButtonName);

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

}
