<?php

namespace Modules\Admin\Offices;

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\Offices();
		$listObj->setColumns(['name', 'iataCode', 'addTime', 'email', 'telephone', 'idAddUser', 'defaultOffice', 'countryCode', 'cityId', 'address']);

		// FILTER TEXT
		if ($filters['text']) {
			$vars = [$filters['text'], $filters['text'].'%', '%,'.$filters['text'], '%'.$filters['text'].'%', $filters['text']];
			$listObj->setCustomFilter('name LIKE ? OR name LIKE ? OR name LIKE ? OR name LIKE ? OR iataCode = ? ', $vars);
		}

		if ($filters['location']['id']) {
			// extract details
			$locationDetails = explode('|', $filters['location']['id']);
			$listObj->setFilter('countryCode', '=', $locationDetails[0]);
			$listObj->setFilter('cityId', '=', $locationDetails[1]);
		}

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

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

}
