<?php

namespace Modules\User\Http\Controllers\Admin;

use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Grid;
use Modules\User\Models\Account;
use Modules\User\Models\AccountLog;

class AccountController extends AdminController
{

    protected $title = '用户账户';

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

        $grid->model()->orderByDesc('user_id');

        $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', '用户昵称');
            });
        });

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

        $grid->model()->with(['user.info']);

        $grid->column('user.username', '用户名');
        $grid->column('用户昵称')->display(function () {
            return $this->user->info->nickname;
        });
        $grid->column('balance', '余额');
        $grid->column('score', '水滴');
//        $grid->column('coins');
//        $grid->column('other');
        $grid->column('updated_at', '更新时间');
        $grid->column('账户日志')->display(function () {
            return '账户日志';
        })->link(function () {
            return route('admin.user.account.logs', $this);
        }, '_self');

        return $grid;
    }

    public function detail($id): Grid
    {
        $grid = new Grid(new AccountLog());

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

        if (is_numeric($id)) {
            $grid->model()->where('account_id', $id);
        } else {
            $grid->column('用户')
                ->display(function () {
                    return $this->account->user->username."({$this->account->user->info->nickname})";
                });
        }

        $grid->column('type', '账户类型');
        $grid->column('rule.title', '账变规则');
        $grid->column('amount', '账变金额');
        $grid->column('balance', '当期余额');
        $grid->column('remark', '备注');
        $grid->column('source', '详情')->hide();
        $grid->column('created_at', '账变时间');

        return $grid;
    }

}