<?php


namespace App\Http\Controllers\Admin;


use App\AdminUser;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class AdminLogController extends BaseController
{
    public function index(Request $request)
    {

        $start_time = $request->get('start_time');
        $end_time = $request->get('end_time');
        $nickname = $request->get('nickname');

        $where = [];

        !empty($start_time) && $where[] = ['log.create_at','>=',$start_time];
        !empty($end_time) && $where[] = ['log.create_at','<=',$end_time];
        !empty($nickname) && $where[] = ['admin.nickname','=',$nickname];


        $list = DB::table('agent.dbo.admin_log as log')
            ->join('agent.dbo.admin_users as admin','log.admin_id','=','admin.id')
            ->where($where)
            ->orderByDesc(DB::raw("CONVERT(varchar(100), log.create_at, 120)"))
            ->select('log.id','log.admin_id','url','content','explain',DB::raw("CONVERT(varchar(100), log.create_at, 120) as create_at"),DB::raw("CONVERT(varchar(100), log.update_at, 120) as update_at"))

            ->groupBy('log.id','log.admin_id','url','content','explain',DB::raw("CONVERT(varchar(100), log.create_at, 120)"),DB::raw("CONVERT(varchar(100), log.update_at, 120)"))

            ->paginate(10);
        foreach ($list as &$value){
            $admin = AdminUser::where('id',$value->admin_id)->first();
            $value->nickname = $admin->nickname;
            $value->account = $admin->account;
            $value->roles = $admin->roles->map(function ($val){
                    return $val->name;
                })[0] ?? '';
        }

        return  view('admin.adminLog.record',[
            'list'=>$list,
            'start_time' => $start_time,
            'end_time' => $end_time,
            'nickname' => $nickname
        ]);
        return view('admin.adminLog.index',['list'=>$list]);
    }

    public function show($id)
    {

        $list = DB::table('agent.dbo.admin_log as log')
            ->select('explain')
            ->first()->explain;
        return view('admin.adminLog.show',['list'=>$list]);
    }
}
