<?php 
header("Access-Control-Allow-Origin: *");
header("Content-Type:application/json; charset=UTF-8");
header("Access-Control-Allow-Methods:POST");
header("Access-Control-Max-Age:3600");
header("Access-Control-Allow-Headers:Content-Type,Access-Control-Allow-Headers,Authorization,X-Requested-With");

include_once '../Database.php';
include_once 'favorito.php';

$database = new DataBase();
$db = $database->getConnection();

$usuario = new Favorito($db);
try{
	$data = json_decode(file_get_contents("php://input"));
	if(!isset($data)){
	    return json_encode(array("Error"=>true,"Mensaje"=>"Los  valores no son validos"));
	}
	//print_r($data);
	
	$user = isset($data->user)?$data->user:null;
	$ads = isset($data->ads)?$data->ads:null;
	
	$query = "INSERT INTO ".$usuario->table_name." SET ads=:ads, user=:user";
	$stm = $usuario->conn->prepare($query);
	$stm->bindParam(":ads",$ads);
	$stm->bindParam(":user",$user);
	
	$stmt = $usuario->Insert($stm);
	if($stmt !==false){
		echo json_encode(array("Error"=>false,"Id"=>$stmt));
	}else{
		echo json_encode(array("Error"=>true,"Mensaje"=>"No se ha creado el registro","datos"=>$data));
	}
}catch(Exception $ex){
	echo json_encode(array("Error"=>true,"Mensaje"=>"No se ha podido procesar la solicitud"));
}


?>