<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\BaseProductCategory;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Layout\Content;
use App\Admin\Repositories\BaseProductParameter;

class BaseProductCategoryController extends AdminController
{

    public function index(Content $content)
    {
        return $content
            ->header('Product Category')
            ->description('')
            ->body($this->grid());
    }


    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(BaseProductCategory::with('baseProductParameter'), function (Grid $grid) {
            $grid->column('id','ID')->bold()->sortable();
            $grid->column('name')->tree(true,true);
            $grid->column('parent_id');
            $grid->column('baseProductParameter.name','Parameter Name');
            $grid->column('order')->orderable();
            $grid->column('enabled')->switch();
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
            //过滤器
            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->expand();
                $filter->like('name')->width(2);
                $filter->equal('enabled', 'Enabled')->select(config('dictionary.enabled'))->width(2);
            });
            //工具栏
            $grid->disableViewButton();
            $grid->showQuickEditButton();
            $grid->enableDialogCreate();
            $grid->disableEditButton();
            //$grid->quickSearch('name');
        });

    }


    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new BaseProductCategory(), function (Form $form) {
            $form->display('id', 'ID');
            $form->select('parent_id', trans('Parent'))
                ->options(BaseProductCategory::selectOptions())
                ->saving(function ($v) {
                    return (int) $v;
                });
            $form->text('name')->required();
            $form->select('parameter_id', 'parameter')->options(BaseProductParameter::selectOptions())->required();
            $form->switch('enabled')->default(1);
          //  $form->display('created_at');
          //  $form->display('updated_at');
        });
    }
}
