<?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(admin_trans( 'admin.proudct_category'))
            ->description('')
            ->breadcrumb(['text'=>'list','url'=>''])
            ->body($this->grid());
    }


    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(BaseProductCategory::with('baseProductParameter'), function (Grid $grid) {
            $grid->view('admin.grid-custom.table');
            //默认分页条数
            $grid->paginate(config('admin.per_page'));
            $grid->column('id')->display(function () {
                return $this->_index+1;
            });
            $grid->column('name')->display(function ($name) {
                $level = $this->level; // 获取当前行的 level 字段值
                $prefix = str_repeat('&nbsp; ', $level * 2). ' <i class="fa fa-angle-right"></i>&nbsp; '; // 生成 level * 3 个加号
                return $prefix . ' ' . html_entity_decode($name); // 拼接加号和名称
            });;
            $grid->column('parent_id');
            $grid->column('baseProductParameter.name',admin_trans_label('attribute_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->like('name')->width(2);
                $filter->equal('enabled', admin_trans_label('enabled'))->select(admin_trans_array(config('dictionary.enabled')))->width(2);
            });
            //工具栏
            $grid->disableViewButton();
            $grid->showQuickEditButton();
            $grid->enableDialogCreate();
            $grid->disableEditButton();

            $grid->disablePerPages();
            //$grid->quickSearch('name');
        });

    }


    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new BaseProductCategory(), function (Form $form) {
            $form->select('parent_id', admin_trans_field('parent'))
                ->options(BaseProductCategory::selectMainOptions())
                ->saving(function ($v) {
                    return (int) $v;
                });
            $form->text('name')->required();
            $form->select('parameter_id', admin_trans_field('attribute_name'))->options(BaseProductParameter::selectOptions())->required();
            $form->textarea('remark')->rows(2);
            $form->number('order')
                ->default(0)
                ->rules('numeric');
            $form->switch('enabled')->default(1);
          //  $form->display('created_at');
          //  $form->display('updated_at');
        });
    }
}
