<?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()
    {
        return Grid::make(new DistProductCategory(), 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('order');
            $grid->column('enabled')->switch();
            $grid->column('created_at');
            $grid->column('updated_at');//->sortable();
            $grid->disableViewButton();
            $grid->showQuickEditButton();
            $grid->enableDialogCreate();
            $grid->disableEditButton();
            //弹窗大小
            $grid->setDialogFormDimensions('830px','670px');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new DistProductCategory(), function (Form $form) {
            $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->textarea('remark')->rows(2);
            //SEO
            $form->text('seo_title');
            $form->text('seo_keywords');
            $form->text('seo_description');
            $form->text('slug',admin_trans_label('url_and_handle'))->width(9, 2)->prepend(getSiteDomain().config('dictionary.site_url.product_category').'/');
            $form->number('order')
                ->default(0)
                ->rules('numeric');
            $form->switch('enabled')->default(1);
            //隐藏字段
            $form->hidden('dist_id'); // 隐藏dist_id字段,用于保存
            //新建时插入JS
            $form->creating(function (Form $form) {
                CommonHelper::seoReplace('name','productCategory');
            });
            // 在保存时写死dist_id
            $form->saving(function (Form $form) {
                $form->dist_id =getDistributorId();
                //slug配置是否重复
                $id = $form->getKey();
                $form->slug=trim($form->slug);
                if ($form->slug != '') {
                    //把slug中的空格替换成-，多个空格在一起只用一个-，大写字母转换成小写
                    $form->slug = preg_replace('/\s+/', '-', $form->slug);
                    $form->slug = strtolower($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;
                }
            });
        });
    }
}
