<?php

namespace App\Admin\Forms;

use Dcat\Admin\Contracts\LazyRenderable;
use Dcat\Admin\Traits\LazyWidget;
use Dcat\Admin\Widgets\Form;
use Illuminate\Support\Arr;

class AdminSetting extends Form implements LazyRenderable
{
    use LazyWidget;

    /**
     * 主题颜色.
     *
     * @var array
     */
    protected $colors;

    public function __construct($data = [], $key = null)
    {
        parent::__construct($data, $key);
        $this->colors = [
            'default'    => trans('site-setting.Dark_blue'),
            'blue'       => trans('site-setting.blue'),
//          'blue-light' => '浅蓝',
//          'blue-dark'  => '深蓝',
            'green'      => trans('site-setting.green'),
        ];
    }

    /**
     * 处理表单请求.
     *
     * @param array $input
     *
     * @return mixed
     */
    public function handle(array $input)
    {
        foreach (Arr::dot($input) as $k => $v) {
            $this->update($k, $v);
        }

        return $this->response()->success(trans('site-setting.Set_successfully'));
    }

    /**
     * 构建表单.
     */
    public function form()
    {
        // 'en' => 'English',
        $this->radio('lang', trans('site-setting.language'))->width(6)->required()->options(['zh' => '简体中文','ko'=>'한국어']);
    }

    /**
     * 设置接口保存成功后的回调JS代码.
     *
     * 1.2秒后刷新整个页面.
     *
     * @return string|void
     */
    public function savedScript()
    {
        return <<<'JS'
    if (data.status) {
        setTimeout(function () {
          location.reload()
        }, 1000);
    }
JS;
    }

    /**
     * 返回表单数据.
     *
     * @return array
     */
    public function default()
    {
        return user_admin_config();
    }

    /**
     * 更新配置.
     *
     * @param string $key
     * @param string $value
     */
    protected function update($key, $value)
    {
        user_admin_config([$key => $value]);
    }
}
