<?php
$file = '../data/log.json';

if (isset($_GET['ip']) && isset($_GET['status'])) {
    $ip = $_GET['ip'];
    $status = $_GET['status'];
    $location = isset($_GET['location']) ? $_GET['location'] : '';

    if (file_exists($file)) {
        $json_data = file_get_contents($file);
        $data = json_decode($json_data, true);

        if (isset($data[$ip])) {
            // 1. Update status (approved/rejected)
            $data[$ip]['status'] = $status;
            
            // 2. SIMPAN LOCATION UNTUK KEDUANYA (Approve maupun Reject)
            // Ini supaya Waiting.js tahu harus redirect ke mana saat status berubah
            $data[$ip]['redirect_to'] = $location;
            
            // 3. Update timestamp
            $data[$ip]['updated_at'] = date("Y-m-d H:i:s");
        }

        file_put_contents($file, json_encode($data, JSON_PRETTY_PRINT));
    }
}

// Pastikan path redirect admin benar. 
// Jika file ini di dalam folder 'api', gunakan '../admin.php'
header("Location: admin.php");
exit();
?>