<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\User;
use App\Models\UserLock;
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()->orderByDesc('id');
            $grid->column('id')->sortable();
            $grid->column('name');
            $grid->column('account');
            $grid->column('email');
            $grid->column('online')->using(config('map.online'))->label(['gray', 'success']);
            $grid->column('member_type_text',admin_trans_field('member_type'))->label(['gray', 'success']);
            $grid->column('status')->switch();
            $grid->column('created_at')->sortable();
            $grid->disableCreateButton();
            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->like('name', admin_trans_field('name'))->width(4);
                $filter->like('mobile', admin_trans_field('mobile'))->width(4);
                $filter->like('email', admin_trans_field('email'))->width(4);
                $filter->equal('member_type', admin_trans_field('member_type'))->select([
                    1=>'一般会员',
                    2=>'企业会员',
                ])->width(4);
                $filter->between('created_at', admin_trans_field('created_at'))->datetime()->width(4);
            });
        });
    }

    /**
     * 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')->width(4);
            $show->field('name')->width(4);
            $show->field('email')->width(4);
            $show->field('mobile')->width(4);
            $show->field('jpush_reg_id')->width(4);
            $show->field('last_login_ip')->width(4);
            $show->field('last_login_time')->width(4);
            $show->field('register_ip')->width(4);
            $show->field('created_at')->width(4);
            $show->field('updated_at')->width(4);
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new User(), function (Form $form) {
            $form->display('id')->width(4);
            $form->text('name')->required()->width(4);
            $form->text('account')->required()->width(4);
            $form->email('email')->required()->width(4);
            $form->password('password')->default('')
                ->placeholder('不修改时，默认为空。')
                ->minLength(6)
                ->maxLength(20)
                ->customFormat(function ($v) {
                    if ($v == $this->password) {
                        return;
                    }
                    return $v;
                })->width(4);
            $form->switch('status')->width(4);
            $form->display('created_at')->width(4);
            $form->display('updated_at')->width(4);

            $form->saving(function (Form $form) {
                if(!$form->input('password')){
                    $form->deleteInput('password');
                }
            });

        });
    }
}
