<?php
require '../../config.php';
    $CEK = $db->query("SELECT * FROM provider WHERE code='VR1'");
    $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();
    $VRL = 'https://vip-reseller.co.id/api/prepaid';
    $results = $curl->connectPost($VR['link'].'prepaid',['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['brand'];
            $type = $loop['type'];
            $sku = $loop['code'];
            $note = $loop['note'];
            $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;
            
            $basic = '657';
            $reseller = '500';
            $vip = '300';
            $h2h = '150';
            
            $price_member = $price + $basic;
            $price_reseller = $price + $reseller;
            $price_premium = $price + $vip;
            $price_h2hspecial = $price + $h2h;
            if($status == 'available'){
                $statuss = 'Normal';
            }else if($status == 'empty'){
                $statuss = 'Gangguan';
            }
            
             if ($type == 'paket-internet') {
                $tipee = 'DATA';
            } else if ($type == 'pulsa-reguler') {
                $tipee = 'PULSA';
            } else if ($type == 'saldo-emoney') {
                $tipee = 'EMONEY';
            } else if ($type == 'voucher-game') {
                $tipee = 'VGAME';
            } else if ($type == 'token-pln') {
                $tipee = 'DATA';
            } else if ($type == 'voucher-game') {
                $tipee = 'VGAME';
            } else if ($type == 'paket-telepon') {
                $tipee = 'SMSDANTELPON';
            } 
            $pp = strtoupper($sku).'-S1';
            $check_dataa = $db->query("SELECT * FROM layanan_pulsa WHERE operator='$operator' AND provider_id='$sku' AND tipe='$tipee' AND provider='VR1'");
            if ($check_dataa->num_rows == 0) {
                $db->query("INSERT INTO `layanan_pulsa` VALUES ('', '$pp', '$sku', '$operator', '$product', 'null','null','null','$price_member', '$price_reseller', '$price_premium', '$price_h2hspecial', '0', '$statuss', 'VR1', '$tipee', '$note')");
                $db->query("DELETE FROM layanan_pulsa WHERE status='Gangguan'");
            }else{
                $db->query("UPDATE layanan_pulsa SET status='$statuss',harga='$price_member',service_id='$pp', harga_reseller='$price_reseller', harga_premium='$price_premium' WHERE operator='$operator' AND provider_id ='$sku' AND tipe='$tipee' AND provider='VR1'");
                $db->query("DELETE FROM layanan_pulsa WHERE status='Gangguan'");
            }
        }
    }
if (isset($_GET['redirect'])) {
    header('location:/admin/setting/provider');
}