<?php

class Connection{
	private $conn;
  // private $database;
	function __construct($CONN_OBJ)
	{
		$ROOT = realpath($_SERVER["DOCUMENT_ROOT"]);

		include "$ROOT/config.php";
		$this->conn = null;

		// // $connString = "mysql:host=localhost;dbname=db;charset=utf8mb4";
		$connString = "sqlsrv:Server=$CONN_OBJ[HOST_NAME];Database=$CONN_OBJ[DB_NAME];";
		$options = [
			PDO::ATTR_EMULATE_PREPARES   => false,
			PDO::ATTR_EMULATE_PREPARES => true,
			PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
			PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
		];
		$this->conn = new PDO($connString, $CONN_OBJ["DB_USER"], $CONN_OBJ["DB_PWD"], $options);
		// // $this->conn = new PDO($connString, 'usuario', 'contraseña', $options);
	}


  /*
    $query --> (string) consutla 
    $array --> (bool) true= si desea esperar más de un registro como respuesta || false= si solo espera un resultado (un objeto)
  */
	function Select($query, $array=true){
		try{
		    $sth = $this->conn->prepare($query);
		    $sth->execute();
		    $sth->setFetchMode(PDO::FETCH_ASSOC);
        if ($array) $result = $sth->fetchAll();
        else $result = $sth->fetch();
				// $this->conn = null;
		    return $result;
		}
		catch(PDOException $e){
		    error_log('PDOException - ' . $e->getMessage(), 0);
		    http_response_code(500);
		    die($e->getMessage());
	    }
	}

	function SelectOnlyOne($q){
		try{
		    $sth = $this->conn->prepare($q);
		    $sth->execute();
		    $sth->setFetchMode(PDO::FETCH_ASSOC);
		    $result = $sth->fetch();
				$this->conn = null;
		    return $result;
		}
		catch(PDOException $e){
		    error_log('PDOException - ' . $e->getMessage(), 0);
		    http_response_code(500);
		    die($e->getMessage());
	    }
	}

	function SelectAndContinue($q){
		try{
		    $sth = $this->conn->prepare($q);
		    $sth->execute();
		    $sth->setFetchMode(PDO::FETCH_ASSOC);
		    $result = $sth->fetchAll();
		    return $result;
		}
		catch(PDOException $e){
		    error_log('PDOException - ' . $e->getMessage(), 0);
		    http_response_code(500);
		    die($e->getMessage());
	    }
	}

	function SelectOneAndContinue($q){
		try{
				$sth = $this->conn->prepare($q);
				$sth->execute();
				$sth->setFetchMode(PDO::FETCH_ASSOC);
				$result = $sth->fetch();
				return $result;
		}
		catch(PDOException $e){
				error_log('PDOException - ' . $e->getMessage(), 0);
				http_response_code(500);
				die($e->getMessage());
			}
	}


	function ExecuteQuery($q, $parametros){
		try {
		    $sth = $this->conn->prepare($q);
		    $sth->execute($parametros);
		    $this->conn = null;
		}
		catch(PDOException $e){
		    error_log('PDOException - ' . $e->getMessage(), 0);
		    http_response_code(500);
		    die($e->getMessage());
		    return $e->getMessage();
	    }
	}
   function ExecuteQueryAndContinue($q, $parametros){
		try {
		    $sth = $this->conn->prepare($q);
		    $sth->execute($parametros);
		}
		catch(PDOException $e){
		    error_log('PDOException - ' . $e->getMessage(), 0);
		    http_response_code(500);
		    die($e->getMessage());
		    return $e->getMessage();
	    }
	}
	function ObtenerIdInsertado() {
		try {
         return $this->conn->lastInsertId();
		}
		catch(PDOException $e){
		    error_log('PDOException - ' . $e->getMessage(), 0);
		    http_response_code(500);
		    die($e->getMessage());
		    return $e->getMessage();
	    }
	}

	function Procedure($q){
		try {
			$res = $this->conn->prepare($q);
			$res->execute();
			$array = array();
			while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
				$array[] = $row;
			}
			return $array;
		} catch (\Exception $e) {
			error_log($e);
		}
	}

  function DefaultResponse() {
    $response = array(
      "result" => null,
			"message" => "mensaje para mostrar en consola, solo informativo",
      "alert_icon" => 'warning',
      "alert_title" => 'Oh oh',
      "alert_text" => 'No se encontraron registros.',
      // "alert_text" => 'No registers found.',
			"data" => [],
    );
    return $response;
  }
  function CorrectResponse() {
    $response = array(
      "result" => true,
			"message" => "mensaje para mostrar en consola, solo informativo",
      "alert_icon" => 'success',
      "alert_title" => 'Exito',
      "alert_text" => 'Registros cargados.',
      // "alert_text" => 'No registers found.',
			"data" => [],
    );
    return $response;
  }
  function CatchResponse($error_message) {
    error_log($error_message,"./error_log.log");
    $response = array(
      "result" => false,
			"message" => "Peticion fallida | $error_message",
      "alert_icon" => 'error',
      "alert_title" => 'Opps...!',
      "alert_text" => "Ha ocurrido un error, verifica tus datos.\n $error_message",
      // "alert_text" => "An error has occurred, please verify your info.\n $error_message",
			"data" => [],
    );
    return $response;
  }

   function Close() {
      $this->conn = null;
   }
}