<?php

namespace App\Http\Middleware;

use App\Models\SystemTrack;
use Auth;
use Closure;

class SystemTrackMiddleware
{

    public function handle($request, Closure $next)
    {
        $uri = $request->path();

        $method = $request->method();

        $ipAddress = $request->ip();

        //$input = $request->all();

        $url = $request->url();

        $routeName = \Route::currentRouteName();

        //$header = request()->headers;

        $model = new SystemTrack;
        $user = Auth::user();
        if ($user) {
            $model->auth_user_id = $user->id;
            //$model->user = $user;
        }

        $model->ip_address = $ipAddress;
        $model->method = $method;
        $model->uri = $uri;
        //$model->request_body = $input;
        $model->url = $url;
        $model->route_name = $routeName;
        //$model->header = $header;

        $model->route_name = $routeName;

        $model->save();

        return $next($request);
    }
}
