<?php
require '../config.php';
$dates = date('Y-m-d H:i:s');
if(isset($_POST['key']) || isset($_POST['sign']) || isset($_POST['sku']) || isset($_POST['userid']) || isset($_POST['order_id'])){
    $key = $_POST['key'];
    $sign = $_POST['sign'];
    $sku = $_POST['sku'];
    $userid = $_POST['userid'];
    $zoneid = $_POST['zoneid'];
    $order_id = $_POST['order_id'];
    $s_api = $db->query("SELECT * FROM users WHERE api_key = '$key'");
    if(mysqli_num_rows($s_api) == 0) die(json_encode(['result' => false,'message' => 'API Key not registered.']));
    $r_api = $s_api->fetch_assoc();
    $usernames = $r_api['username'];
    $phone = $r_api['phone'];
    $check_service = $db->query("SELECT * FROM layanan_pulsa WHERE service_id = '$sku' AND status = 'Normal'");
    if(mysqli_num_rows($check_service) > 0) {
        $pp = $check_service->fetch_assoc();
        $providers = $pp['provider'];
        $gamess = $pp['operator'];
        $skus = $pp['provider_id'];
        $l_id = $pp['id'];
        $services = $pp['layanan'];
        if($r_api['level'] == "Member"){
            $prices = $pp['harga'];
        }else if($r_api['level'] == 'Reseller'){
            $prices = $pp['harga_reseller'];
        }else if($r_api['level'] == 'Vip' || $r_api['level'] == 'Admin'){
            $prices = $pp['harga_premium'];
        }
            $check_provider = $db->query("SELECT * FROM provider WHERE code = '$providers'");
    if(mysqli_num_rows($check_provider) > 0) $pp_prv = $check_provider->fetch_assoc();
    if(check_ip($client_ip, $r_api['ip_static']) == false) die(json_encode(['result' => false,'message' => 'IP Anda '.$client_ip.' Tidak Dikenali']));
    if($sign <> md5($r_api['api_id'].$r_api['api_key'].$order_id)) die(json_encode(['result' => false,'message' => 'Signature Not Valid.']));
    if($usernames == false) die(json_encode(['result' => false,'message' => 'User not found.']));
    if(mysqli_num_rows($check_service) == 0) die(json_encode(['result' => false,'message' => 'Layanan tidak ditemukan atau saat ini tidak tersedia.']));
    if(mysqli_num_rows($check_provider) == 0) die(json_encode(['result' => false,'message' => 'Server tidak ditemukan atau saat ini tidak tersedia.']));
    $checkk = $db->query("SELECT * FROM pembelian_guest WHERE order_id = '$order_id'");
    $chekks = $checkk->fetch_assoc();
    if(mysqli_num_rows($checkk) !== 0) die(json_encode(['result' => true,'data' => ['order_id' => $order_id,'userid' => $userid,'zoneid' => $zoneid,'games' => $gamess,'layanan' => $services,'status' => $chekks['status'],'sn' => $chekks['sn'],'saldo' => (int)$r_api['saldo'],'price' => (int)$chekks['harga']],'message' => 'Data Berhasil Didapatkan']));
    if($r_api['saldo'] < $prices) die(json_encode(['result' => false,'message' => 'Saldo Anda tidak cukup untuk melakukan pemesanan ini.']));
    $inp =  $db->query("INSERT INTO `pembelian_guest` VALUES ('','$usernames','$phone','$order_id','-','15','Saldo Akun','-','$gamess','$l_id','$services','$skus','$userid', '$zoneid','','','','1','0','$prices','0','$prices','Waiting','$providers','Transaksi Pending','none','$dates','$dates','','-')"); 
    $inp1 = $db->query("UPDATE users SET saldo=saldo-$prices WHERE username='$usernames'");
    if($inp == true && $inp1 == true) {
        die(json_encode(['result' => true,'data' => [
            'order_id' => $order_id,
            'userid' => $userid,
            'zoneid' => $zoneid,
            'games' => $gamess,
            'layanan' => $services,
            'status' => 'Waiting',
            'sn' => 'Transaksi Pending',
            'saldo' => (int)$r_api['saldo'],
            'price' => (int)$prices
            ],
        'message' => 'Pesanan berhasil, pesanan akan diproses secepatnya.']));
    } else {
        //echo $gamess.' '.$l_id.' '.$providers.' '.$skus.' '.$order_id.' '.$phone.' '.$prices;
        die(json_encode(['result' => false,'message' => 'System Error Harap Menghubungi Admin'. $gamess.' '.$l_id.' '.$providers.' '.$skus.' '.$order_id.' '.$phone.' '.$prices.' '.$dates.' '.$services.' '.$userid.' '.$zoneid]));
    }
    }
}else{
    die(json_encode(['result' => false,'message' => 'Masih ada parameter yang kosong.']));
}