<?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()
                ->orderBy('id','desc');
            $grid->column('id')->sortable()->width('80px');
            $grid->column('nickname');
            $grid->column('head_img')->image('',80,80);
            $grid->column('parent_id','上级用户')->display(function () {
                return $this->parent?$this->parent->nickname:'';
            });
            $grid->column('phone_num');
            $grid->column('is_vip')->using([0 => '否', 1 => '是']);
            $grid->column('created_at','注册时间');

            $grid->showQuickEditButton();
            // 禁用删除按钮
            $grid->disableDeleteButton();
            $grid->disableCreateButton();
            // 禁用
            $grid->disableBatchActions();
            // 禁用
            $grid->disableRowSelector();
            $grid->filter(function (Grid\Filter $filter) {
                $filter->like('nickname','用户姓名');
                $filter->equal('phone_num','手机号');
                $filter->like('parent.nickname','上级用户');
            });
            /*$grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });*/
            $grid->export();
        });
    }

    /**
     * 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('parent');
            $show->field('id');
            $show->field('nickname');
            $show->field('head_img','头像')->image();
            $show->parent_id()->as(function ($parent_id) {
                return $this->parent?$this->parent->nickname:'无';
            });
            $show->field('phone_num');
            $show->field('income');
            $show->field('is_vip')->using([0=>'否',1=>'是']);
            $show->field('share_qrcode','推广码')->image();
            $show->field('become_vip_at');
            $show->field('become_child_at');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new User(), function (Form $form) {
            $form->display('id');
            $form->display('nickname','昵称');
            $form->image('head_img')->disable();
            $form->display('phone_num');
            $form->display('income');
            $form->switch('is_vip', '是否会员？')->default(1);
            $form->image('share_qrcode')->disable();
            $form->display('become_vip_at');
            $form->display('become_child_at');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
