<?php
#[AllowDynamicProperties]

class ServiceSpacesData
{
	public static $tablename = "service_spaces";

	public function __construct()
	{
		$this->id = "";
		$this->space_name = "";
		$this->observations = "";
		$this->date_reg = "";
	}


	public function add()
	{

		$sql = "INSERT into " . self::$tablename . " (
		space_name, 
		observations
			)";
		$sql .= " VALUES (
			?,
			?
			);";
		$values = [
			$this->space_name,
			$this->observations
		];

		return ExecutorPg::insert($sql, $values);
	}


	public static function getByIdPg($id)
	{
		$sql = "SELECT * from " . self::$tablename . " where id=" . $id;
		$query = ExecutorPg::doit($sql);
		// return $query[0];
		$array = ModelPg::one($query[0][0], new ServiceSpacesData());
		if ($array->id == "") {
			return "null";
		} else {
			return $array;
		}
	}

	public function del()
	{
		$sql = "delete from " . self::$tablename . " where id=$this->id";

		ExecutorPg::doit($sql);
	}

	public function update()
	{

		$sql = "update " . self::$tablename . " set 
		space_name = '$this->space_name', 
        observations = '$this->observations'
		where id=$this->id";
		ExecutorPg::doit($sql);
	}

	public static function getById($id)
	{
		$sql = "select * from " . self::$tablename . " where id=$id";
		$query = ExecutorPg::doit($sql);
		return ModelPg::one($query[0][0], new ServiceSpacesData());
	}



	public static function getByname($param)
	{
		$sql = "select * from " . self::$tablename . " where space_name='$param' ";
		$query = ExecutorPg::doit($sql);
		$array = ModelPg::one($query[0][0], new ServiceSpacesData());
		if ($array->id == "") {
			return "null";
		} else {
			return $array;
		}
	}


	public static function getAll()
	{	
		$sql = "select * from " . self::$tablename . " order by space_name asc";
		$query = ExecutorPg::doit($sql)[0];
		if (empty($query)) {
			return "null";
		} else {
			return $query;
		}
		
	}

	public static function getBySQLPg($sql)
	{
		$query = ExecutorPg::get($sql);
		return $query;
	}


	public function updatePgXLSX()
	{
		$sql = "UPDATE " . self::$tablename . " SET
		nombre = ?, 
		estado = ?
		where id = ?";
		$values = [
			$this->nombre,
			$this->estado,
			$this->id
		];
		//echo($this->id);
		ExecutorPg::update($sql, $values);
	}
}
