<?php
require_once "conexion.php";


class ModeloClientes {

    static public function MdlMostrarClientes($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 = 0");
            
            $stmt -> execute();

            return $stmt -> fetchAll();
        }

        $stmt -> close();

        $stmt = null;
    }




     /** Registro de clientes */

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

        $stmt = Conexion:: conectar()->prepare("INSERT INTO $tabla(ruc_dni, razon_social, email, telefono, direccion) VALUES (:ruc_dni, :razon_social, :email, :telefono, :direccion)");
        
        $stmt -> bindParam(":ruc_dni", $datos["ruc_dni"], PDO::PARAM_STR);
        $stmt -> bindParam(":razon_social", $datos["razon_social"], PDO::PARAM_STR);
        $stmt -> bindParam(":email", $datos["email"], PDO::PARAM_STR);
        $stmt -> bindParam(":telefono", $datos["telefono"], PDO::PARAM_STR);
        $stmt -> bindParam(":direccion", $datos["direccion"], PDO::PARAM_STR);

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

        $stmt = null;

        
    }

    /** Editar de clientes */

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

        $stmt = Conexion:: conectar()->prepare("UPDATE $tabla SET ruc_dni = :ruc_dni, razon_social = :razon_social, email = :email, telefono = :telefono, direccion = :direccion, ultima_compra = :ultima_compra WHERE id = :id");
        
        $stmt -> bindParam(":id", $datos["id"], PDO::PARAM_STR);
        $stmt -> bindParam(":ruc_dni", $datos["ruc_dni"], PDO::PARAM_STR);
        $stmt -> bindParam(":razon_social", $datos["razon_social"], PDO::PARAM_STR);
        $stmt -> bindParam(":email", $datos["email"], PDO::PARAM_STR);
        $stmt -> bindParam(":telefono", $datos["telefono"], PDO::PARAM_STR);
        $stmt -> bindParam(":direccion", $datos["direccion"], PDO::PARAM_STR);
        $stmt -> bindParam(":ultima_compra", $datos["ultima_compra"], PDO::PARAM_STR);

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

        $stmt = null;

        
    }

        /*=============================================
	BORRAR MARCA
	=============================================*/

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

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

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

		if($stmt -> execute()){

			return "ok";
		
		}else{

			return "error";	

		}

		$stmt -> close();

		$stmt = null;


	}
    /*=============================================
	ACTUALIZAR CLIENTE
	=============================================*/

	static public function mdlActualizarCliente($tabla, $item1, $valor1, $valor){

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

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

		if($stmt -> execute()){

			return "ok";
		
		}else{

			return "error";	

		}

		$stmt -> close();

		$stmt = null;

	}

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

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

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

		if($stmt -> execute()){

			return "ok";
		
		}else{

			return "error";	

		}

		$stmt -> close();

		$stmt = null;

	}

    static public function mdlRutaClientes($tabla, $ruta){

        if ( $ruta == null) {

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

			return $stmt -> fetchAll();	
  
        } else {

            $stmt = Conexion:: conectar()->prepare("SELECT * FROM $tabla WHERE ruta = '$ruta' AND alias = 0 ");
            $stmt -> execute();

			return $stmt -> fetchAll();	
        }

        $stmt -> close();

		$stmt = null;

    }
    
}