<?php

namespace App\Admin\Controllers;

use Dcat\Admin\Form;
use Dcat\Admin\Http\Controllers\AuthController as BaseAuthController;
use Dcat\Admin\Http\Repositories\Administrator;

class AuthController extends BaseAuthController
{
    protected $view = 'admin.pages.login';

    /**
     * Model-form for user setting.
     *
     * @return Form
     */
    protected function settingForm()
    {

        return new Form(new Administrator(), function (Form $form) {
            $form->action(admin_url('auth/setting'));
            $form->disableCreatingCheck();
            $form->disableEditingCheck();
            $form->disableViewCheck();

            $form->tools(function (Form\Tools $tools) {
                $tools->disableView();
                $tools->disableDelete();
            });

            $form->display('username', trans('admin.username'));
            $form->text('name', trans('admin.name'))->required();
            //$form->image('avatar', trans('admin.avatar'))->autoUpload();

            $form->password('old_password', trans('admin.old_password'));

            $form->password('password', trans('admin.password'))
                ->minLength(5)
                ->maxLength(20)
                ->customFormat(function ($v) {
                    if ($v == $this->password) {
                        return;
                    }

                    return $v;
                });
            $form->password('password_confirmation', trans('admin.password_confirmation'))->same('password');

            $form->ignore(['password_confirmation', 'old_password']);

            // 添加语言选择的下拉框
            $form->select('language', trans('admin.language'))
                ->options(config('dictionary.languages'))
                ->default('en')
                ->required();;  // 设置默认语言

            $form->saving(function (Form $form) {
                if ($form->password && $form->model()->password != $form->password) {
                    $form->password = bcrypt($form->password);
                }

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

            $form->saved(function (Form $form) {
                return $form
                    ->response()
                    ->success(trans('admin.update_succeeded'))
                    //->redirect('/');
                    ->script('setTimeout(() => {location.reload();}, 1000);');//保存成功后刷新页面
            });

        });
    }


}
