<?php

namespace App\Reports\Sources\UsersAccess;


class PopulateInitialImport extends \App\Reports\Sources\PopulateAbstract
{

	/**
	 * {@inheritDoc}
	 * @see \App\Reports\Sources\PopulateAbstract::populate()
	 */
	public function populate() :void
	{

		// INITS //////////////////////////////////////////////////////////////////////////////////
		global $DBTUsers, $DBTUsersAccess, $DBTUsersProfilesAccess;

		$reportTable = $this->tableManager->getTableName();

		$activeColumns = array_keys( $this->columnsManager->getStdColumns() );

		$stdFilters = $this->filtersManager->getStdFilters();

		// refresh report status
		$this->reportGenerationStatusUpdate('extractMainDataFromDb', 'runningMainSQL', 0);

		// INSERT THE ACCESS FOR THE USER THAT DON'T HAVE PROFILE SET /////////////////////////////
		$reportCopyInfo = new \App\Reports\Db\ManageRecords\Insert($reportTable);
		$reportCopyInfo->setMainTable($DBTUsers,				'user');
		$reportCopyInfo->addJoinTable($DBTUsersAccess,			'access',		'access.idUser = user.id');

		$cols = [
			'idUser'					=> 'user.id',
			'firstName'					=> 'user.firstName',
			'lastName'					=> 'user.lastName',
			'userProcessedRemarks'		=> 'user.processedRemarks',
			'idOffice'					=> 'user.idOffice',
			'idDepartment'				=> 'user.idDepartment',
			'idProfile'					=> 'user.idUsersAccessProfile',
			'idSection'					=> 'access.idSection',
			'type'						=> 'access.type',
			'optionValue'				=> 'access.optionValue',
		];

		$reportCopyInfo->setStructureColumns($cols);
		$reportCopyInfo->addStructureColumn("id",     	"CONCAT(user.id, '|', access.idSection)", 		"access");

		$reportCopyInfo->setActiveColumns($activeColumns);

		// FILTERS ////////////////////////////////////////////////////////////////////////////////
		if ($stdFilters['users']) {
			$reportCopyInfo->addFilter('user.id', 'IN', $stdFilters['users']);
		}
		// filter the users that don't have profile access
		$reportCopyInfo->addFilter('user.idUsersAccessProfile', '=', 0);
		// filter the users that are deleted
		$reportCopyInfo->addFilter('user.deleted', '=', 0);

		$reportCopyInfo->populate();


		// INSERT THE ACCESS FOR THE USER THAT HAVE PROFILE SET ///////////////////////////////////
		$reportCopyInfo = new \App\Reports\Db\ManageRecords\Insert($reportTable);
		$reportCopyInfo->setMainTable($DBTUsers,					'user');
		$reportCopyInfo->addJoinTable($DBTUsersProfilesAccess,		'profile',		'profile.idProfile = user.idUsersAccessProfile');

		$cols = [
			'idUser'					=> 'user.id',
			'firstName'					=> 'user.firstName',
			'lastName'					=> 'user.lastName',
			'userProcessedRemarks'		=> 'user.processedRemarks',
			'idOffice'					=> 'user.idOffice',
			'idDepartment'				=> 'user.idDepartment',
			'idProfile'					=> 'user.idUsersAccessProfile',
			'idSection'					=> 'profile.idSection',
			'type'						=> 'profile.type',
			'optionValue'				=> 'profile.optionValue',
		];

		$reportCopyInfo->setStructureColumns($cols);
		$reportCopyInfo->addStructureColumn("id",     	"CONCAT(user.id, '|', profile.idSection)", 		"profile");

		$reportCopyInfo->setActiveColumns($activeColumns);

		// FILTERS ////////////////////////////////////////////////////////////////////////////////
		if ($stdFilters['users']) {
			$reportCopyInfo->addFilter('user.id', 'IN', $stdFilters['users']);
		}
		// filter the users that have profile access
		$reportCopyInfo->addFilter('user.idUsersAccessProfile', '>', 0);
		// filter the users that are deleted
		$reportCopyInfo->addFilter('user.deleted', '=', 0);

		$reportCopyInfo->populate();

	}

}
