<?php
namespace Modules\Admin\AgencyCreditCards;

class Model extends \App\Mvc\Model_ListingAbstract
{
	/**
	 * {@inheritDoc}
	 * @see \App\Mvc\Model_ListingAbstract::list_execute()
	 */
	public function list_execute(\App\Mvc\View\Listing\Members\Params $listingParams)
	{

		$filters = $listingParams->getFilters();

		$listObj = new \App\EntityList\AgencyCreditCards();
		$listObj->setColumns(['agencyCreditCards.name', 'agencyCreditCards.comments', 'agencyCreditCards.idCreditCard', 'agencyCreditCards.idAddUser', 'agencyCreditCards.addTime', 'creditCards.company']);

		if ($filters['name']) {
			$listObj->setFilter('agencyCreditCards.name', 'LIKE', "%{$filters['name']}%");
		}

		if ($filters['comments']) {
			$listObj->setFilter('agencyCreditCards.comments', 'LIKE', "%{$filters['comments']}%");

		}

		if ($filters['company']) {
			$listObj->setFilter('creditCards.company', '=', $filters['company']);
		}

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

		$agencyCreditCards = $listObj->exportRecords();
		$agencyCreditCardsCount = $listObj->getRecordsCount();

		$recordList = [];
		foreach ($agencyCreditCards as $agencyCreditCard) {
			$agencyCC = \Entity\AgencyCreditCard::select($agencyCreditCard['id']);
			$agencyCreditCard['number'] = $agencyCC->cardDetails['truncatedNumber'];
			$agencyCreditCard['expireDate'] = $agencyCC->cardDetails['expireYm'];
			$recordList[] = $agencyCreditCard;
		}

		$this->recordsList = $recordList;
		$this->totalRecordsCount = $agencyCreditCardsCount;
	}

}