<?php 

require_once "conexion.php";

class ModeloUsuarios {

    static public function MdlMostrarUsuarios($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();
        }

        $stmt -> close();

        $stmt = null;
    }

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

        if($item == null){

            $stmt = Conexion:: conectar()->prepare("SELECT * FROM $tabla WHERE tipo_usuario = 'Vendedor' AND estado= 1  ");

         
            
            $stmt -> execute();

            return $stmt -> fetchAll();

        }else {

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

            return $stmt -> fetchAll();
        }

        $stmt -> close();

        $stmt = null;
    }

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

        if($item == null){

            $stmt = Conexion:: conectar()->prepare("SELECT * FROM $tabla WHERE tipo_usuario = 'Administrador' AND estado= 1  ");

         
            
            $stmt -> execute();

            return $stmt -> fetchAll();

        }else {

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

            return $stmt -> fetchAll();
        }

        $stmt -> close();

        $stmt = null;
    }


    static public function MdlMostrarUsuarios3($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 estado = 1 ");
            
            $stmt -> execute();

            return $stmt -> fetchAll();
        }

        $stmt -> close();

        $stmt = null;
    }

    static public function MdlMostrarUsuarios2($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 u.*, e.nombre FROM $tabla u INNER JOIN empleado e ON u.id_empleado = e.id WHERE u.estado=1");
            
            $stmt -> execute();

            return $stmt -> fetchAll();
        }

        $stmt -> close();

        $stmt = null;
    }

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

        $item1 = "estado";
        $valor1 = "1";

        if($item != null){

            $stmt = Conexion:: conectar()->prepare("SELECT * FROM $tabla WHERE $item = :$item AND $item1 = :$item1");

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

            return $stmt -> fetchAll();

        }else {

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

            return $stmt -> fetchAll();
        }

        $stmt -> close();

        $stmt = null;
    }

    /** Registro de usuarios */

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

        $stmt = Conexion:: conectar()->prepare("INSERT INTO $tabla(id_empleado, usuario, password, sucursal_id, id_area, foto, estado) VALUES (:id_empleado, :usuario, :password, :sucursal_id, :id_area, :foto, :estado)");
        
        $stmt -> bindParam(":id_empleado", $datos["id_empleado"], PDO::PARAM_INT);
        $stmt -> bindParam(":usuario", $datos["usuario"], PDO::PARAM_STR);
        $stmt -> bindParam(":password", $datos["password"], PDO::PARAM_STR);
        $stmt -> bindParam(":sucursal_id", $datos["sucursal_id"], PDO::PARAM_STR);
        $stmt -> bindParam(":id_area", $datos["id_area"], PDO::PARAM_STR);
        $stmt -> bindParam(":estado", $datos["estado"], PDO::PARAM_INT);
        $stmt -> bindParam(":foto", $datos["foto"], PDO::PARAM_STR);

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

        $stmt = null;

        
    }
/*=============================================
	EDITAR USUARIO
	=============================================*/
    static public function mdlEditarUsuario($tabla, $datos){

        $stmt = Conexion::conectar()->prepare("UPDATE $tabla SET password = :password, id_area = :id_area, foto = :foto WHERE usuario = :usuario");

		$stmt -> bindParam(":password", $datos["password"], PDO::PARAM_STR);
		$stmt -> bindParam(":id_area", $datos["id_area"], PDO::PARAM_STR);
		$stmt -> bindParam(":foto", $datos["foto"], PDO::PARAM_STR);
		$stmt -> bindParam(":usuario", $datos["usuario"], PDO::PARAM_STR);

		if($stmt -> execute()){

			return "ok";
            
		
		}else{

			return "error";	

		}

		$stmt -> close();

		$stmt = null;

    }
    
	/*=============================================
	ACTUALIZAR USUARIO
	=============================================*/

	static public function mdlActualizarUsuario($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;

	}
    /*=============================================
	BORRAR USUARIO
	=============================================*/

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

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

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

		if($stmt -> execute()){

			return "ok";
		
		}else{

			return "error";	

		}

		$stmt -> close();

		$stmt = null;


	}

}