<?php


namespace App\Http\Controllers\Admin;


use App\Http\Controllers\Controller;
use App\Http\logic\admin\IpWhiteListLogic;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;


class IpWhiteListController extends Controller
{
    public function index()
    {

        $result = (new IpWhiteListLogic())->index();
        return view('admin.IpWhiteList.index', $result);
    }

    public function add_view()
    {
        return view('admin.IpWhiteList.add');
    }

    public function add(Request $request)
    {

        $requests = $request->post();

        $validator = \Illuminate\Support\Facades\Validator::make($requests, [
            'ip' => 'ip'
        ]);

        if ($validator->fails()) {
            return $this->json(500, $validator->errors()->getMessages()['ip'][0]);
        }

        $admin_id = $request->session()->get('admin')['id'];

        (new IpWhiteListLogic())->add($requests, $admin_id);

        return $this->json(200, '添加成功');
    }

    public function update_view($id)
    {

        $info = DB::table('agent.dbo.ip_white_list')
            ->where('id',$id)
            ->first();

        return view('admin.IpWhiteList.update',['info'=>$info]);
    }

    public function update(Request $request)
    {
        $post = $request->post();

        $validator = \Illuminate\Support\Facades\Validator::make($post, [
            'ip' => 'ip'
        ]);

        if ($validator->fails()) {
            return $this->json(500, $validator->errors()->getMessages()['ip'][0]);
        }

        $admin_id = $request->session()->get('admin')['id'];

        (new IpWhiteListLogic())->update($post, $admin_id);

        return $this->json(200, '添加成功');

    }

    public function del($id)
    {
        $r = DB::table('agent.dbo.ip_white_list')->where('id',$id)->delete();

        return $this->json(200, '删除成功');
    }

    public function login_ip()
    {
        $result = (new IpWhiteListLogic())->login_ip();
        return view('admin.IpWhiteList.login_ip', $result);
    }
}
