<?php
require_once "conexion.php";

class ModeloAccion {

    static public function mdlMostrarAccion($tabla, $item , $valor){

        if ($item != null) {
            
            $stmt = Conexion::conectar()-> prepare("SELECT * FROM $tabla WHERE $item =:$item");

            $stmt -> bindParam(":".$item, $valor, PDO::PARAM_STR );

             $stmt -> execute();

            return $stmt -> fetch();

        }else {

            $stmt = Conexion:: conectar()->prepare("SELECT * FROM $tabla WHERE activo = 1");
            
            $stmt -> execute();

            return $stmt -> fetchAll();
        

        }
    }

    
    static public function mdlMostrarAccion1($tabla, $item , $valor){

        if ($item != null) {
            
            $stmt = Conexion::conectar()-> prepare("SELECT * FROM $tabla WHERE $item =:$item");

            $stmt -> bindParam(":".$item, $valor, PDO::PARAM_STR );

             $stmt -> execute();

            return $stmt -> fetch();

        }else {

            $stmt = Conexion:: conectar()->prepare("SELECT * FROM $tabla ");
            
            $stmt -> execute();

            return $stmt -> fetchAll();
        

        }
    }

    static public function mdlCrearAccion($tabla, $datos){
        try {

            $stmt = Conexion::conectar()-> prepare("INSERT INTO $tabla(nombre) VALUES(:nombre)");
            $stmt -> bindParam(":nombre", $datos["nombre"], PDO::PARAM_STR);
            if ($stmt -> execute()) {
                return "ok";
            }else {
                return "error";
            }
            
        } catch (PDOException $e) {

        error_log("Error en mdlIngresarAccion: " . $e->getMessage());
        return "error";
        } finally {
            $stmt = null; // liberar recursos
        }
    }

        static public function mdlEditarAccion($tabla, $datos){
        try {

            $stmt = Conexion::conectar()->prepare("UPDATE $tabla SET nombre = :nombre WHERE id = :id");

            $stmt -> bindParam(":id", $datos["id"], PDO::PARAM_INT);
            $stmt -> bindParam(":nombre", $datos["nombre"], PDO::PARAM_STR);


            if($stmt -> execute()){

                return "ok";
                
            
            }else{

                return "error";	

            }

		} catch (PDOException $e) {
        // Log para depuración
        error_log("Error en mdlCrearAccion: " . $e->getMessage());
        return "error";
        } finally {
            $stmt = null; // liberar recursos
        }

    }

     /*=============================================
	BORRAR Modulo
	=============================================*/

	static public function mdlBorrarAccion($tabla, $datos){

        try {

            $stmt = Conexion::conectar()->prepare("DELETE FROM $tabla WHERE id = :id");

            $stmt -> bindParam(":id", $datos, PDO::PARAM_INT);

            if($stmt -> execute()){

                return "ok";
            
            }else{

                return "error";	

            }

		} catch (PDOException $e) {
        // Log para depuración
        error_log("Error en mdlBorrarAccion: " . $e->getMessage());
        return "error";
        } finally {
            $stmt = null; // liberar recursos
        }


	}

    static public function mdlActualizarAccion($tabla, $item1, $valor1, $item2, $valor2){

		$stmt = Conexion::conectar()->prepare("UPDATE $tabla SET $item1 = :$item1 WHERE $item2 = :$item2");

		$stmt -> bindParam(":".$item1, $valor1, PDO::PARAM_STR);
		$stmt -> bindParam(":".$item2, $valor2, PDO::PARAM_STR);

		if($stmt -> execute()){

			return "ok";
		
		}else{

			return "error";	

		}

		$stmt -> close();

		$stmt = null;

	}
}