<?php

namespace App\Http\Middleware;

use App\AdminUser;
use Closure;
use Illuminate\Support\Facades\DB;

class CheckSession
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {

//        if(!env('admin_check',0)){
//            exit(0);
//            return false;
//        }

            $ip = $request->ip();

//            $white_ip = DB::table('agent.dbo.ip_white_list')->where('ip', $ip)->exists();
//            if (!$white_ip) {
//                http_response_code(404);
//                exit();
//            }
//        }
        if (!$request->session()->has('admin')) {
            return redirect('/admin/login_op');
        }
        $admin = $request->session()->get('admin');
        if (!($admin instanceof AdminUser)) {
            return redirect('/admin/login_op');
        }
        \App::setLocale($admin->locale);
        // 判断管理员有没有被禁用
        $admin_id = $admin->id;
        $status = AdminUser::where('id',$admin_id)->value('status');
        if ($status == -1) {
            return redirect('/admin/login_op');
        }
        return $next($request);
    }
}
