<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models;
use Session;

class VpnController extends Controller{
    public function indexBK(){
        $result = Models\VpnConfig::where('partner_type', 'mno')->with('mno_info')->paginate(15);
        // dd($result);
        $data = [
            'page_title' => 'infrastructure: VPN', 
            'vpn_list' => $result
        ];
        // dd($result);
        return view('vpn.index', $data);
    }
    public function indexBKK(){
        $result = Models\VpnConfig::get();
        // dd($result);
        $data = [
            'page_title' => 'infrastructure: VPN', 
            'vpn_list' => $result
        ];
        return view('vpn.index_test', $data);
    }
     public function index(){
        $data = [
            'page_title' => 'Infrastructure: VPN',
            'current_user' => session('current_user')
        ];
        return view('vpn.index_rawjs', $data);
    }
    public function getVpnListJsonRawJs(Request $request){
       // dd($request->has('keyword') && $request->keyword == true);
        
        if($request->has('keyword') && $request->keyword == true){
            //'vpn_configs.client_id',
            $keyword = $request->keyword;
            $vpn_list_arr = \DB::table('vpn_configs')
           ->join('auth_users', 'auth_users.id', '=', 'vpn_configs.user_id')
            ->join('network_operators', 'network_operators.id', '=', 'vpn_configs.mno_id')
            ->select('network_operators.id', 'vpn_configs.date_received', 'network_operators.name AS mnoName', 'network_operators.country', 'auth_users.name As username', 'vpn_configs.vpn_device', 'vpn_configs.mno_id', 'vpn_configs.partner_type', 'vpn_configs.remote_peer_ip', 'vpn_configs.local_access_control_list', 'vpn_configs.partner_access_control_list')
            ->whereRaw("network_operators.name LIKE '%$keyword%' OR vpn_configs.vpn_device LIKE '%$keyword%' OR network_operators.country LIKE '%$keyword%' OR vpn_configs.remote_peer_ip LIKE '%$keyword%' OR vpn_configs.local_access_control_list like '%$keyword%' OR vpn_configs.partner_access_control_list LIKE '%$keyword%'")
            ->paginate(10);
        }
        else{
            $vpn_list_arr = \DB::table('vpn_configs')
            ->join('auth_users', 'auth_users.id', '=', 'vpn_configs.user_id')
            ->join('network_operators', 'network_operators.id', '=', 'vpn_configs.mno_id')
            ->select('vpn_configs.id', 'vpn_configs.date_received', 'network_operators.name AS mnoName', 'network_operators.country', 'auth_users.name As username', 'vpn_configs.vpn_device', 'vpn_configs.mno_id', 'vpn_configs.partner_type', 'vpn_configs.remote_peer_ip', 'vpn_configs.local_access_control_list', 'vpn_configs.partner_access_control_list')
            ->paginate(10);
    
        }
        return response()->json($vpn_list_arr);
    }

    public function create(){
        $partner_type_arr = ['client' => 'client', 'mno' => 'mno'];
        $partners_arr = []; 
        $services = Models\Service::pluck('name', 'name');
        $data = [
            'page_title' => 'infrastructure: VPN', 
            'partner_type_arr' => $partner_type_arr,
            'partners_arr' => $partners_arr,
            'services_arr' => $services
        ];
        return view('vpn.create', $data);    
    }
     public function edit($id){
        $vpn = Models\VpnConfig::find($id);
        $partner_type_arr = ['client' => 'client', 'mno' => 'mno'];
        $partners_arr = []; 
        $services = Models\Service::pluck('name', 'name');
        $data = [
            'vpn' => $vpn,
            'page_title' => 'infrastructure: VPN', 
            'partner_type_arr' => $partner_type_arr,
            'partners_arr' => $partners_arr,
            'services_arr' => $services
        ];
        return view('vpn.create', $data);    
    }



    public function store(Request $request){
        $request->validate([
            'partner_type_id' => 'required',
            'vpn_device' => 'sometimes',
            'remote_peer_ip' => 'required',
            'partner_access_control_list' => 'required',
            'local_access_control_list' => 'required',
            'partner_type' => 'required',
            'phase_1_raw_settings' => 'sometimes',
            'phase_2_raw_settings' => 'sometimes',
        ]);
        // dd($request->all());
        $vpn_configs_arr = $request->except('_token', 'vpn_form');
        if ($request->partner_type == 'mno') {
            
            $vpn_configs_arr['mno_id'] = $request->partner_type_id;
        }
        elseif($request->partner_type == 'client'){
            $vpn_configs_arr['client_id'] = $request->partner_type_id;
        }
        else{
            Session::flash('error_message', 'Your request could not be handled at this time. Try again');
            return redirect(url('infrastructure/vpn')); 
        }
        
        $vpn_configs_arr['user_id'] = session('current_user.id');

        // dd($vpn_configs_arr);
         if ($request->hasFile('vpn_form')) {
           if ($request->file('vpn_form')->isValid()) {
                $filename = "erp_" . time() . "." . $request->vpn_form->extension();
                $request->vpn_form->storeAs('vpn_form_files', $filename, 'public');
                $document_arr['vpn_form'] = json_encode([$filename]);
                $vpn_configs_arr['partner_vpn_form'] = $filename;
            }
        }
        $result = Models\VpnConfig::create($vpn_configs_arr);           

        Session::flash('success_message', 'VPN Configs successfully added');
        return redirect(url('infrastructure/vpn'));
    }

    public function getMNOandClient($type){
        if ($type == 'mno') {
            $data_arr = \DB::table('network_operators')->Select(\DB::raw('id, concat(name, " (", country, ")") AS partner'))->orderBy('name')->get()->toArray();
        }
        elseif ($type == 'client') {
            $data_arr = \DB::table('clients')->Select(\DB::raw('id, concat(name, " (", country, ")") AS partner'))->orderBy('name')->get()->toArray();
        }
        else{
            return response()->json(['code' => 3, 'msg' => 'invalid request']);
        }
        return response()->json(['code' => 1, 'msg' => 'success', 'result' => $data_arr]);
    }
    public function getPhaseOneTwo($vpn_list_id){
        $vpn = Models\VpnConfig::find($vpn_list_id);
        if ($vpn) {
            // code...
            $data = ['code' => 1, 'msg' => 'success', 'result' => $vpn];
        }
        else{
            $data = ['code' => 3, 'msg' => 'request could not be handled'];
        }
        return response()->json($data);

    }
}

