<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Grid\ResetPassword;
use App\Models\Account;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class AccountController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Account(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('user_name');
            $grid->column('type')
                ->using(config('global.user_type'))
                ->label(['info','primary','success']);;
            $grid->column('account');
            $grid->column('status')
                ->using(config('global.user_status'))
                ->label(['danger','success'])->switch();
            $grid->column('remark')->editable();
            $grid->column('created_at');
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->append(new ResetPassword(Account::class, $actions->row->id));
            });

            $grid->disableViewButton();

        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Account(), function (Show $show) {
            $show->field('id');
            $show->field('user_name');
            $show->field('account');
            $show->field('password');
            $show->field('type');
            $show->field('remark');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Account(), function (Form $form) {
            $form->display('id');
            $form->text('user_name')->required();
            $form->mobile('account')
                ->placeholder('手机号')
                ->required()
                ->rules(function ($form) {
                    // 如果不是编辑状态，则添加字段唯一验证
                    if (!$form->model()->id) {
                        return 'unique:accounts';
                    }
                });
            $form->password('password')
                ->placeholder('默认123456');
            $form->radio('type')
                ->options(config('global.user_type'))
                ->default(1);
            $form->radio('status')
                ->options(config('global.user_status'))
                ->default(1);
            $form->textarea('remark');

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

            $form->saving(function (Form $form) {
                // 如果不是编辑状态 不填写密码默认123456
                if (!$form->model()->id) {
                     if(!$form->password){
                         $form->password = \Hash::make(123456);
                     }else{
                         $form->password = \Hash::make($form->password);
                     }
                }else{
                    if ($form->password && $form->model()->password != $form->password) {
                        $form->password = \Hash::make($form->password);
                    }

                    if (! $form->password) {
                        $form->deleteInput('password');
                    }
                }

            });

            $form->disableViewButton();
            $form->disableDeleteButton();
            $form->disableListButton();
            $form->disableEditingCheck();
            $form->disableViewCheck();
            $form->disableCreatingCheck();

        });
    }
}
