<?php
namespace Modules\Admin\KamSalesTargets;

class Controller extends \App\Mvc\Controller\Window\PaginatedListing\PaginatedListing
{
	/**
	 * Store the ordering list columns
	 *
	 * @var array
	 */
	protected $listOrderingColumns = ['year'];

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

		$this->actionsCfg['insert'] = 				['checkSection' => true, 	'checkOption' => false];
		$this->actionsCfg['update'] = 				['checkSection' => true, 	'checkOption' => false];
		$this->actionsCfg['delete'] = 				['checkSection' => true, 	'checkOption' => false];
	}


	protected function optionDefineParams(\App\Mvc\View\Utils\RowOptions\Option\Option $option, $selection)
	{
		switch ($option->getId()) {

			case 'insert': case 'update': case 'delete':
			break;

			default:
				return false;
		}

		return true;
	}



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

	/**
	 * (non-PHPdoc)
	 * @see \App\Mvc\Controller\Window\PaginatedListing\PaginatedListing::insert_getDefaultFormData()
	 */
	protected function insert_getDefaultFormData($R)
	{
		return [
			'salesTarget' => [
				1 =>  '0.00', 2 =>  '0.00', 3 =>  '0.00',
				4 =>  '0.00', 5 =>  '0.00', 6 =>  '0.00',
				7 =>  '0.00', 8 =>  '0.00', 9 =>  '0.00',
				10 => '0.00', 11 => '0.00', 12 => '0.00',
			],
			'contactsTarget' => [
				1 =>  0, 2 =>  0, 3 =>  0,
				4 =>  0, 5 =>  0, 6 =>  0,
				7 =>  0, 8 =>  0, 9 =>  0,
				10 => 0, 11 => 0, 12 => 0,
			],
		];
	}

	/**
	 *
	 * {@inheritDoc}
	 * @see \App\Mvc\Controller\Window\PaginatedListing\PaginatedListing::insert_doInsert()
	 */
	protected function insert_doInsert($formData, \App\MessagesCollector $msgs)
	{
		$model = new Model();

		\DB::startTransaction();

		try {
			foreach ($formData['salesTarget'] as $month=>$salesTarget) {
				$model->insertKamMonthTargets($formData['idKam']['id'], $formData['year'], $month, $formData['salesTarget'][$month], $formData['contactsTarget'][$month]);
			}

			$msgs->addSuccess($this->lang['messages']['insertSuccess']);
			$this->insertIdRecord = sprintf("%s-%s", $formData['idKam']['id'], $formData['year']);

			\DB::commitTransaction();
			return true;
		}
		catch (\App\MessagesException $ex) {
			\DB::rollbackTransaction();
			$msgs->appendMessages($ex->getMessagesCollector());
		}
	}

	/**
	 *
	 * {@inheritDoc}
	 * @see \App\Mvc\Controller\Window\PaginatedListing\PaginatedListing::update_getDefaultFormData()
	 */
	protected function update_getDefaultFormData($idEntity)
	{
		list($idKam, $year) = explode("-", $idEntity);

		$model = new Model();

		$res = $model->getKamYearTargets($idKam, $year);

		return [
			'idKam' => $idKam,
			'year' => $year,
			'salesTarget' => [
					1 =>  $res[1]['salesTarget'],  2 =>  $res[2]['salesTarget'],  3 =>  $res[3]['salesTarget'],
					4 =>  $res[4]['salesTarget'],  5 =>  $res[5]['salesTarget'],  6 =>  $res[6]['salesTarget'],
					7 =>  $res[7]['salesTarget'],  8 =>  $res[8]['salesTarget'],  9 =>  $res[9]['salesTarget'],
					10 => $res[10]['salesTarget'], 11 => $res[11]['salesTarget'], 12 => $res[12]['salesTarget'],
			],
			'contactsTarget' => [
					1 =>  $res[1]['contactsTarget'],  2 =>  $res[2]['contactsTarget'],  3 =>  $res[3]['contactsTarget'],
					4 =>  $res[4]['contactsTarget'],  5 =>  $res[5]['contactsTarget'],  6 =>  $res[6]['contactsTarget'],
					7 =>  $res[7]['contactsTarget'],  8 =>  $res[8]['contactsTarget'],  9 =>  $res[9]['contactsTarget'],
					10 => $res[10]['contactsTarget'], 11 => $res[11]['contactsTarget'], 12 => $res[12]['contactsTarget'],
			],
		];

	}

	/**
	 *
	 * {@inheritDoc}
	 * @see \App\Mvc\Controller\Window\PaginatedListing\PaginatedListing::update_doUpdate()
	 */
	protected function update_doUpdate($formData, \App\MessagesCollector $msgs)
	{
		list($idKam, $year) = explode("-", $formData['id']);

		$model = new Model();

		\DB::startTransaction();

		try {
			foreach ($formData['salesTarget'] as $month=>$salesTarget) {
				$model->updateKamMonthTargets($idKam, $year, $month, $formData['salesTarget'][$month], $formData['contactsTarget'][$month]);
			}

			//$id = $model->updateKamTargets($idKam, $year, $formData['salesTarget'], $formData['contactsTarget']);
			$msgs->addSuccess($this->lang['messages']['updateSuccess']);
			$this->updateIdRecord = sprintf("%s-%s", $idKam, $year);

			\DB::commitTransaction();
			return true;
		}
		catch (\App\MessagesException $ex) {
			\DB::rollbackTransaction();
			$msgs->appendMessages($ex->getMessagesCollector());
		}
	}


	/**
	 *
	 * {@inheritDoc}
	 * @see \App\Mvc\Controller\Window\PaginatedListing\PaginatedListing::delete_doDelete()
	 */
	protected function delete_doDelete($recordsIds, \App\MessagesCollector $msgs, $R)
	{
		list($idKam, $year) = explode("-", $recordsIds[0]);

		$model = new Model();

		\DB::startTransaction();

		try {
			for ($m=1; $m<=12; $m++) {
				$model->deleteKamMonthTargets($idKam, $year, $m);
			}
			\DB::commitTransaction();

			// remove this record from the internal list
			$this->getListingSelection()->delete($recordsIds);
			$msgs->addSuccess($this->lang['messages']['deleteSuccess']);
		}
		catch (\App\MessagesException $ex) {
			\DB::rollbackTransaction();
			$msgs->appendMessages($ex->getMessagesCollector());
		}
	}

}