<?php

namespace App\Http\Controllers\Admin;

use App\Models\User;
use App\Models\SystemLog;
use Illuminate\Http\Request;

class SystemLogController extends AdminController
{
    /**
     * Display a listing of the resource.
     *
     * @param Request $request
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $where = $request->except('page');

        $user = User::where('status', 1)->where('username', 'master')->first();

        $query = SystemLog::with('user');

        if ($user && !auth()->user()->isMaster()) {
            $query->where('user_id', '<>', $user->id);
        }

        if ($request->has('record')) {
            $query->where('record', 'like', "%{$where['record']}%");
        }

        $logs = $query->latest()->paginate($where['perPage'] ?? 20);

        return view('admin.system_log.index', compact('logs', 'where'));
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $data = SystemLog::with('user')->find($id);

        return view('admin.system_log.show', compact('data'));
    }
}