 <?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json");

require_once "koneksi.php";

$id_info = $_POST['id_info'] ?? 0;
$type = $_POST['type'] ?? '';

// Cek apakah sudah ada
$check = $conn->query("SELECT * FROM clicks WHERE id_info = $id_info");
if ($check->num_rows == 0) {
    $conn->query("INSERT INTO clicks (id_info, star, fire, thumbs) VALUES ($id_info, 0, 0, 0)");
}

$field = "";
if ($type == "star") $field = "star";
elseif ($type == "fire") $field = "fire";
elseif ($type == "thumbs") $field = "thumbs";

if ($field) {
    $conn->query("UPDATE clicks SET $field = $field + 1 WHERE id_info = $id_info");
    echo json_encode(["success" => true]);
} else {
    echo json_encode(["success" => false, "msg" => "Invalid type"]);
}

$conn->close();
?>