<?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 'vip.php';

		$database = new DataBase();
		$db = $database->getConnection();
     
        
        $ciudad = isset($_GET['ciudad'])?$_GET['ciudad']:0;
        $estado =isset($_GET['estado'])?$_GET['estado']:0;
        $busca =isset($_GET['busca'])?$_GET['busca']:0;
		$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"=>getCupones($db,$CategoriaId,$estado,$ciudad,$busca),
			        );
			        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 getCupones($db,$cat,$estado,$ciudad,$busca){
	  	$concepto = new Vip($db);
			$stmt = $concepto->select("select * from ".$concepto->table_name."   where tipo=3 and id_anuncio IN (select id from anuncios where id_estados=".$estado." and id_ciudades=".$ciudad." and 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.") and titulo LIKE '%".$busca."%' )");
		$num = $stmt->rowCount();

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