<?php

namespace Modules\Admin\WebhookQueue;

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 the departments list
		$listObj = new \App\EntityList\WebhookQueue();
		$listObj->setColumns( ['wq.entity', 'wq.id', 'wq.action',  'wq.lastSentValue', 'wq.toSend', 'wq.addTime', 'wq.lastUpdateTime', 'wq.lastSendTime' ] );

		if ($filters['addDateTime']['start'] and substr($filters['addDateTime']['start'], 0, 10)!=DATE_LIMIT_MIN) {
			$listObj->setFilter("wq.addTime", '>=', $filters['addDateTime']['start']);
		}
		if ($filters['addDateTime']['end'] and substr($filters['addDateTime']['end'], 0, 10)!=DATE_LIMIT_MIN) {
			$listObj->setFilter("wq.addTime", '<=', $filters['addDateTime']['end']);
		}

		if ($filters['sendStatus']=='toSend') {
			$listObj->setFilter("wq.toSend", '=', 1);
		} elseif ($filters['sendStatus']=='sent') {
			$listObj->setFilter("wq.toSend", '=', 0);
		}

		if ($filters['id']) {
			$listObj->setFilter("wq.id", '=', $filters['id']);
		}
		if ($filters['entity']) {
			$listObj->setFilter("wq.entity", '=', $filters['entity']);
		}
		if ($filters['action']) {
			$listObj->setFilter("wq.action", '=', $filters['action']);
		}


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

		// these must be called in this specific order
		$this->recordsList = $listObj->exportRecords();
		$this->totalRecordsCount = $listObj->getRecordsCount();
	}

}
