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

// ==========================================
// 1. KREDENSIAL BARU (Untuk Transaksi & Status)
// ==========================================
$kodereseller = "NF00514";
$pin = "180979";
$password = "reyy35";
$baseUrlNew = "http://213.163.206.110:3333/api";

// ==========================================
// 2. KREDENSIAL LAMA (Khusus untuk List Produk)
// ==========================================
$apiKeyOld = "8F1199C1-483A-4C96-825E-F5EBD33AC60A";
$baseUrlOld = "https://panel.khfy-store.com/api_v2";

$action = $_GET['action'] ?? '';
$currentTime = date('His');

// Fungsi Fleksibel: Bisa GET (lama) dan POST JSON (baru)
function callAPI($url, $payload = null) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    
    // Jika ada payload, gunakan metode POST JSON
    if ($payload !== null) {
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
    }
    
    $result = curl_exec($ch);
    if (curl_errno($ch)) {
        return json_encode(['status' => false, 'message' => 'Curl Error: ' . curl_error($ch)]);
    }
    curl_close($ch);
    return $result;
}

// ==========================================
// ROUTING AKSI
// ==========================================
if ($action == 'list') {
    // Aksi: List Produk -> Pakai API Lama (GET)
    echo callAPI("$baseUrlOld/list_product?api_key=$apiKeyOld");
} 
elseif ($action == 'trx') {
    // Aksi: Transaksi -> Pakai API Baru (POST JSON)
    $p = $_GET['produk'] ?? ''; 
    $t = $_GET['tujuan'] ?? ''; 
    $r = $_GET['reff_id'] ?? '';
    
    if(!$p || !$t || !$r) {
        echo json_encode(['status' => false, 'message' => 'Data tidak lengkap']);
        exit;
    }
    
    $payload = [
        "req" => "topup",
        "kodereseller" => $kodereseller,
        "produk" => $p,
        "msisdn" => $t,
        "reffid" => $r,
        "time" => $currentTime,
        "pin" => $pin,
        "password" => $password
    ];
    
    echo callAPI($baseUrlNew, $payload);
} 
elseif ($action == 'status') {
    // Aksi: Cek Status -> Pakai API Baru (POST JSON)
    $r = $_GET['refid'] ?? '';
    
    if(!$r) {
        echo json_encode(['status' => false, 'message' => 'Data tidak lengkap (refid kosong)']);
        exit;
    }

    $payload = [
        "req" => "cmd",
        "kodereseller" => $kodereseller,
        "perintah" => "CEK." . $r,
        "time" => $currentTime,
        "pin" => $pin,
        "password" => $password
    ];

    echo callAPI($baseUrlNew, $payload);
} 
else {
    // Jika aksi tidak dikenali
    echo json_encode(['status' => false, 'message' => 'Action invalid. Use: list, trx, or status']);
}
?>