<?php

namespace App\Admin\Controllers\Setting;

use App\Category;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class CategoryController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('类型配置');
            $content->description('列表');

            $content->row(function ($row) use ($content) {
                $row->column(6, Category::tree());
                $row->column(6, $this->form()->render());
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Category::class, function (Form $form) {
            $form->setAction(url('admin/setting/category'));

            $form->display('id', 'ID');
            $form->select('parent_id', '分类')->options(array_merge([0 => 'Root'], Category::getRoot()->toArray()));
            $form->text('title', '名称');

            $form->text('value', '值');

            $states = [
                'on' => ['value' => 'T', 'text' => '是', 'color' => 'success'],
                'off' => ['value' => 'F', 'text' => '否', 'color' => 'danger'],
            ];
            $form->switch('root', '是否是根目录')->states($states)->default('T');

            $form->display('created_at', 'Created At');
            $form->display('updated_at', 'Updated At');
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('类型配置');
            $content->description('编辑');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('类型配置');
            $content->description('创建');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Category::class, function (Grid $grid) {

            $grid->id('ID')->sortable();

            $grid->created_at();
            $grid->updated_at();
        });
    }
}
