<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Kategori;
use App\Models\Layanan;


class VipResellerController extends Controller
{
    public function ordergame($uid = null, $zone = null, $service = null)
    {
        /*
        $api = \DB::table('setting_webs')->where('id',1)->first();
        $sign = md5($api->vip_apiid.$api->vip_apikey);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, 'https://vip-reseller.co.id/api/game-feature');
        curl_setopt($ch, CURLOPT_POST, TRUE);
        curl_setopt($ch, CURLOPT_POSTFIELDS, "key=".$api->vip_apikey."&sign=$sign&type=order&service=$service&data_no=$uid&data_zone=$zone");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
        $res = json_decode(curl_exec($ch), true);
        return $res;
        */

        $api = \DB::table('setting_webs')->where('id', 1)->first();
    $merchantCodes = $api->vip_apiid; // Assuming this is the correct variable
    $apiKey = $api->vip_apikey; // Assuming this is the correct variable
    $signe = $merchantCodes . $apiKey;
    $sign = md5($signe);
    
    $curl = curl_init();

    curl_setopt_array($curl, array(
        CURLOPT_URL => 'https://vip-reseller.co.id/api/game-feature',
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => '',
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 0,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => 'POST',
        CURLOPT_POSTFIELDS => array(
            'key' => $apiKey,
            'sign' => $sign,
            'type' => 'order',
            'service' => $service,
            'data_no' => $uid,
            'data_zone' => $zone
        ),
    ));

    //$response = curl_exec($curl);
    $response = json_decode(curl_exec($curl), true);
    curl_close($curl);
    
    return $response;

    
    }


    
    
    public function orderpulsa($uid = null, $service = null)
    {
        /*
        $api = \DB::table('setting_webs')->where('id',1)->first();
        $sign = md5($api->vip_apiid.$api->vip_apikey);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, 'https://vip-reseller.co.id/api/prepaid');
        curl_setopt($ch, CURLOPT_POST, TRUE);
        curl_setopt($ch, CURLOPT_POSTFIELDS, "key=".$api->vip_apikey."&sign=$sign&type=order&service=$service&data_no=$uid");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
        $res = json_decode(curl_exec($ch), true);
        return $res;*/
        $api = \DB::table('setting_webs')->where('id', 1)->first();
        $merchantCodes = $api->vip_apiid; // Assuming this is the correct variable
        $apiKey = $api->vip_apikey; // Assuming this is the correct variable
        $signe = $merchantCodes . $apiKey;
        $sign = md5($signe);
        
        $curl = curl_init();
    
        curl_setopt_array($curl, array(
            CURLOPT_URL => 'https://vip-reseller.co.id/api/prepaid',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_POSTFIELDS => array(
                'key' => $apiKey,
                'sign' => $sign,
                'type' => 'order',
                'service' => $service,
                'data_no' => $uid
                //'data_zone' => $zone
            ),
        ));
    
        //$response = curl_exec($curl);
        $response = json_decode(curl_exec($curl), true);
        curl_close($curl);
        
        return $response;
    }
    
    public function orderpasca($uid = null, $service = null)
    {
        /*
        $api = \DB::table('setting_webs')->where('id',1)->first();
        $sign = md5($api->vip_apiid.$api->vip_apikey);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, 'https://vip-reseller.co.id/api/postpaid');
        curl_setopt($ch, CURLOPT_POST, TRUE);
        curl_setopt($ch, CURLOPT_POSTFIELDS, "key=".$api->vip_apikey."&sign=$sign&type=pay-pasca&service=$service&data_no=$uid");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
        $res = json_decode(curl_exec($ch), true);
        return $res;
        */
        $api = \DB::table('setting_webs')->where('id', 1)->first();
        $merchantCodes = $api->vip_apiid; // Assuming this is the correct variable
        $apiKey = $api->vip_apikey; // Assuming this is the correct variable
        $signe = $merchantCodes . $apiKey;
        $sign = md5($signe);
        
        $curl = curl_init();
    
        curl_setopt_array($curl, array(
            CURLOPT_URL => 'https://vip-reseller.co.id/api/postpaid',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_POSTFIELDS => array(
                'key' => $apiKey,
                'sign' => $sign,
                'type' => 'order',
                'service' => $service,
                'data_no' => $uid
                //'data_zone' => $zone
            ),
        ));
    
        //$response = curl_exec($curl);
        $response = json_decode(curl_exec($curl), true);
        curl_close($curl);
        
        return $response;
    }

