<?php

namespace App\Distributor\Controllers;

use App\Distributor\Repositories\DistAdminUsersSetting;
use App\Distributor\Repositories\DistProductCategory;

use App\Libraries\CommonHelper;
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 AdminDistController
{

    protected function title()
    {
        return admin_trans('admin.proudct_category');
    }

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

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


    /**
     * 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->number('order')
                ->default(100)
                ->rules('numeric')
                ->help(admin_trans_label('order_tips')); // 添加备注
            $form->switch('enabled')->default(1);
            //SEO
            $form->radio('visibility')->when(1,function (Form $form) {
                $form->text('seo_title');
                $form->text('seo_keywords');
                $form->textarea('seo_description');
                $form->text('slug',admin_trans_label('url_and_handle'))->width(9, 2)->prepend(getSiteDomain().config('dictionary.site_url.product_category').'/');
            })->options(admin_trans_array(config('dictionary.visibility')))
                ->value(DistAdminUsersSetting::getValue('visibility'));
            //隐藏字段
            $form->hidden('dist_id'); // 隐藏dist_id字段,用于保存
            $form->ignore(['visibility']);
            //新建时插入JS
            $form->creating(function (Form $form) {
                CommonHelper::seoReplace('name','productCategory');
            });
            //提交前
            $form->submitted(function (Form $form) {
                //保存可见性配置
                DistAdminUsersSetting::setValue('visibility', $form->input('visibility'));
            });
            // 在保存时写死dist_id
            $form->saving(function (Form $form) {
                $form->dist_id =getDistributorId();
                //slug配置是否重复
                $id = $form->getKey();
                if ($form->slug != '') {
                    if (DistProductCategory::isRepeatedSlug($id, $form->slug)) {
                        //slug重复
                        return $form->response()->error(admin_trans_label('url_repeated'));
                    }
                }
                //当slug为空时生成slug
                if ($form->slug == '') {
                    $form->slug = DistProductCategory::generateSlug($form->name, $id);
                }
                //seo_title不能为空
                if ($form->seo_title == '') {
                    $form->seo_title = $form->name;
                }
            });
        });
    }
}
