<?php
/**
* Classe User.class.php
* @filesource
* @autor		Kenio de Souza
* @copyright	Copyright 2024 - Souza Consultoria Tecnológica
* @package		vendor
* @subpackage	model
* @version		1.0
* @date			07/08/2024
*/


/** Defino o local onde esta a classe */
namespace vendor\model;

class User
{
	/** Declaro as vaiavéis da classe */
	private $connection = null;
	private $sql = null;
	private $stmt = null;
	private $start = null;
	private $max = null;
	private $limit = null;
	private $userId = null;
	private $nameFirst = null;
	private $nameLast = null;
	private $email = null;
	private $password = null;
	private $passwordTemp = null;
	private $active = null;
	private $birthDate = null;
	private $genre = null;
	private $dateRegister = null;
	private $accessFirst = null;
	private $accessLast = null;
	private $administrator = null;
	private $passwordTempConfirm = null;
	private $usersIdCreate = null;
	private $usersIdUpdate = null;
	private $usersIdDelete = null;

	/** Construtor da classe */
	function __construct()
	{
		/** Cria o objeto de conexão com o banco de dados */
		$this->connection = new Mysql();
	}

	/** Carrega os campos de uma tabela */
	public function Describe()
	{

		/** Consulta SQL */
		$this->sql = "describe user";

		/** Preparo o SQL para execução */
		$this->stmt = $this->connection->connect()->prepare($this->sql);

		/** Executo o SQL */
		$this->stmt->execute();

		/** Retorno o resultado */
		$this->field = $this->stmt->fetchAll(\PDO::FETCH_OBJ);

		/** Declara o objeto */
		$resultDescribe = new \stdClass();
		$Field = '';

		/** Lista os campos da tabela para objetos */
		foreach($this->field as $UsersKey => $Result){

			/** Pega o nome do Field/Campo */
			$Field = $Result->Field;

			/** Carrega os objetos como null */
			$resultDescribe->$Field = null;

		}

		/** Retorna os campos declarados como vazios */
		return $resultDescribe;

	}

	/** Lista os registros do banco de dados com limitação */
	public function Get(int $clientId)
	{

		/** Parametros de entrada */
		$this->clientId = $clientId;

		/** Consulta SQL */
		$this->sql = 'select * from user  
					  where client_id = :client_id';

		/** Preparo o SQL para execução */
		$this->stmt = $this->connection->connect()->prepare($this->sql);

		/** Preencho os parâmetros do SQL */
		$this->stmt->bindParam(':client_id', $this->clientId);

		/** Executo o SQL */
		$this->stmt->execute();

		/** Retorno o resultado */
		return $this->stmt->fetchObject();

	}

	/** Lista todos os egistros do banco com ou sem paginação*/
	public function All(int $start, int $max)
	{
		/** Parametros de entrada */
		$this->start = $start;
		$this->max = $max;

		/** Verifico se há paginação */
		if($this->max){
        	$this->limit = "limit $this->start, $this->max";
        }

		/** Consulta SQL */
		$this->sql = 'select * from user '. $this->limit;

		/** Preparo o SQL para execução */
		$this->stmt = $this->connection->connect()->prepare($this->sql);

		/** Executo o SQL */
		$this->stmt->execute();

		/** Retorno o resultado */
		return $this->stmt->fetchAll(\PDO::FETCH_OBJ);

	}

	/** Conta a quantidades de registros */
	public function Count()
	{
		/** Consulta SQL */
		$this->sql = 'select count(client_id) as qtde
					  from user ';

		/** Preparo o SQL para execução */
		$this->stmt = $this->connection->connect()->prepare($this->sql);

		/** Executo o SQL */
		$this->stmt->execute();

		/** Retorno o resultado */
		return $this->stmt->fetchObject()->qtde;

	}

