<?php
require '../../config.php';
    $CEK = $db->query("SELECT * FROM provider WHERE code='VR'");
    $VR = $CEK->fetch_assoc();
    $hmem = $db->query("SELECT * FROM options WHERE opt_name='df_harga_member'");
    $hmems = $hmem->fetch_assoc();
    $hres = $db->query("SELECT * FROM options WHERE opt_name='df_harga_reseller'");
    $hress = $hres->fetch_assoc();
    $hpre = $db->query("SELECT * FROM options WHERE opt_name='df_harga_premium'");
    $hprem = $hpre->fetch_assoc();
    $h2h = $db->query("SELECT * FROM options WHERE opt_name='df_harga_h2hspecial'");
    $h2hh = $h2h->fetch_assoc();
    $results = $curl->connectPost($VR['link'].'game-feature',['key' => $VR['api_key'],'sign' => md5($VR['api_id'].$VR['api_key']),'type' => 'service']);
    if(isset($results['data'])){
        foreach($results['data'] as $loop){
            $product = $loop['name'];
            $price = $loop['price']['special'];
            $operator = $loop['game'];
            $sku = $loop['code'];
            $status = $loop['status'];
            $pmember = $price * $hmems['opt_value'] / 100;
            $preseller = $price * $hress['opt_value'] / 100;
            $ppremium = $price * $hprem['opt_value'] / 100;
            $ph2hspecial = $price * $h2hh['opt_value'] / 100;
            $price_member = $price + $pmember;
            $price_reseller = $price + $preseller;
            $price_premium = $price + $ppremium;
            $price_h2hspecial = $price + $ph2hspecial;
            if($status == 'available'){
                $statuss = 'Normal';
            }else if($status == 'empty'){
                $statuss = 'Gangguan';
            }
            if($operator == "Mobile Legends B"){
                $p = explode('_',$sku);
                $pp = $p[0];
            }else{
                $p = explode('-',$sku);
                $pp = $p[0];
            }
            $ppp = $pp.'-S1';
            $check_data = $db->query("SELECT * FROM layanan_pulsa WHERE operator='$operator' AND provider_id='$sku' AND tipe='VGAME' AND provider='VR'");
            if(mysqli_num_rows($check_data) == 0){
                $db->query("INSERT INTO `layanan_pulsa` VALUES ('', '{$ppp}', '$sku', '$operator', '$product', 'null','0','0','$price_member', '$price_reseller', '$price_premium', '$price_h2hspecial', '0', '$statuss', 'VR', 'VGAME', '-')");
                $db->query("DELETE FROM layanan_pulsa WHERE status='Gangguan'");
            }else{
                $db->query("UPDATE layanan_pulsa SET status='$statuss', harga='$price_member', service_id='{$ppp}', harga_reseller='$price_reseller', harga_premium='$price_premium' WHERE operator='$operator' AND provider_id='$sku' AND provider='VR' AND tipe='VGAME'");
                $db->query("DELETE FROM layanan_pulsa WHERE status='Gangguan'");
            }
        }
    }
if (isset($_GET['redirect'])) {
    header('location: /admin/setting/provider');
}