<?php

namespace Modules\RemoteData\EntityManagementWs\Model\Entity\Customer;

class Search extends \Modules\RemoteData\EntityManagementWs\Model\Entity\SearchAbstract
{

	/**
	 * {@inheritDoc}
	 * @see \Modules\RemoteData\EntityManagementWs\Model\Entity\SearchAbstract::search()
	 */
	protected function search() :void
	{

		// GETS THE RAW DETAILS OF THE SEARCH /////////////////////////////////////////////////////
		$words = preg_split("/[\s,]+/", $this->request->searchString);

		$entityList = new \App\EntityList\Customers();
		$entityList->setColumns([
			'firstName', 'middleName', 'lastName',
			'firstName2', 'middleName2', 'lastName2', 'nameAlphabet',
			'birthday', 'email', 'telephone', 'idCorporate',
		]);

		foreach($words as $word) {
			if ($this->request->searchMode=='contains') { // contains
				$entityList->setCustomFilter(
					"lastName LIKE ? OR lastName2 LIKE ? OR firstName LIKE ? OR firstName2 LIKE ? OR email LIKE ?",
					["%{$word}%", "%{$word}%", "%{$word}%", "%{$word}%", "%{$word}%"]
				);
			} else { // starts
				$entityList->setCustomFilter(
					"lastName LIKE ? OR lastName2 LIKE ? OR firstName LIKE ? OR firstName2 LIKE ? OR email LIKE ?",
					["{$word}%", "{$word}%", "{$word}%", "{$word}%", "{$word}%"]
				);
			}
		}

		$entityList->setFilter('idRealCustomer', '=', 0);

		if ($this->request->idCorporate??null) {
			$entityList->setFilter('idCorporate', '=', $this->request->idCorporate);
		}
		// order the results
		if ($this->request->columnOrder and $this->request->directionOrder) {
			$entityList->setOrderBy($this->request->columnOrder, $this->request->directionOrder);
		}
		elseif ($this->request->columnOrder and !$this->request->directionOrder) {
			$entityList->setOrderBy($this->request->columnOrder, 'ASC');
		}
		else {
			$entityList->setOrderBy('lastName', 'ASC');
			$entityList->setOrderBy('firstName', 'ASC');
		}

		$entityList->setLimit((Int)$this->request->offset, $this->request->limit);

		$records = $entityList->exportRecords();

		// COMPOSE THE OUTPUT /////////////////////////////////////////////////////////////////////
		$listResults = [];
		foreach ($records as $record) {

			$object = new \stdClass();

			$object->id	= $record['id'];

			if ($record['nameAlphabet'] == 'standard') {
				$object->lastNameStandard 		= $record['lastName'];
				$object->firstNameStandard 		= $record['firstName'];
				$object->middleNameStandard 	= $record['middleName'];
				$object->lastNameLocal			= $record['lastName2'];
				$object->firstNameLocal 		= $record['firstName2'];
				$object->middleNameLocal 		= $record['middleName2'];
			}
			else {
				$object->lastNameStandard 		= $record['lastName2'];
				$object->firstNameStandard 		= $record['firstName2'];
				$object->middleNameStandard 	= $record['middleName2'];
				$object->lastNameLocal 			= $record['lastName'];
				$object->firstNameLocal 		= $record['firstName'];
				$object->middleNameLocal 		= $record['middleName'];
			}

			$object->idCorporate = $record['idCorporate'];
			if ($record['email']) {
				$object->email 		= $record['email'];
			}
			if ($record['telephone']) {
				$object->telephone 	= $record['telephone'];
			}
			if ($record['birthday']) {
				$object->birthday 	= $record['birthday'];
			}

			$listResults[] = $object;
		}

		// populate the response with records and total results
		$this->populateSearchResponse($listResults, $entityList->getRecordsCount());

	}

}
