<?php

namespace App\Admin\Controllers;

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

class BaseProductParameterController extends AdminController
{
    public function index(Content $content)
    {
        return $content
            ->header(admin_trans( 'admin.product_attribute'))
            ->description('')
            ->breadcrumb(['text'=>'list','url'=>''])
            ->body($this->grid());
    }


    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new BaseProductParameter(), function (Grid $grid) {
            //默认分页条数
            $grid->paginate(config('admin.per_page'));
            $grid->column('id')->display(function () {
                return $this->_index+1;
            });
            $grid->column('name');
            $grid->column('order');
            $grid->column('enabled')->switch();
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
            // 允许筛选
            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->expand();
                $filter->equal('name')->width(2);
                $filter->equal('enabled', )->select(admin_trans_array(config('dictionary.enabled')))->width(2);
            });
            //按钮
            $grid->disableViewButton();
            $grid->showQuickEditButton();
            $grid->enableDialogCreate();
            $grid->disableEditButton();
            //$grid->quickSearch('name');
            //排序
            $grid->model()->orderBy("order",'desc')->orderBy('id', 'desc');
        });
    }


    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new BaseProductParameter(), function (Form $form) {
            $form->text('name')->required();
            $form->table('content',admin_trans_label('attribute'), function (NestedForm $table) {
                $table->text('key')->required();
                $table->text('value');
            })->setLabelClass('control-label')->default([
                ['key' => '', 'value' => ''],
            ]);
            $form->number('order')
                ->default(0)
                ->rules('numeric');
            $form->switch('enabled')->default(1);
        });
    }
}
