<?php

namespace Modules\User\Http\Controllers\Admin;

use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Grid;
use Modules\User\Models\IdentityLog;

class IdentityLogController extends AdminController
{

    protected $title = '身份变动记录';

    public function grid(): Grid
    {
        $grid = new Grid(new IdentityLog());

        $grid->disableActions();
        $grid->disableCreateButton();

        $grid->filter(function (Grid\Filter $filter) {
            $filter->column(1 / 3, function (Grid\Filter $filter) {
                $filter->like('user.username', '用户名');
            });

            $filter->column(1 / 3, function (Grid\Filter $filter) {
                $filter->like('user.info.nickname', '用户昵称');
            });

            $filter->column(1 / 3, function (Grid\Filter $filter) {
                $filter->equal('channel', '变更方式')->select(IdentityLog::CHANNEL_MAP);
            });
        });

        $grid->column('user.username', '用户名');
        $grid->column('用户昵称')->display(function () {
            return $this->user->info->nickname;
        });
        $grid->column('before_identity.name', '变更前身份')->display(function () {
            if ($this->before == 0) {
                return '无';
            }

            return $this->before_identity->name;
        });
        $grid->column('after_identity.name', '变更后身份')->display(function () {
            if ($this->after == 0) {
                return '无';
            }

            return $this->after_identity->name;
        });
        $grid->column('channel', '变更方式')->using(IdentityLog::CHANNEL_MAP);
        $grid->column('remark', '备注');
        $grid->column('created_at', '变更时间');

        return $grid;
    }

}
