<?php

namespace App\Distributor\Controllers;

use App\Distributor\Repositories\DistAdminUsersSetting;
use App\Distributor\Repositories\DistVideo;
use App\Distributor\Repositories\DistVideoCategory;
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;

class DistVideoCategoryController extends AdminDistController
{
    /**
     * page index
     */
    public function index(Content $content)
    {
        return $content
            ->header(admin_trans('admin.video_category'))
            ->breadcrumb(['text'=>'Video Category','url'=>''])
            ->body($this->grid());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new DistVideoCategory(), function (Grid $grid) {
            //默认分页条数
            $grid->paginate(config('admin.per_page'));
            $grid->column('id')->display(function () {
                return $this->_index+1;
            });
            $grid->column('name')->width('20%');
            //$grid->column('parent_id');
            $grid->column('order');
            $grid->column('enabled')->switch();
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
            //$grid->quickSearch('name');
            //按钮
            $grid->disableViewButton();
            $grid->showQuickEditButton();
            $grid->enableDialogCreate();
            $grid->disableEditButton();
            //权限与排序
            $grid->model()->where('dist_id', getDistributorId());//这里限不了，改为仓库限制
            $grid->model()->orderBy('order', 'desc')->orderBy('id', 'desc');
            //弹窗大小
            $grid->setDialogFormDimensions('830px','670px');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new DistVideoCategory(), function (Form $form) {
            //$form->display('id');
            $form->text('name')->required();
            //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.video_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','videoCategory');
            });
            //保存前
            $form->saving(function (Form $form) {
                $form->dist_id =getDistributorId();//保存时直接写死dist_id
                $id = $form->getKey();
                //slug配置是否重复
                $form->slug=trim($form->slug);
                if ($form->slug != '') {
                    //把slug中的空格替换成-，多个空格在一起只用一个-，大写字母转换成小写
                    $form->slug = preg_replace('/\s+/', '-', $form->slug);
                    $form->slug = strtolower($form->slug);
                    if (DistVideoCategory::isRepeatedSlug($id, $form->slug)) {
                        //slug重复
                        return $form->response()->error(admin_trans_label('url_repeated'));
                    }
                }
                //当slug为空时生成slug
                if ($form->slug == '') {
                    $form->slug = DistVideoCategory::generateSlug($form->name, $id);
                }
                //seo_title不能为空
                if ($form->seo_title == '') {
                    $form->seo_title = $form->name;
                }
            });

        });
    }
}
