<?php

require_once "conexion.php";

class ModeloArea{

	/*=============================================
	MOSTRAR PRODUCTOS DEL ALMACEN 1
	=============================================*/

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

		if($item != null){

			$stmt = Conexion::conectar()->prepare("SELECT * FROM $tabla WHERE $item = :$item ORDER BY id ASC");

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

			$stmt -> execute();

			return $stmt -> fetch();

		}else{
			

			$stmt = Conexion::conectar()->prepare("SELECT * FROM $tabla ORDER BY id ASC ");

			$stmt -> execute();

			return $stmt -> fetchAll();

		}
		
		$stmt -> close();

		$stmt = null;

	}

	/*=============================================
	MOSTRAR AREA
	=============================================*/

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

		if($item != null){

			$stmt = Conexion::conectar()->prepare("SELECT * FROM $tabla WHERE $item = :$item ORDER BY id ASC");

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

			$stmt -> execute();

			return $stmt -> fetch();

		}else{
			

			$stmt = Conexion::conectar()->prepare("SELECT * FROM $tabla WHERE estado = 1 ORDER BY id ASC ");

			$stmt -> execute();

			return $stmt -> fetchAll();

		}
		
		$stmt -> close();

		$stmt = null;

	}

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

		$stmt = Conexion::conectar();

		$sentencia=$stmt ->prepare("INSERT INTO $tabla( productos, estado) VALUES ( :productos, :estado)");

		$sentencia->bindParam(":productos", $datos["productos"], PDO::PARAM_STR);
		$sentencia->bindParam(":estado", $datos["estado"], PDO::PARAM_STR);

		if($sentencia->execute()){

			return "ok";

		}else{

			return "error";
		
		}

		$sentencia->close();
		$sentencia = null;

	}

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

        $stmt = Conexion::conectar()->prepare("INSERT INTO $tabla(nombre, estado) VALUES (:nombre, :estado)");

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

        if($stmt -> execute()){
            return "ok";
        }else{
            return "error";
        }
        
        $stmt -> close();

        $stmt = null;
    }

	 /** EDITAR AREA */

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

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

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

        if($stmt -> execute()){
            return "ok";
        }else{
            return "error";
        }
        
        $stmt -> close();

        $stmt = null;
    }

	  /*=============================================
	BORRAR AREA
	=============================================*/

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

		$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";	

		}

		$stmt -> close();

		$stmt = null;


	}



}