<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;

class OperationLog
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse)  $next
     * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
     */
    public function handle(Request $request, Closure $next)
    {

        $thisMethod = $request->method();
        if (in_array(strtoupper($thisMethod),['POST','PUT','DELETE','PATCH'])){
            $uri = $request->route()->uri();
            $thisUrl = $request->url();
            $thisRouteTitle = $request->route()->getName();
            $arg=$request->all();
            if(isset($arg['_token'])){
                unset($arg['_token']);
            }
            if(isset($arg['password'])){
                unset($arg['password']);
            }
            if (isset($arg['password_confirmation'])){
                unset($arg['password_confirmation']);
            }
            if(isset($arg['s'])){
                unset($arg['s']);
            }
            $authInfo=Auth::user();
            $data = [
                'uri'=>$uri,
                'url' => $thisUrl,
                'method' => $thisMethod,
                'route_title'=>$thisRouteTitle,
                'admin_id'=>$authInfo->id??'',
                'admin_name'=>$authInfo->name??'',
                'admin_mobile'=>$authInfo->mobile??'',
                'details' => $arg,//collect($arg)->toJson(JSON_UNESCAPED_UNICODE),
                'input' => $arg,
            ];
            Log::info('操作日志-前',$data);
            //写入Db
            if(!empty($authInfo->name)){
                $this->writeDbLog($data);
            }
        }
        return $next($request);
    }

    /**
     * 操作后执行
     * @param $request
     * @param $response
     */
    public function terminate($request, $response){
        $thisMethod = $request->method();
        if (in_array(strtoupper($thisMethod),['POST','PUT','DELETE','PATCH'])){
            $uri = $request->route()->uri();
            $thisUrl = $request->url();
            $thisRouteTitle = $request->route()->getName();
            $arg=$request->all();
            if(isset($arg['_token'])){
                unset($arg['_token']);
            }
            if(isset($arg['password'])){
                unset($arg['password']);
            }
            if (isset($arg['password_confirmation'])){
                unset($arg['password_confirmation']);
            }
            if(isset($arg['s'])){
                unset($arg['s']);
            }
            $authInfo=Auth::user();
            $response=$response->getContent();
            $data = [
                'url' => $thisUrl,
                'route'=>$uri,
                'method' => $thisMethod,
                'route_title'=>$thisRouteTitle,
                'admin_id'=>$authInfo->id??'',
                'admin_name'=>$authInfo->name??'',
                'admin_mobile'=>$authInfo->email??'',
                'details' => $arg,//collect($arg)->toJson(JSON_UNESCAPED_UNICODE),
                'response'=>json_decode($response,true),
                'input' => $arg,
            ];
            Log::info('操作日志-后',$data);
        }
    }

    /**
     * 操作日志写入DB
     * @param array $data
     */
    private function writeDbLog(array $data){
        $details = $data['details']??[];
        $describe = $data['route_title']??'';
        \App\Models\OperationLog::create([
            'url' => $data['url']??'',
            'admin_id'=>$data['admin_id']??0,
            'admin_name'=>$data['admin_name']??'',
            'admin_mobile'=>$data['admin_mobile']??'',
            'describe'=>$describe,
            'route'=>$data['uri']??'',
            'route_title'=>$data['route_title']??'',
            'method'=>$data['method']??'',
            'details'=>collect($details)->toJson(JSON_UNESCAPED_UNICODE),
            'input' => collect($details)->toJson(JSON_UNESCAPED_UNICODE),
            'ip' => get_real_ip(),
        ]);

    }
}
