<?php

namespace App\Admin\Controllers\UserManagement\BmUser;

use App\Models\UserBalanceLog;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class UserBalanceLogController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '余额明细';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new UserBalanceLog());
        $grid->disableCreateButton();
        //根据id筛选数据
        $uesr_id = \request('user_id');
        $grid->filter(function ($filter){
            $filter->disableIdFilter();
            $filter->equal('user_id','用户id');
        });
        //关闭全部的操作
        $grid->disableActions();
        $grid->model()->orderBy('id','desc');
        $grid->column('id', __('Id'));
        $grid->column('user_id', __('用户ID'));
        $grid->column('user.nickname', __('用户名称'));
        $grid->column('admin_user_id', __('后台用户ID'));
        $grid->column('type', __('类型'))
            ->using([1=>'订单消费',2=>'用户充值',3=>'后台赠送']);
        $grid->column('relation_id', __('订单id'));
        $grid->column('before_balance', __('变化前的余额'))->display(function ($price){
            return $price/100;
        });
        $grid->column('change_balance', __('改变的余额'))->display(function ($price){
            return $price/100;
        });
        $grid->column('after_balance', __('变化后的余额'))->display(function ($price){
            return $price/100;
        });
        $grid->column('remark', __('备注'));
        $grid->column('created_at', __('创建时间'));
        $grid->column('updated_at', __('更新时间'));

        return $grid;
    }

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

        $show->field('id', __('Id'));
        $show->field('user_id', __('User id'));
        $show->field('admin_user_id', __('Admin user id'));
        $show->field('type', __('Type'));
        $show->field('relation_id', __('Relation id'));
        $show->field('before_balance', __('Before balance'));
        $show->field('change_balance', __('Change balance'));
        $show->field('after_balance', __('After balance'));
        $show->field('remark', __('Remark'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new UserBalanceLog());

        $form->number('user_id', __('User id'));
        $form->number('admin_user_id', __('Admin user id'));
        $form->switch('type', __('Type'));
        $form->number('relation_id', __('Relation id'));
        $form->number('before_balance', __('Before balance'));
        $form->number('change_balance', __('Change balance'));
        $form->number('after_balance', __('After balance'));
        $form->text('remark', __('Remark'));

        return $form;
    }
}
