<?php

namespace App\Distributor\Controllers;

use App\Distributor\Repositories\DistProductCategory;

use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Admin;
use Illuminate\Support\Facades\Session;

class DistProductCategoryController extends AdminController
{



    /**
     * page index
     */
    public function index(Content $content)
    {

        return $content
            ->header(admin_trans('admin.proudct_category'))
            ->description('')
            ->body($this->grid());
    }

    /**
     * Edit interface.
     *
     * @param  mixed  $id
     * @param  Content  $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->translation($this->translation())
            ->title($this->title())
            ->description($this->description()['edit'] ?? trans('admin.edit'))
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param  Content  $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->translation($this->translation())
            ->title($this->title())
            ->description($this->description()['create'] ?? trans('admin.create'))
            ->body($this->form());
    }



    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
      //  var_dump(Session::get('distributor'));

        return Grid::make(new DistProductCategory(), function (Grid $grid) {

            //$grid->model()->where('dist_user_id', 1);//这里限不了，改为仓库限制

            $grid->column('id','ID')->bold();//->sortable();
            $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('level');
            $grid->column('parent_id');
            //$grid->column('distProductParameter.name',admin_trans_label('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->like('name');
//            });
            $grid->disableViewButton();
            $grid->showQuickEditButton();
            $grid->enableDialogCreate();
            $grid->disableEditButton();

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

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new DistProductCategory(), function (Form $form) {
            $form->display('id', 'ID');
            $form->select('parent_id', admin_trans_field('parent'))
                ->options(DistProductCategory::selectMainOptions(function ($query) {
                    // 在这里定义查询条件
                    $query->where('parent_id', 0);
                }))
                ->saving(function ($v) {
                    return (int) $v;
                });
            $form->text('name')->required();
            $form->text('order')
                ->default(0)
                ->rules('numeric')
                ->help(admin_trans_label('order_tips')); // 添加备注
            $form->switch('enabled')->default(1);
            $form->hidden('dist_id'); // 隐藏dist_id字段,用于保存
            // 在保存时进行验证
            $form->saving(function (Form $form) {
                $form->dist_id =getDistributorId();
            });
          //  $form->select('parameter_id', 'parameter')->options(DistProductParameter::selectOptions())->required();// DIST不需要绑定 parameter
          //  $form->display('created_at');
          //  $form->display('updated_at');
        });
    }
}
