<?php

namespace App\Admin\Controllers\UserManagement\BmUser;


use App\Admin\Actions\backstage\User\archives;
use App\Admin\Actions\backstage\User\BalanceLog;
use App\Admin\Actions\backstage\User\BlackList;
use App\Admin\Actions\backstage\User\coupons;
use App\Admin\Actions\backstage\User\Orders;
use App\Admin\Actions\backstage\User\service;
use App\Models\Patient;
use App\Models\User;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class UserListController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '用户列表';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new User());
        $grid->disableCreateButton();
        $grid->actions(function ($actions) {
        // 去掉删除
        $actions->disableDelete();
        // 去掉编辑
        $actions->disableEdit();
        // 去掉查看
        $actions->disableView();
//        $actions->add(new archives());
        $actions->add(new BalanceLog());
        $actions->add(new BlackList());
        $actions->add(new coupons());
        $actions->add(new Orders());
        $actions->add(new service());
    });
        //筛选
        $grid->filter(function ($filter){
            $filter->column(1/2, function ($filter) {
                $filter->disableIdFilter();
                $filter->equal('phone','手机')->mobile()->placeholder("请输入手机");
                $filter->like('patient.name','患者姓名')->placeholder("请输入患者姓名");
                $filter->like('nickname','昵称');
            });
            $filter->column(1/2, function ($filter) {
                $filter->equal('status','用户状态')->radio([
                    ''   => '不限',
                    0    => '黑名单',
                    1    => '正常',
                ]);
                $filter->between('created_at', '注册时间')->datetime();
                $filter->timestampBetween('last_login_time', "最后登录时间")->datetime();
            });
        });
            $grid ->model()->where('status','>=','0')->where('is_docter',0)->orderBy('id','desc');
            $grid->column('id', __('用户id'))->sortable();
            $grid->column('avatar', __('头像'))->lightbox(['width' =>'', 'height' => 30]);
            $grid->column('nickname', __('用户名'));
            $grid->column('phone', __('手机号码'));
            $grid->column('status', __('状态'))->using([0=>'黑名单',1=>'正常',]);
            $grid->column('balance', __('当前余额'))->display(function ($price){
                return $price/100;
            });
            $grid->column('created_at', __('注册时间'));
            $grid->column('last_login_time', __('最后登录时间'))->display(function ($time){
                return date("Y-m-d H:i",$time);
            });
        return $grid;
    }

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

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