<?php

namespace App\ReportsData\EntityFields;

class Corporate extends \App\ReportsData\EntityFields\GeneralAbstract
{

	/**
	 * Returns the table name that will store the current entity queue.
	 * @return string
	 */
	protected function getDbTableName()
	{
		global $DBTRd_corporates;
		return $DBTRd_corporates;
	}


	protected function getCustomValues($fieldName)
	{
		switch ($fieldName) {
			case 'fullNameStd':
				$this->values[$fieldName] = trim($this->values['companyNameStd'].' '.$this->values['companySuffixStd']);
				return $this->values[$fieldName];

			case 'fullNameLoc':
				$this->values[$fieldName] = trim($this->values['companyNameLoc'].' '.$this->values['companySuffixLoc']);
				return $this->values[$fieldName];

			case 'segment':
				return new CorporateSegment($this->values['idSegment']);
			case 'trust':
				return new Trust($this->values['idTrust']);

			case 'domainCategory':
				return new ActivityDomainCategory($this->values['idDomainCategory']);
			case 'domain':
				return new ActivityDomain($this->values['idDomain']);

			case 'accountManager':
				return new User($this->values['idAccountManager']);
			case 'collectionResponsible':
				return new User($this->values['idCollectionResponsible']);

			case 'realCorporate':
				return new Corporate($this->values['idRealCorporate']);

			case 'addUser':
				return new User($this->values['idAddUser']);
			case 'updateUser':
				return new User($this->values['idUpdateUser']);

			case 'groupElementsIds': {
				global $DBTRd_corporateGroups;
				$db = \DB::getInstance();
				$selectRowsSQL = "SELECT `idGroupElement` FROM `{$DBTRd_corporateGroups}` WHERE `idCorporate` = ?";
				$groupElemList = $db->GetAll($selectRowsSQL, $this->values['id']);
				if ($db->ErrorNo()) {
					throw new \DBException('The RD record could not be selected: '.$db->ErrorMsg());
				}

				return array_column($groupElemList, 'idGroupElement');
			}

			case 'groupElementsNames': {
				if ($this->getValue('groupElementsIds')) {
					$groupList = [];
					foreach ($this->getValue('groupElementsIds') as $idGroupElem) {
						$groupElemDetails = \App\Config\CorporateGroups::getInstance()->getGroupElementDetailsById($idGroupElem);
						$groupList[] = $groupElemDetails['groupName'].':'.$groupElemDetails['groupElementName'];
					}

					$this->values[$fieldName] = implode(' ; ', $groupList);
				}
				else {
					$this->values[$fieldName] = '';
				}

				return $this->values[$fieldName];
			}

			case 'duplicateChildrenIds': {
				global $DBTRd_corporates;
				$db = \DB::getInstance();
				$selectRowsSQL = "SELECT `id` FROM `{$DBTRd_corporates}` WHERE `idRealCorporate` = ?";
				$duplicateChildrenIdsList = $db->GetAll($selectRowsSQL, $this->values['id']);
				if ($db->ErrorNo()) {
					throw new \DBException('The RD record could not be selected: '.$db->ErrorMsg());
				}
				return array_column($duplicateChildrenIdsList, 'id');
			}

			case 'duplicateChildrenIdsList': {
				if ($this->getValue('duplicateChildrenIds')) {
					$this->values[$fieldName] = implode(', ', $this->getValue('duplicateChildrenIds'));
				}
				else {
					$this->values[$fieldName] = null;
				}

				return $this->values[$fieldName];
			}

			case 'duplicateChildrenCount':
				return count($this->getValue('duplicateChildrenIds'));

			// offices list of main office id for the current corporation
			case 'officesListDetails': case 'idMainOffice':
				$db = \DB::getInstance();
				global $DBTRd_corporateOffices;
				$corpOffListSQL = "SELECT id, name, mainOffice, countryCode, cityId, address, zipCode, regNo, telephone, fax, email FROM `{$DBTRd_corporateOffices}` WHERE `idCorporate` = ? AND deleted=0";
				$corpOffListArr = $db->GetAll($corpOffListSQL, [$this->values['id']]);
				$this->values['officesListDetails'] = [];
				$this->values['idMainOffice'] = 0;
				foreach($corpOffListArr as $offDet) {
					$this->values['officesListDetails'][$offDet['id']] = $offDet;
					if ($offDet['mainOffice']) {
						$this->values['idMainOffice'] = $offDet['id'];
					}
				}
				return $this->values[$fieldName];
			case 'mainOffice':
				return new CorporateOffice($this->getValue('idMainOffice'));

			default:
				return parent::getCustomValues($fieldName);
		}

	}

	protected function processDefaultValues()
	{
		$this->values['processedRemarks'] = ($this->values['processedRemarks'])?json_decode($this->values['processedRemarks'], true):[];
	}
}
