<?php

namespace App\Admin\Controllers;

use App\Models\SystemConfig;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

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

        $grid->column('id')->sortable();
        $grid->column('group')->editable();
        $grid->column('key')->editable();
        $grid->column('value')->copyable()->width(150);
        $grid->column('chinese')->editable();
        $grid->column('fieldType')->select(SystemConfig::getType());
        $grid->column('comment')->editable()->width(150);
        $grid->column('updated_at')->sortable();
        $grid->quickSearch(function ($model, $query) {
            $model->where('group', 'like', "%{$query}%")
                ->orWhere('key', 'like', "%{$query}%")
                ->orWhere('value', 'like', "%{$query}%");
        });
        return $grid;
    }

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

        $show->field('id');
        $show->field('group');
        $show->field('key');
        $show->field('value');
        $show->field('fieldType')->using(SystemConfig::getType());
        $show->field('chinese');
        $show->field('comment');
        $show->field('created_at');
        $show->field('updated_at');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return \Encore\Admin\Form
     */
    protected function form()
    {
        $form = new Form(new SystemConfig);
        $form->display('id');
        $form->text('group');
        $form->text('key');
        $form->text('chinese');
        $form->select('fieldType')->options(SystemConfig::getType());
        $form->text('comment');
        $form->textarea('value');
        $form->display('created_at');
        $form->display('updated_at');

        $form->footer(function ($footer) {
            // 去掉`查看`checkbox
            $footer->disableViewCheck();
            // 去掉`继续编辑`checkbox
            $footer->disableEditingCheck();
            // 去掉`继续创建`checkbox
            $footer->disableCreatingCheck();
        });

        return $form;
    }
}
