<?php

namespace App\Models;

use CodeIgniter\Model;
use App\Libraries\Componente;

class General extends Model
{
	// protected $session;
	// public function __construct(){
	// $this->session = \Config\Services::session();
	// }
	public static function getData($campos, $tablaPrincipal, $where, $order)
	{
		$db = \Config\Database::connect();
		$builder = $db->table($tablaPrincipal);
		$builder->select($campos);
		$builder->where($where);
		$builder->orderBy($order);
		$query = $builder->get();
		return $query->getResult();
	}
	public static function getData2($campos, $tablaPrincipal, $tablasSecundarias, $where, $order)
	{
		$db = \Config\Database::connect();
		$builder = $db->table($tablaPrincipal);
		$builder->select($campos);
		if ($tablasSecundarias != false) {
			$builder->from($tablasSecundarias);
		}
		foreach ($where as $w) {
			$builder->where($w);
		}
		$builder->orderBy($order);
		$query = $builder->get();
		return $query->getResult();
	}
	public static function actualizar($tabla, $where, $data)
	{
		$db = \Config\Database::connect();
		$builder = $db->table($tabla);
		$builder->where($where);
		$builder->update($data);
		return $db->affectedRows();
	}
	public static function insertar($tabla, $data)
	{
		$db = \Config\Database::connect();
		$builder = $db->table($tabla);
		$builder->insert($data);
		return $db->affectedRows();
	}
	public static function eliminar($tabla, $where)
	{
		$db = \Config\Database::connect();
		$builder = $db->table($tabla);
		$builder->delete($where);
		return $db->affectedRows();
	}
	public static function ultimoId()
	{
		$db = \Config\Database::connect();
		$sql = "
			SELECT
                last_insert_id() as id
        ";
		$query = $db->query($sql);
		return $query->getResult();
	}
}
