<?php

namespace App\Http\Controllers;

use App\Log;
use App\Services\LogService;
use Exception;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Redirector;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Validator;
use function GuzzleHttp\Psr7\str;

class LogController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @param Request $request
     * @return Application|LengthAwarePaginator|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(Request $request)
    {
        if (!Gate::allows('日志-查询')) {
            return redirect(url('/'));
        }
        //没有查询条件，默认展示最近50条
        if (!$request->has('created_at_start') &&
            !$request->has('created_at_end') &&
            !$request->has('operation') &&
            !$request->has('type') &&
            !$request->has('description')
        ) {
            $logs = Log::query()
                ->orderBy('id', 'desc')
                ->paginate(50);
            return view('maintenance.log.index', ['logs' => $logs]);
        }
        //不传开始时间提示错误信息并返回
        if (!$request->has('created_at_start')) {
            session()->flash('warning', '请选择开始时间');
            return view('maintenance.log.index', ['logs' => null]);
        }
        $query = Log::query();
        if ($request->has('operation')) {
            $query->where('operation', 'like', $request->operation . '%');
        }
        if ($request->has('type')) {
            $query->where('type', 'like', $request->type . '%');
        }
        if ($request->has('description')) {
            $str = $request->description;
            $str = trim($str, '\\');
            $str = str_replace('\\', '\\\\', $str);
            $query->where('description', 'like', '%' . $str . '%');
        }
        if ($request->has('created_at_start')) {
            $query->where('created_at', '>=', $request->created_at_start);
        }
        if ($request->has('created_at_end')) {
            $query->where('created_at', '<=', $request->created_at_end);
        }
        $query->orderByDesc('id');
        $logs = $query->paginate($request->paginate??50);
        return view('maintenance.log.index', ['logs' => $logs]);
    }

    /**
     * Display the specified resource.
     *
     * @param Log $log
     * @return Response
     */
    public function show(Log $log)
    {
        return view('maintenance.log.show', ['log' => $log]);
    }


    /**
     * Remove the specified resource from storage.
     *
     * @param Log $log
     * @return array|Response
     * @throws Exception
     */
    public function destroy(Log $log)
    {
//        if(!Gate::allows('物流公司-删除')){ return redirect(url('/'));  }
//        app('LogService')->log(__METHOD__,__FUNCTION__,$log->toJson(),Auth::user()['id']);
//        $re=$log->delete();
//        return ['success'=>$re];
    }

    public function syncRedisLogs()
    {
        LogService::syncRedisLogs();
        return redirect('maintenance/log');
    }
}
