<?php

namespace App\Admin\Controllers\System;

use App\Http\Controllers\Controller;
use App\Models\MembersLog;
use App\Repositories\MemberLogRepository;
use Encore\Admin\Controllers\HasResourceActions;

use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class UserLogController extends Controller
{
    use HasResourceActions;

    protected $memberLogRepository;

    public function __construct(
        MemberLogRepository $memberLogRepository
    )
    {
    $this->memberLogRepository=$memberLogRepository;
    }


    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        $grid=$this->grid()->render();
        return $content
            ->header('会员操作日志')
            ->description('系统会自动删除一个月前的日志')
            ->body(view('admin.system.userlog.index')->with('grid', $grid));
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('操作日志')
            ->description('详情')
            ->body($this->detail($id));
    }



    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new MembersLog());
        $grid->disableActions();
        $grid->disableRowSelector();
        $grid->model()
//            ->whereIn('type',[1002,1003,1006,1007,1009,1008,1000,1001])
            ->orderby('created_at','desc');
        $grid->id('ID');
        $grid->log_username('会员名称')->display(function ($username){

            return "<span style='' title='{$username}' class='vtip' >".$username."</span>";
        })->width(200);
        $grid->log_utype('用户类型')->display(function ($log_utype) {
            $html='';
            if ($log_utype==1) {
                $html.='企业';
            }else{
                $html.="个人";
            }
            return $html;
        })->width(200);


        $grid->log_value('描述')->width(600);
        $grid->log_ip('登陆ip')->display(function ($ip) {
            return long2ip($ip);
        });
        $grid->log_source('操作途径');
        $grid->created_at('操作时间');

        $grid->filter(function ($filter) {
            $filter->column(1/2, function ($filter) {
                // 在这里添加字段过滤器
                $filter->like('log_username', '操作用户');
                $filter->equal('log_utype', '用户类型')->select(['1'=>"企业","2"=>"个人"]);
            });

            $filter->column(1/2, function ($filter) {
                // 在这里添加字段过滤器
                $filter->equal('type', '日志类型')->select($this->memberLogRepository->typeArrs());
                $filter->between('created_at', '操作时间')->datetime();
            });
        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(MembersLog::findOrFail($id));
        $show->id('ID');


        $show->log_username('操作用户');
        $show->log_value('日志说明');
        $show->log_ip('登陆ip')->as(function ($ip){
            return long2ip($ip);
        });
        $show->log_source('操作途径');
        $show->created_at('操作时间');
        $show->log_address('登陆地址');


//        $typeArrs=$this->memberLogRepository->type_arrs();
//        $show->type("日志类型")->as(function ($type) use ($typeArrs) {
//            return $typeArrs[$type]['type'];
//        });

        $show->log_utype('用户类型')->as(function ($log_utype) {
            $html='';
            if ($log_utype==1) {
                $html.='企业';
            }else{
                $html.="个人";
            }

            return $html;
        })->width(200);

        $show->created_at('创建时间');
//        $show->updated_at('Updated at');

        return $show;
    }
    
}