<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Games\GameScores;
use App\Admin\Actions\Users\FansList;
use App\Admin\Actions\Users\FollowList;
use App\Admin\Actions\Users\SetUserInfo;
use App\Admin\Actions\Users\UserAction;
use App\Admin\Actions\Users\UserAuth;
use App\Admin\Actions\Users\UserChange;
use App\Admin\Actions\Users\UsersInfo;
use App\Admin\Actions\Users\UserVip;
use App\Models\User;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Widgets\Modal;
use Illuminate\Database\Eloquent\Model;

class UserController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new User());
        $grid->model()->with('user_extra')->orderBy('id', 'desc');

        $grid->column('id')->sortable();
        $grid->column('name');
        $grid->column('avatar')->image("", 50);
        $grid->column('email');
        $grid->column('mobile');
        $grid->column('sex')->using([0 => trans('user.fields.unknown'), 1 => trans('user.fields.man'), 2 => trans('user.fields.woman')])->label(['gray', 'primary', 'danger']);
        $grid->column('is_auth')->using([0 => trans('user.fields.unauthorized'), 1 => trans('user.fields.organization'), 2 => trans('user.fields.public_figure')])->label(['gray', 'success', 'red']);;
        $grid->column('online', trans('user.fields.online_status'))->using([0 => trans('user.fields.offline'), 1 => trans('user.fields.online')])->label(['gray', 'success']);
        $grid->column('user_extra.fans_count', trans('user.fields.fansNum'))->display(function ($res) {
            $form = FansList::make()->payload(['id' => $this->id]);
            return Modal::make()
                ->lg()
                ->title(trans('user.fields.fansList'))
                ->body($form)
                ->button('<i class="feather icon-align-right"></i> ' . $res);
        });
        $grid->column('user_extra.follow_count', trans('user.fields.followNum'))->display(function ($res) {
            $form = FollowList::make()->payload(['id' => $this->id]);
            return Modal::make()
                ->lg()
                ->title(trans('user.fields.followList'))
                ->body($form)
                ->button('<i class="feather icon-align-right"></i> ' . $res);
        });
        $grid->column('status')->switch('success', true);
        $grid->column('created_at');

        // 禁用
        $grid->disableRowSelector();
        $grid->filter(function (Grid\Filter $filter) {
            $filter->equal('id');
            $filter->like('mobile');
            $filter->like('email');
            $filter->like('name');
            $filter->equal('sex')->select(['1' => trans('user.fields.man'), '2' => trans('user.fields.woman')]);
            $filter->equal('is_auth')->select(['0' => trans('user.fields.unauthorized'), '1' => trans('user.fields.organization'), '2' => trans('user.fields.public_figure')]);
            $filter->equal('status')->select(['1' => trans('user.fields.normal'), '0' => trans('user.fields.frozen')]);
        });

        //操作管理
        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableView();
            $actions->append(new UserChange(User::class));
            $actions->append(new UserAuth(User::class));
            $actions->append(new GameScores('user'));
        });

        //批量操作
        $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('name');
            $show->field('avatar');
            $show->field('email');
            $show->field('mobile');
            $show->field('bio');
            $show->field('sex');
            $show->field('location');
            $show->field('bg');
            $show->field('tencent_im_user_id');
            $show->field('register_ip');
            $show->field('last_login_ip');
            $show->field('is_auth');
            $show->field('online');
            $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->text('name');
            $form->image('avatar');
            $form->text('email');
            $form->text('mobile');
            $form->text('bio');
            $form->image('bg');
            $form->text('tencent_im_user_id')->readOnly();
            $form->switch('status')->default(1);
            $form->display('created_at');
            $form->display('updated_at');
            $form->footer(function ($footer) {
                // 去掉`查看`checkbox
                $footer->disableViewCheck();
                // 去掉`继续编辑`checkbox
                $footer->disableEditingCheck();
                // 去掉`继续创建`checkbox
                $footer->disableCreatingCheck();
            });
        });
    }
}
