<?php

namespace App\Http\Controllers\Radius;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class Disable_User extends Controller
{
    public function disable(Request $request)
    {
        // dd($request->player_id);
        $params = [
            
        ];
        if(!isset($request->pin) || !isset($request->username)) {
            return response()->json(['status' => 400, 'message' => 'error parameter tidak sesuai'], 400);
        }
        //disableuser
        $disable = "http://172.27.0.5/radiusmanager/api/sysapi.php?apiuser=api&apipass=wifian2022&q=edit_user&username=$request->username&enabled=0";
        $params = json_encode($params);
        $ch = curl_init(); // Initialize cURL
        curl_setopt($ch, CURLOPT_URL, $disable);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
        // curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json',
            // 'Content-Length: ' . strlen($params),
            'accept:application/json'
        ));
        //pod
        $pod = "http://172.27.0.5/radiusmanager/api/sysapi.php?apiuser=api&apipass=wifian2022&q=send_pod&username=$request->username";
        $params = json_encode($params);
        $ch1 = curl_init(); // Initialize cURL
        curl_setopt($ch1, CURLOPT_URL, $pod);
        curl_setopt($ch1, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch1, CURLOPT_SSL_VERIFYHOST, 2);
        curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch1, CURLOPT_CUSTOMREQUEST, "GET");
        // curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
        curl_setopt($ch1, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json',
            // 'Content-Length: ' . strlen($params),
            'accept:application/json'
        ));
        //get data
        $getdata = "http://172.27.0.5/radiusmanager/api/sysapi.php?apiuser=api&apipass=wifian2022&q=get_userdata&username=$request->username";
        $params = json_encode($params);
        $ch2 = curl_init(); // Initialize cURL
        curl_setopt($ch2, CURLOPT_URL, $getdata);
        curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch2, CURLOPT_SSL_VERIFYHOST, 2);
        curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch2, CURLOPT_CUSTOMREQUEST, "GET");
        // curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
        curl_setopt($ch2, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json',
            // 'Content-Length: ' . strlen($params),
            'accept:application/json'
        ));
        
        if ($request->pin === '808081') {
            $statusdisable = curl_exec($ch);
            curl_close($ch);

            // return $statusdisable;
            if (preg_match('/0,""/', $statusdisable)){
                sleep(2);
                $data = curl_exec($ch2);
                curl_close($ch2);
                $sendpod = curl_exec($ch1);
                curl_close($ch1);
                // echo $data;
                $array = json_decode($data, true);
                $enableuser = $array[1]['enableuser'];
                if ($enableuser == 1) {
                $enableuser = "yes";
                } else {
                $enableuser = "no";
                }
                $srvid = $array[1]['srvid'];
                if ($srvid == 1) {
                    $srvid = "Helium 10Mbps";
                } else if ($srvid == 2) {
                    $srvid = "Helium 20Mbps";
                } else if ($srvid == 3) {
                    $srvid = "Helium 30Mbps";
                } else if ($srvid == 4) {
                    $srvid = "Helium 50Mbps";
                } else if ($srvid == 5) {
                    $srvid = "Helium 100Mbps";
                } else if ($srvid == 6) {
                    $srvid = "Helium 150Mbps";
                } else if ($srvid == 7) {
                    $srvid = "Helium 5Mbps";
                }else {
                    $srvid = $array[1]['srvid'];
                }
                $comment = $array[1]['comment'];
                $firstname = $array[1]['firstname'];
                $address = $array[1]['address'];
                return response()->json([
                    'status_code' => 200,
                    'Message' => "user $request->username berhasil di Disable",
                    'current data' => [
                        'username' => $request->username,
                        'enableuser' => $enableuser,
                        'service' => $srvid,
                        'firstname' => $firstname,
                        'address' => $address,
                        'comment' => $comment
                    ]
                ], 200);
                // return response()->json(['status' => 200,'Message' => "user $request->username berhasil didisable"], 200);
            } else if (preg_match('/User not found!/', $statusdisable)) {
            return response()->json(['status_code' => 400,'Message' => "user $request->username tidak ditemukan diradius"], 400);
            } else {    
                return response()->json(['status_code' => 304,'Message' => "user $request->username gagal di Disable"], 304);
            }

        }else {
            return response()->json(['status' => 401,'Message' => "Pin tidak sesuai"], 401);
        }
       
    }
}