	/** Insere um novo registro no banco */
	public function Save(int $clientId, string $nameFirst, string $nameLast, string $email, string $password, string $passwordTemp, string $active, string $birthDate, string $genre, string $dateRegister, string $accessFirst, string $accessLast, string $administrator, string $passwordTempConfirm, string $usersIdCreate, string $usersIdUpdate, string $usersIdDelete)
	{


		/** Parametros */
		$this->clientId = $clientId;
		$this->nameFirst = $nameFirst;
		$this->nameLast = $nameLast;
		$this->email = $email;
		$this->password = $password;
		$this->passwordTemp = $passwordTemp;
		$this->active = $active;
		$this->birthDate = $birthDate;
		$this->genre = $genre;
		$this->dateRegister = $dateRegister;
		$this->accessFirst = $accessFirst;
		$this->accessLast = $accessLast;
		$this->administrator = $administrator;
		$this->passwordTempConfirm = $passwordTempConfirm;
		$this->usersIdCreate = $usersIdCreate;
		$this->usersIdUpdate = $usersIdUpdate;
		$this->usersIdDelete = $usersIdDelete;
	

		/** Verifica se o ID do registro foi informado */
		if($this->clientId > 0){

			/** Consulta SQL */
			$this->sql = 'update user set name_first = :name_first,
									   	     name_last = :name_last,
									   	     email = :email,
									   	     password = :password,
									   	     password_temp = :password_temp,
									   	     active = :active,
									   	     birth_date = :birth_date,
									   	     genre = :genre,
									   	     date_register = :date_register,
									   	     access_first = :access_first,
									   	     access_last = :access_last,
									   	     administrator = :administrator,
									   	     password_temp_confirm = :password_temp_confirm,
									   	     users_id_create = :users_id_create,
									   	     users_id_update = :users_id_update,
									   	     users_id_delete = :users_id_delete
					  	  where user_id = :user_id';

		}else{//Se o ID não foi informado, grava-se um novo registro

			/** Consulta SQL */
			$this->sql = 'insert into user(client_id, 
											  name_first, 
											  name_last, 
											  email, 
											  password, 
											  password_temp, 
											  active, 
											  birth_date, 
											  genre, 
											  date_register, 
											  access_first, 
											  access_last, 
											  administrator, 
											  password_temp_confirm, 
											  users_id_create, 
											  users_id_update, 
											  users_id_delete 
								 	 ) values (:client_id, 
									  		   :name_first,
									  		   :name_last,
									  		   :email,
									  		   :password,
									  		   :password_temp,
									  		   :active,
									  		   :birth_date,
									  		   :genre,
									  		   :date_register,
									  		   :access_first,
									  		   :access_last,
									  		   :administrator,
									  		   :password_temp_confirm,
									  		   :users_id_create,
									  		   :users_id_update,
									  		   :users_id_delete)';

		}

		/** Preparo o sql para receber os valores */
		$this->stmt = $this->connection->connect()->prepare($this->sql);

		/** Preencho os parâmetros do SQL */
		$this->stmt->bindParam('client_id', $this->clientId);
		$this->stmt->bindParam('name_first', $this->nameFirst);
		$this->stmt->bindParam('name_last', $this->nameLast);
		$this->stmt->bindParam('email', $this->email);
		$this->stmt->bindParam('password', $this->password);
		$this->stmt->bindParam('password_temp', $this->passwordTemp);
		$this->stmt->bindParam('active', $this->active);
		$this->stmt->bindParam('birth_date', $this->birthDate);
		$this->stmt->bindParam('genre', $this->genre);
		$this->stmt->bindParam('date_register', $this->dateRegister);
		$this->stmt->bindParam('access_first', $this->accessFirst);
		$this->stmt->bindParam('access_last', $this->accessLast);
		$this->stmt->bindParam('administrator', $this->administrator);
		$this->stmt->bindParam('password_temp_confirm', $this->passwordTempConfirm);
		$this->stmt->bindParam('users_id_create', $this->usersIdCreate);
		$this->stmt->bindParam('users_id_update', $this->usersIdUpdate);
		$this->stmt->bindParam('users_id_delete', $this->usersIdDelete);

		/** Executo o SQL */
		return $this->stmt->execute();

	}

	/** Deleta um determinado registro no banco de dados */
	function Delete(int $clientId)
	{
		/** Parametros de entrada */
		$this->clientId = $clientId;

		/** Consulta SQL */
		$this->sql = 'delete from user
					  where  user_id = :user_id';

		/** Preparo o sql para receber os valores */
		$this->stmt = $this->connection->connect()->prepare($this->sql);

		/** Preencho os parâmetros do SQL */
		$this->stmt->bindParam('client_id', $this->clientId);

		/** Executo o SQL */
		return $this->stmt->execute();

	}

	/** Fecha uma conexão aberta anteriormente com o banco de dados */
	function __destruct()
	{
		$this->connection = null;
    }
}
