<?php

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

require_once("../config/db.php");

$data = json_decode(file_get_contents("php://input"), true);

$visitor_id = intval($data['visitor_id'] ?? 0);
$village_id = intval($data['village_id'] ?? 0);
$checked_by = intval($data['checked_by'] ?? 0);

if (!$visitor_id || !$village_id) {
    echo json_encode([
        "status" => false,
        "message" => "Missing data"
    ]);
    exit;
}

$query = "INSERT INTO visitor_logs 
(visitor_id, village_id, checked_by, action) 
VALUES 
('$visitor_id', '$village_id', '$checked_by', 'visited')";

if ($conn->query($query)) {
    echo json_encode([
        "status" => true,
        "message" => "Visit recorded"
    ]);
} else {
    echo json_encode([
        "status" => false,
        "message" => "Database error"
    ]);
}