<?php

namespace App\Admin\Controllers;

use App\Models\User;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class UserController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new User(), function (Grid $grid) {
            $grid->model()->withCount('creation')->orderByDesc('id');
            $grid->column('id')->sortable();
            $grid->column('name');
//            $grid->column('open_id');
            $grid->column('avatar')->image('',80,80);
            $grid->column('mobile');
//            $grid->column('status')->switch();
            $grid->column('online','是否在线')->switch();
            $grid->column('last_login_ip','最后登录IP');
//            $grid->column('register_ip');
            $grid->column('last_login_time','最后登录时间');
            $grid->column('is_black','是否拉黑')->switch();
//            $grid->column('sessionKey');
            $grid->column('diamond','剩余次数');
//            $grid->column('is_share');
//            $grid->column('share_name');
//            $grid->column('share_phone');
//            $grid->column('share_pid');
//            $grid->column('share_date');
            $grid->column('income','可提现佣金');
            $grid->column('creation_count','创作次数');
            $grid->column('qr_code','邀请二维码')->image();
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
            $grid->quickSearch(['name', 'id', 'mobile'])->placeholder('搜索...');
            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->equal('id');
                $filter->like('mobile');
                $filter->between('created_at')->datetime();
            });
            $grid->disableCreateButton();
            $grid->disableDeleteButton();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new User(), function (Show $show) {
            $show->model()->with('userShare');
            $show->field('id');
            $show->field('name');
            $show->field('open_id','微信openid');
            $show->field('avatar')->image();
            $show->field('mobile');
            $show->field('online');
            $show->field('last_login_ip');
            $show->field('last_login_time');
            $show->field('is_black')->as(function ($item){
                return $item ? '拉黑' : '正常';
            });
            $show->field('diamond');
            $show->field('is_share')->as(function ($item){
                return $item ? '是推广人' : '不是推广人';
            });
            $show->field('share_name');
            $show->field('share_phone');
            $show->field('userShare.name','推广人昵称（被）');
            $show->field('share_date');
            $show->field('income');
            $show->field('qr_code')->image();
            $show->field('created_at');
            $show->field('updated_at');
            $show->disableDeleteButton();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new User(), function (Form $form) {
            $form->display('id');
            $form->text('name');
//            $form->text('open_id');
            $form->image('avatar')->disk('oss')->autoUpload()->saving(function ($res) {
                return $res;
            })->required();;
            $form->text('mobile');
//            $form->text('status');
//            $form->text('online');
//            $form->text('last_login_ip');
//            $form->text('register_ip');
//            $form->text('last_login_time');
            $form->switch('is_black');
//            $form->text('sessionKey');
            $form->text('diamond');
            $form->switch('is_share');
            $form->text('share_name');
            $form->text('share_phone');
//            $form->text('share_pid');
//            $form->text('share_date');
            $form->text('income');
//            $form->text('qr_code');
//
//            $form->display('created_at');
//            $form->display('updated_at');
            $form->disableDeleteButton();
        });
    }
}
