<?php
// khfy_proxy.php - FINAL FIX
// Mematikan display error agar tidak merusak format JSON
ini_set('display_errors', 0);
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');

$apiKey = "8F1199C1-483A-4C96-825E-F5EBD33AC60A";
$baseUrl = "https://panel.khfy-store.com/api_v2";

$action = $_GET['action'] ?? '';

function callAPI($url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $result = curl_exec($ch);
    if (curl_errno($ch)) {
        return json_encode(['status' => false, 'message' => 'Curl Error: ' . curl_error($ch)]);
    }
    curl_close($ch);
    return $result;
}

if ($action == 'list') {
    echo callAPI("$baseUrl/list_product?api_key=$apiKey");
} 
elseif ($action == 'trx') {
    $p = $_GET['produk'] ?? ''; 
    $t = $_GET['tujuan'] ?? ''; 
    $r = $_GET['reff_id'] ?? '';
    
    if(!$p || !$t) {
        echo json_encode(['status' => false, 'message' => 'Data tidak lengkap']);
        exit;
    }
    echo callAPI("$baseUrl/trx?produk=$p&tujuan=$t&reff_id=$r&api_key=$apiKey");
} 
elseif ($action == 'status') {
    // --- BAGIAN INI YANG SEBELUMNYA HILANG ---
    $r = $_GET['refid'] ?? '';
    echo callAPI("$baseUrl/history?api_key=$apiKey&refid=$r");
} 
else {
    echo json_encode(['status' => false, 'message' => 'Action invalid. Use: list, trx, or status']);
}
?>