<?php


namespace App\Http\logic\admin;


use Illuminate\Support\Facades\DB;

class IpWhiteListLogic extends BaseLogicController
{
    public function index()
    {
        $list = DB::connection('read')->table('agent.dbo.ip_white_list')
            ->paginate(10);

        return compact('list');
    }

    public function add($requests,$admin_id)
    {
        $data = [
            'ip' => $requests['ip'],
            'explain' => $requests['explain'],
            'admin_id' => $admin_id,
            'create_at' => date('Y-m-d H:i:s'),
            'update_at' => date('Y-m-d H:i:s'),
        ];

        $r = DB::table('agent.dbo.ip_white_list')
            ->insert($data);

        return $r;

    }

    public function update($requests,$admin_id)
    {
        $data = [
            'ip' => $requests['ip'],
            'explain' => $requests['explain'],
            'admin_id' => $admin_id,
            'update_at' => date('Y-m-d H:i:s'),
        ];

        $r = DB::table('agent.dbo.ip_white_list')
            ->where('id',$requests['id'])
            ->update($data);

        return $r;

    }

    public function login_ip()
    {
        $list = DB::connection('read')->table('agent.dbo.admin_login_ip as ali')
            ->join('agent.dbo.admin_users as au','ali.admin_id','=','au.id')
            ->select('ali.*','au.nickname','au.account')
            ->orderBy('ali.admin_id')
            ->orderByDesc('last_login_time')
            ->paginate(10);
        return compact('list');
    }
}