    public function statusgame($poid = null)
    {
        $api = \DB::table('setting_webs')->where('id',1)->first();
        $sign = md5($api->vip_apiid.$api->vip_apikey);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, 'https://vip-reseller.co.id/api/game-feature');
        curl_setopt($ch, CURLOPT_POST, TRUE);
        curl_setopt($ch, CURLOPT_POSTFIELDS, "key=".$api->vip_apikey."&sign=$sign&type=status&trxid=$poid");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
        $res = json_decode(curl_exec($ch), true);
        return $res;
    }
    
    public function statuspulsa($poid = null)
    {
        $api = \DB::table('setting_webs')->where('id',1)->first();
        $sign = md5($api->vip_apiid.$api->vip_apikey);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, 'https://vip-reseller.co.id/api/prepaid');
        curl_setopt($ch, CURLOPT_POST, TRUE);
        curl_setopt($ch, CURLOPT_POSTFIELDS, "key=".$api->vip_apikey."&sign=$sign&type=status&trxid=$poid");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
        $res = json_decode(curl_exec($ch), true);
        return $res;
    }
    
    public function statuspasca($poid = null)
    {
        $api = \DB::table('setting_webs')->where('id',1)->first();
        $sign = md5($api->vip_apiid.$api->vip_apikey);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, 'https://vip-reseller.co.id/api/postpaid');
        curl_setopt($ch, CURLOPT_POST, TRUE);
        curl_setopt($ch, CURLOPT_POSTFIELDS, "key=".$api->vip_apikey."&sign=$sign&type=status&trxid=$poid");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
        $res = json_decode(curl_exec($ch), true);
        return $res;
    }
    
    public function username($uid = null, $zone = null)
    {
        $api = \DB::table('setting_webs')->where('id',1)->first();
        $sign = md5($api->vip_apiid.$api->vip_apikey);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, 'https://vip-reseller.co.id/api/game-feature');
        curl_setopt($ch, CURLOPT_POST, TRUE);
        curl_setopt($ch, CURLOPT_POSTFIELDS, "key=".$api->vip_apikey."&sign=$sign&type=$get_nickname&code=$code&service=$service&data_no=$uid&data_zone=$zone");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
        $res = json_decode(curl_exec($ch), true);
        return $res;
    }


    public function store(Request $request)
    {
        // Validate the incoming request data
        $request->validate([
            'provider' => 'required|string',
            'profit' => 'required|numeric',
            'profit_member' => 'required|numeric',
            'profit_platinum' => 'required|numeric',
            'profit_gold' => 'required|numeric',
            // Add other validation rules as necessary
        ]);

        // Determine the service based on the provider
        switch ($request->provider) {
            case 'game':
                $serviceData = $this->servicegame();
                break;

            case 'pulsa':
                $serviceData = $this->servicepulsa();
                break;

            case 'pasca':
                $serviceData = $this->servicepasca();
                break;

            default:
                return response()->json(['error' => 'Invalid provider selected'], 400);
        }

      //  return $serviceData;
        
       // $serviceData = json_decode($serviceData);

        /*
// Cek apakah hasilnya benar
if ($serviceData['result']) {
    // Iterasi setiap item dalam data
    foreach ($serviceData['data'] as $item) {
        // Ambil kategori berdasarkan nama game dari item
        $idcat = Kategori::where('nama', $item['game'] ?? $item['brand'])->first();

        // Pastikan kategori ada dan status item adalah 'available'
        if ($idcat && $item['status'] === 'available') {
            // Ambil harga basic dari item
            $hargaBasic = $item['price']['basic'];

            // Buat instance Layanan baru
            $layanan = new Layanan();
            $layanan->kategori_id = $idcat->id;
            $layanan->layanan = $item['name']; // Nama layanan
            $layanan->provider_id = $item['code']; // ID provider
            $layanan->harga = $hargaBasic + ($hargaBasic * $request->profit / 100);
            $layanan->harga_member = $hargaBasic + ($hargaBasic * $request->profit_member / 100);
            $layanan->harga_platinum = $hargaBasic + ($hargaBasic * $request->profit_platinum / 100);
            $layanan->harga_gold = $hargaBasic + ($hargaBasic * $request->profit_gold / 100);
            $layanan->profit = $request->profit;
            $layanan->profit_member = $request->profit_member;
            $layanan->profit_platinum = $request->profit_platinum;
            $layanan->profit_gold = $request->profit_gold;
            $layanan->provider = 'VIP';
            $layanan->catatan = 'GENERATE BY HARY-IT';
            $layanan->status = 'available';
            $layanan->product_logo = 'https://cdns.mobi/upload/subimage.png'; 
            $layanan->harga_flash_salee = 0; 
            $layanan->is_flash_sale = 0; 
            $layanan->expired_flash_sale = now(); 
            $layanan->save();
        }
    }
}
    */
    \Log::info($serviceData);

    // Cek apakah hasilnya benar
if ($serviceData['result']) {
    // Iterasi setiap item dalam data
    foreach ($serviceData['data'] as $item) {
        // Ambil kategori berdasarkan nama game dari item
        $idcat = Kategori::where('nama', $item['game'] ?? $item['brand'])->first();

        // Pastikan kategori ada dan status item adalah 'available'
        if ($idcat && $item['status'] === 'available') {
            // Ambil harga basic dari item
            $hargaBasic = $item['price']['basic'];

            // Cek apakah layanan sudah ada di database
            $layanan = Layanan::where('provider_id', $item['code'])->first();

            if ($layanan) {
                // Jika sudah ada, update data
                $layanan->kategori_id = $idcat->id;
                $layanan->layanan = $item['name']; // Nama layanan
                $layanan->harga = $hargaBasic + ($hargaBasic * $request->profit / 100);
                $layanan->harga_member = $hargaBasic + ($hargaBasic * $request->profit_member / 100);
                $layanan->harga_platinum = $hargaBasic + ($hargaBasic * $request->profit_platinum / 100);
                $layanan->harga_gold = $hargaBasic + ($hargaBasic * $request->profit_gold / 100);
                $layanan->profit = $request->profit;
                $layanan->profit_member = $request->profit_member;
                $layanan->profit_platinum = $request->profit_platinum;
                $layanan->profit_gold = $request->profit_gold;
                $layanan->provider = 'VIP';
                $layanan->catatan = 'GENERATE BY HARY-IT';
                $layanan->status = 'available';
                $layanan->product_logo = 'https://cdns.mobi/upload/subimage.png'; 
                $layanan->harga_flash_sale = 0; 
                $layanan->is_flash_sale = 0; 
                $layanan->expired_flash_sale = now(); 
                $layanan->save();
            } else {
                // Jika belum ada, buat instance Layanan baru
                $layanan = new Layanan();
                $layanan->kategori_id = $idcat->id;
                $layanan->layanan = $item['name']; // Nama layanan
                $layanan->provider_id = $item['code']; // ID provider
                $layanan->harga = $hargaBasic + ($hargaBasic * $request->profit / 100);
                $layanan->harga_member = $hargaBasic + ($hargaBasic * $request->profit_member / 100);
                $layanan->harga_platinum = $hargaBasic + ($hargaBasic * $request->profit_platinum / 100);
                $layanan->harga_gold = $hargaBasic + ($hargaBasic * $request->profit_gold / 100);
                $layanan->profit = $request->profit;
                $layanan->profit_member = $request->profit_member;
                $layanan->profit_platinum = $request->profit_platinum;
                $layanan->profit_gold = $request->profit_gold;
                $layanan->provider = 'VIP';
                $layanan->catatan = 'GENERATE BY HARY-IT';
                $layanan->status = 'available';
                $layanan->product_logo = 'https://cdns.mobi/upload/subimage.png'; 
                $layanan->harga_flash_sale = 0; 
                $layanan->is_flash_sale = 0; 
                $layanan->expired_flash_sale = now(); 
                $layanan->save();
            }
        }
    }
}else{
    \Log::info($serviceData);
return back()->with('error', 'Terjadi Kesalahan Silakan Periksa Log Error!');

}

        // Simpan data di sini

// Mengarahkan kembali dengan pesan sukses
return back()->with('success', 'Data saved successfully!');
    }

    public function servicegamse()
    {
        $api = \DB::table('setting_webs')->where('id',1)->first();
        $sign = md5($api->vip_apiid.$api->vip_apikey);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, 'https://vip-reseller.co.id/api/game-feature');
        curl_setopt($ch, CURLOPT_POST, TRUE);
        curl_setopt($ch, CURLOPT_POSTFIELDS, "key=".$api->vip_apikey."&sign=$sign&type=services&filter_type=&filter_value=");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
        //$res = json_decode(curl_exec($ch), true);
        $res = curl_exec($ch);
        \Log::info(curl_exec($ch));
        return $res;
    }
    public function servicegame()
{
    $api = \DB::table('setting_webs')->where('id', 1)->first();
    $merchantCodes = $api->vip_apiid; // Assuming this is the correct variable
    $apiKey = $api->vip_apikey; // Assuming this is the correct variable
    $signe = $merchantCodes . $apiKey;
    $sign = md5($signe);
    
    $curl = curl_init();

    curl_setopt_array($curl, array(
        CURLOPT_URL => 'https://vip-reseller.co.id/api/game-feature',
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => '',
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 0,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => 'POST',
        CURLOPT_POSTFIELDS => array(
            'key' => $apiKey,
            'sign' => $sign,
            'type' => 'services'
        ),
    ));

    //$response = curl_exec($curl);
    $response = json_decode(curl_exec($curl), true);
    curl_close($curl);

    return $response;
}
    
    public function servicepulsa()
    {
        /*
        $api = \DB::table('setting_webs')->where('id',1)->first();
        $sign = md5($api->vip_apiid.$api->vip_apikey);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, 'https://vip-reseller.co.id/api/prepaid');
        curl_setopt($ch, CURLOPT_POST, TRUE);
        curl_setopt($ch, CURLOPT_POSTFIELDS, "key=".$api->vip_apikey."&sign=$sign&type=services&filter_type=&filter_value=");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
        $res = json_decode(curl_exec($ch), true);
        return $res;
        */
        $api = \DB::table('setting_webs')->where('id', 1)->first();
        $merchantCodes = $api->vip_apiid; // Assuming this is the correct variable
        $apiKey = $api->vip_apikey; // Assuming this is the correct variable
        $signe = $merchantCodes . $apiKey;
        $sign = md5($signe);
        
        $curl = curl_init();
    
        curl_setopt_array($curl, array(
            CURLOPT_URL => 'https://vip-reseller.co.id/api/prepaid',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_POSTFIELDS => array(
                'key' => $apiKey,
                'sign' => $sign,
                'type' => 'services'
            ),
        ));
    
        //$response = curl_exec($curl);
        $response = json_decode(curl_exec($curl), true);
        curl_close($curl);
    
        return $response;
    }
    
    public function servicepasca()
    {
        /*
        $api = \DB::table('setting_webs')->where('id',1)->first();
        $sign = md5($api->vip_apiid.$api->vip_apikey);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, 'https://vip-reseller.co.id/api/postpaid');
        curl_setopt($ch, CURLOPT_POST, TRUE);
        curl_setopt($ch, CURLOPT_POSTFIELDS, "key=".$api->vip_apikey."&sign=$sign&type=services&filter_type=&filter_value=");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
        $res = json_decode(curl_exec($ch), true);
        return $res;
        */
        $api = \DB::table('setting_webs')->where('id', 1)->first();
        $merchantCodes = $api->vip_apiid; // Assuming this is the correct variable
        $apiKey = $api->vip_apikey; // Assuming this is the correct variable
        $signe = $merchantCodes . $apiKey;
        $sign = md5($signe);
        
        $curl = curl_init();
    
        curl_setopt_array($curl, array(
            CURLOPT_URL => 'https://vip-reseller.co.id/api/postpaid',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_POSTFIELDS => array(
                'key' => $apiKey,
                'sign' => $sign,
                'type' => 'services'
            ),
        ));
    
        //$response = curl_exec($curl);
        $response = json_decode(curl_exec($curl), true);
        curl_close($curl);
    
        return $response;
    }
}
