<?php

namespace Modules\User\Http\Controllers\Admin;

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

class StockController extends AdminController
{

    protected $title = '会员库存';

    /**
     * Notes   : 用户管理列表
     *
     * @Date   : 2021/3/11 1:59 下午
     * @Author : <Jason.C>
     * @return Grid
     */
    public function grid(): Grid
    {
        $grid = new Grid(new UserStock());


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


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

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

        $grid->column('user.username', '用户名');
        $grid->column('用户昵称')
            ->display(function () {
                return $this->user->info->nickname;
            });

        $grid->column('stock', '总数');
        $grid->column('logs_count', '变动记录')
            ->link(function () {
                return admin_url('/users/stocks/'.$this->id.'/logs');
            }, '_blank');
        $grid->column('剩余')
            ->display(function () {
                return $this->residue;
            });

        $grid->column('created_at', '注册时间');

        return $grid;
    }

}
