<?php

namespace App\Admin\Controllers;

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

class UserIdentifyController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new UserIdentify(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('user_id');
            $grid->column('avatar')->image('',50);
            $grid->column('name');
            $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('bio');
            $grid->column('birthday');
            $grid->column('action_type')->using([0=>trans('user.fields.unknown'),1=>trans('user-identify.fields.background_add'),2=>trans('user-identify.fields.user_apply')])->label(['gray','primary','blue']);
            $grid->column('type')->using([0=>trans('user.fields.unknown'),1=>trans('user.fields.organization'),2=>trans('user.fields.public_figure')])->label(['gray','primary','blue']);
            $grid->column('status')->using([0=>trans('user.fields.unauthorized'),1=>trans('user.fields.authorized')])->label(['gray','success']);
            $grid->column('created_at');
//            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->equal('user_id');
                $filter->like('mobile');
                $filter->like('email');
                $filter->like('name');
                $filter->equal('status')->select(['0'=>trans('user.fields.unauthorized'),'1'=>trans('user.fields.authorized')]);

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

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new UserIdentify(), function (Show $show) {
            $show->field('id');
            $show->field('user_id');
            $show->field('name');
            $show->field('email');
            $show->field('mobile');
            $show->field('bio');
            $show->field('sex');
            $show->field('birthday');
            $show->field('avatar');
            $show->field('action_type');
            $show->field('type');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new UserIdentify(), function (Form $form) {
            $form->display('id');
            $form->text('user_id');
            $form->text('name');
            $form->text('email');
            $form->text('mobile');
            $form->text('bio');
            $form->text('sex');
            $form->text('birthday');
            $form->text('avatar');
            $form->text('action_type');
            $form->text('type');
            $form->text('status');

            $form->display('created_at');
            $form->display('updated_at');
            $form->footer(function ($footer) {
                // 去掉`查看`checkbox
                $footer->disableViewCheck();
                // 去掉`继续编辑`checkbox
                $footer->disableEditingCheck();
                // 去掉`继续创建`checkbox
                $footer->disableCreatingCheck();
            });
        });
    }
}
