<?php
namespace Modules\Admin\QcRules;

class Model extends \App\Mvc\Model_ListingAbstract
{

	/**
	 * (non-PHPdoc)
	 * @see \App\Mvc\Model_ListingAbstract::list_execute()
	 */
	public function list_execute(\App\Mvc\View\Listing\Members\Params $listingParams)
	{
		$filters = $listingParams->getFilters();

		// get list
		$listObj = new \App\EntityList\QcRules();
		$listObj->setColumns(['active', 'entity', 'name', 'object', 'idAddUser', 'lastUpdateTime', 'idUpdateUser', 'updatesCount', 'addTime']);

		// entity filter
		if ($filters['entity']) {
			$listObj->setFilter('entity', '=', $filters['entity']);
		}

		// status filter
		if ($filters['status'] == 'active') {
			$listObj->setFilter('active', '=', '1');
		}
		elseif ($filters['status'] == 'inactive') {
			$listObj->setFilter('active', '=', '0');
		}

		// name of rule filter
		if ($filters['nameOfRule']) {
			$listObj->setFilter('name', 'LIKE', "%".$filters['nameOfRule']."%");
		}

		$cacheFiltersList = $cacheFiltersVals = [];

		// corporate filter
		if ($filters['corporate']['id']) {
			$cacheFiltersList[] = "( (qcRulesCache.field=? AND qcRulesCache.value=?) OR (qcRulesCache.field=? AND qcRulesCache.value=?) )";
			$cacheFiltersVals[] = 'Corporate';
			$cacheFiltersVals[] = $filters['corporate']['id'];
			$cacheFiltersVals[] = 'OrderOwnerCorporate';
			$cacheFiltersVals[] = $filters['corporate']['id'];
		}

		// trust filter
		if ($filters['trust']['id']) {
			$cacheFiltersList[] = "(qcRulesCache.field=? AND qcRulesCache.value=?)";
			$cacheFiltersVals[] = 'CorporateTrust';
			$cacheFiltersVals[] = $filters['trust']['id'];
		}

		// segment filter
		if ($filters['segment']) {
			$cacheFiltersList[] = "(qcRulesCache.field=? AND qcRulesCache.value=?)";
			$cacheFiltersVals[] = 'CorporateSegment';
			$cacheFiltersVals[] = $filters['segment'];
		}

		// add custom cache filters
		if ($cacheFiltersList) {
			$listObj->setCustomFilter(implode(' OR ', $cacheFiltersList), $cacheFiltersVals);
			$listObj->setColumn('qcRulesCache.idRule'); // just to include the table in the sql
			$listObj->setCustomHaving('COUNT(*) = ?', [count($cacheFiltersList)]);
			$listObj->setGroupBy('qcRules.id');
		}

		$listObj = $this->entityList_inheritListingParams($listingParams, $listObj);

		$results = $listObj->exportRecords();
		$recordCount = $listObj->getRecordsCount();

		$resultArr = [];
		foreach($results as $result){
			$restriction = [];
			$filter = [];
			$object = \Entity\QcRule::Select($result['id'])->object;
			foreach ($object->getFilters() as $value) {
				$filter[] = $value->createDescription();
			}
			foreach ($object->getRestrictions() as $value) {
				$restriction[] = $value->createDescription();
			}

			$resultArr[] = [
				'id' => 			$result['id'],
				'entity' => 		$result['entity'],
				'name' => 			$result['name'],
				'filters' => 		$filter,
				'restrictions' => 	$restriction,
				'active' => 		$result['active'],
				'addTime' => 		$result['addTime'],
				'idAddUser' =>		$result['idAddUser'],
			];
		}

		$this->recordsList = $resultArr;
		$this->totalRecordsCount = $recordCount;

	}
}