<?php

namespace App\Admin\Controllers\Config;

use App\Base_Area;
use App\Models\SystemConfig;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

/**
 * @author pxwei
 * Class ConfigController
 * @package App\Admin\Controllers\City
 */
class ConfigController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '配置管理';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new SystemConfig);

        $grid->disableCreateButton(false);
        $grid->column('id', __('ID'))->sortable();
        $grid->column('group', __('分组'))->editable();
        $grid->column('key', __('键'))->editable();
        $grid->column('value', __('值'))->display(function (){
            if (strlen($this->value)>30){
                return mb_substr($this->value,0,20).'...';
            }
            return $this->value;
        })->modal('值',function (){
            return $this->value;
        })->copyable();

        $grid->column('chinese', __('中文名'))->editable();
        $grid->column('fieldType', __('类型'))->editable('select',SystemConfig::getType());
        $grid->column('comment', __('说明'))->editable();
        $grid->column('sort', __('排序'))->editable()->sortable();
        $grid->column('updated_at', __('更新时间'))->sortable();

        $grid->filter(function (Grid\Filter $filter){
            $filter->disableIdFilter();
            $filter->column(1/2, function ($filter) {
                $filter->like('group','分组');
                $filter->like('key','键');

            });
            $filter->column(1/2, function ($filter) {
                $filter->like('value','值');

            });



        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed   $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(SystemConfig::findOrFail($id));

        $show->field('id', __('ID'));
        $show->field('group', __('分组'));
        $show->field('key', __('key'));
        $show->field('value', __('value'));
        $show->field('fieldType', __('类型'))->using(SystemConfig::getType());
        $show->field('chinese', __('标签名称'));
        $show->field('comment', __('说明'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new SystemConfig);
        $form->display('id', __('ID'));
        $form->text('group', __('group/分组'));
//        $form->select('group', __('group/分组'))->options(SystemConfig::getGroup());
        $form->text('key', __('key/键'));
        $form->text('chinese', __('Label/标签名词'))->help('中文标签名词，方便用户查看');
        $form->number('sort', __('Sort/排序'))->help('排序，越大越前')->default(0);
        $form->select('fieldType', __('Type/字段类型'))->options(SystemConfig::getType());
        $form->text('comment', __('comment/配置描述'));
        $form->textarea('value', __('value/内容'));
        $form->display('created_at', __('Created At'));
        $form->display('updated_at', __('Updated At'));

        return $form;
    }
}
