<?php
		header("Access-Control-Allow-Origin: *");
		header("Access-Control-Allow-Headers: access");
		header("Access-Control-Allow-Methods: GET");
		header("Access-Control-Allow-Credentials: true");
		header("Content-Type: application/json; charset=UTF-8");

		include_once '../Database.php';
		include_once '../Categorias/categoria.php';
		include_once '../Cupones/cupones.php';
		include_once '../Comentario/comment.php';
		include_once 'anuncio.php';

		$database = new DataBase();
		$db = $database->getConnection();
     
        $ciudad = isset($_GET['ciudad'])?$_GET['ciudad']:0;
        $estado = isset($_GET['estado'])?$_GET['estado']:0;
        $search = isset($_GET['search'])?$_GET['search']:'';
        
		$concepto = new Categoria($db);
		try{
			$stmt = $concepto->select("select * from ".$concepto->table_name);
		$num = $stmt->rowCount();

		if($num>0){
		    $lista=array();
			while($row =$stmt->fetch(PDO::FETCH_ASSOC)){
			    extract($row);
			    $cat = array(
			        "id"=>$CategoriaId,
			        "nombre"=>$CategoriaNombre,
			        "icono"=>$icono,
			        "banner"=>$banner,
			        "color"=>$color,
			        "ads"=>getAds($db,$CategoriaId,$estado,$ciudad,$search),
			        );
				array_push($lista,$cat);
			}
			echo json_encode($lista);
		}
		else {
			echo json_encode(array("Mensaje"=>"No se han encontrado registros"));
		}
	}catch(Exeption $e){
		echo json_encode(array("Error"=>"Error ".$e));
	}
	
	function getAds($db,$cat,$estado,$ciudad,$search){
	    $concepto = new Anuncio($db);
			$stmt = $concepto->select("select a.*,f.thumbai from anuncios as a inner join sub_archivo as f on f.id_anuncio=a.id where a.id_estados=".$estado."  and a.id_ciudades=".$ciudad." and a.destacado=1 and f.tipo=1 and a.titulo LIKE '%".$search."%' and a.id IN (select s.id_anuncio from sub_giro as s inner join giros as g on s.id_giros=g.id where g.CategoriaId=".$cat.")");
		$num = $stmt->rowCount();
		if($num>0){
		    $lista=array();
			while($row =$stmt->fetch(PDO::FETCH_ASSOC)){
			    extract($row);
			    $ands=array(
			        "id"=>$id,
			        "id_estados"=>$id_estados,
			        "id_ciudades"=>$id_ciudades,
			        "titulo"=>$titulo,
			        "descripcion"=>$descripcion,
			        "telefono"=>$telefono,
			        "movil"=>$movil,
			        "fax"=>$fax,
			        "nextel"=>$nextel,
			        "email"=>$email,
			        "direccion"=>$direccion,
			        "url"=>$url,
			        "cupon"=>$cupon,
			         "vip"=>$vip,
			         "longitud"=>$longitud,
			         "latitud"=>$latitud,
			         "contrato"=>$contrato,
			         "destacado"=>$destacado,
			         "rating"=>$rating,
			         "cupones"=>getCupon($db,$id),
			         "vips"=>getVip($db,$id),
			         "thumbai"=>$thumbai,
			         "comments"=>getComment($db,$id),
			         
			        );
				array_push($lista,$ands);
			}
			return $lista;
		}
		else {
			return null;
		}
	
	
	}
	
	function getCupon($db,$ads){
	    $concepto = new Cupon($db);
		
			$stmt = $concepto->select("select * from ".$concepto->table_name."   where tipo=2 and id_anuncio=".$ads);
		$num = $stmt->rowCount();

		if($num>0){
		    $lista=array();
			while($row =$stmt->fetch(PDO::FETCH_ASSOC)){
				array_push($lista,$row);
			}
			return $lista;
		}
		else {
			return null;
		}
	
	}
	function getComment($db,$ads){
	    $concepto = new Comment($db);
		
			$stmt = $concepto->select("select * from ".$concepto->table_name."   where anunciosId=".$ads);
		$num = $stmt->rowCount();

		if($num>0){
		    $lista=array();
			while($row =$stmt->fetch(PDO::FETCH_ASSOC)){
				array_push($lista,$row);
			}
			return $lista;
		}
		else {
			return null;
		}
	
	}
	function getVip($db,$ads){
	    $concepto = new Cupon($db);
		
			$stmt = $concepto->select("select * from ".$concepto->table_name."   where tipo=3 and id_anuncio=".$ads);
		$num = $stmt->rowCount();

		if($num>0){
		    $lista=array();
			while($row =$stmt->fetch(PDO::FETCH_ASSOC)){
				array_push($lista,$row);
			}
			return $lista;
		}
		else {
			return null;
		}
	
	}
		
?>
