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

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

$visitor_id = intval($data['visitor_id']);
$village_id = intval($data['village_id']);
$user_id    = intval($data['user_id']);

// check if already visited
$check = $conn->query("SELECT id FROM visitor_visits 
WHERE visitor_id='$visitor_id' AND village_id='$village_id'");

$type = ($check->num_rows > 0) ? 'repeat' : 'new';

$conn->query("INSERT INTO visitor_visits 
(visitor_id, village_id, visited_by, visit_type) 
VALUES ('$visitor_id', '$village_id', '$user_id', '$type')");

echo json_encode([
  "status" => true,
  "type" => $type
]);