<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Users\UserAction;
use App\Admin\Actions\Users\UserAuth;
use App\Admin\Actions\Users\UserChange;
use App\Admin\Actions\Users\UserVip;
use App\Models\User;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Show;

class UserController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new User());
        $grid->model()->leftJoin('users_info','users.id','=','users_info.user_id')->select(['users.*','users_info.nickname','users_info.avatar','users_info.area'])->orderBy('id','desc');
        $grid->column('id')->sortable();
        $grid->column('avatar','头像')->image("",50);
        $grid->column('mobile','手机号');
        $grid->column('nickname','昵称');

        $grid->column('pid','上级ID');
        $grid->column('sex','性别')->using([0=>"未知",1=>'男',2=>'女'])->label(['','primary','danger']);
        $grid->column('is_vip','VIP')->using(['无','VIP'])->label(['gray','warning']);
        $grid->column('is_auth','认证状态')->using([0=>'未认证',1=>"真人认证",2=>"女神认证"])->label(['gray','success','red']);;
        $grid->column('ycode','邀请码');
        $grid->column('area','所在城市');
        $grid->column('online','在线状态')->using([0=>'离线',1=>'在线'])->label(['gray','success']);
//        $grid->column('notice_status');
        $grid->column('like_num','他喜欢');
        $grid->column('like_me_num','喜欢他');
        $grid->column('look_num','看过他');
        $grid->column('status','状态')->switch('success',true);
        $grid->filter(function (Grid\Filter $filter) {
            $filter->equal('id','用户ID');
            $filter->equal('pid','上级ID');
            $filter->like('mobile','手机号');
            $filter->like('nickname','昵称');
            $filter->equal('sex','性别')->select(['1'=>'男','2'=>'女']);
            $filter->equal('is_vip','VIP')->select(['0'=>'无','1'=>'VIP']);
            $filter->equal('is_auth','认证状态')->select(['0'=>'未认证','1'=>'真人认证','2'=>'女神认证']);
            $filter->equal('online','在线状态')->select(['0'=>'离线','1'=>'在线']);
        });

        $grid->disableCreateButton();
        //操作管理
        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableView();
            $actions->disableEdit();
            $actions->append(new UserAction(User::class));
            $actions->append(new UserChange(User::class));
//            if ($actions->row->is_auth == 0) {
            $actions->append(new UserAuth(User::class));
//            }
            $actions->append(new UserVip(User::class));
        });

        //批量操作
        $grid->batchActions(function (Grid\Tools\BatchActions $batch) {
            $batch->disableDelete();
        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new User(), function (Show $show) {
            $show->field('id');
            $show->field('mobile','手机号');
            $show->field('pid');
            $show->field('tencent_im_user_id');
            $show->field('sex');
            $show->field('is_vip');
            $show->field('is_auth');
            $show->field('ycode');
            $show->field('city');
            $show->field('online');
            $show->field('notice_status');
            $show->field('like_num');
            $show->field('like_me_num');
            $show->field('look_num');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new User(), function (Form $form) {
            $form->display('id');
            $form->text('mobile');
            $form->text('pid');
            $form->text('tencent_im_user_id');
            $form->text('sex');
            $form->text('is_vip');
            $form->text('is_auth');
            $form->text('ycode');
            $form->text('city');
            $form->text('online');
            $form->text('notice_status');
            $form->text('like_num');
            $form->text('like_me_num');
            $form->text('look_num');
            $form->switch('status')->default(1);
        });
    }
}
