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

		$database = new DataBase();
		$db = $database->getConnection();
     
        $id = isset($_GET['user'])?$_GET['user']:0;
		$concepto = new Favorito($db);
		try{
		echo getFavoritos($db,$id);
	}catch(Exeption $e){
		echo json_encode(array("Error"=>"Error ".$e));
	}
	
	function getFavoritos($db,$user){
	    $fav = new Favorito($db);
	    $stmt=$fav->select("select a.*,f.thumbai from anuncios as a inner join sub_archivo as f on a.id=f.id_anuncio where a.id IN (select ads from fav where user=".$user.")");
	    $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,
			         "thumbai"=>$thumbai,
			        );
				array_push($lista,$ands);
			}
			echo json_encode($lista);
	    }else{
	        echo json_encode(array("error"=>true,"Mensaje"=>"Sin datos para mostrar"));
	    }
	}
		
?>
