<?php

namespace App\Distributor\Controllers;

use App\Distributor\Repositories\DistAdminUsersSetting;
use App\Distributor\Repositories\SitePagesTag;
use App\Distributor\Repositories\SitePagesTagRelationship;
use App\Libraries\CommonHelper;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Admin;

class SitePagesTagController extends AdminDistController
{

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



        return $content
            ->header(admin_trans( 'admin.pages_tag'))
            ->description('')
            ->breadcrumb(['text'=>'','url'=>''])
            ->body($this->grid());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new SitePagesTag(), 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('seo_title')->width('20%');
            $grid->column('seo_keywords')->width('20%');
            $grid->column('created_at');
            //$grid->column('seo_description');
            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->expand();
                $filter->like('name')->width(2);
            });
            //按钮
            $grid->disableViewButton();
            $grid->showQuickEditButton();
            $grid->enableDialogCreate();
            $grid->disableEditButton();
            $grid->model()->where('dist_id', getDistributorId())->orderBy('id', 'desc');
            //弹窗大小
            $grid->setDialogFormDimensions('830px','670px');
        });
    }


    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new SitePagesTag(), function (Form $form) {
            $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.tags').'/');

            //$form->text('slug');
            $form->hidden('dist_id',);
            //新建时插入JS
            $form->creating(function (Form $form) {
                CommonHelper::seoReplace('name','pagesTag');
            });
            //保存前
            $form->saving(function (Form $form) {
                //强制写死dist_id
                $form->dist_id = getDistributorId();
                //slug配置是否重复
                $id = $form->getKey();
                if ($form->slug != '') {
                    if (SitePagesTag::isRepeatedSlug($id, $form->slug)) {
                        //slug重复
                        return $form->response()->error(admin_trans_label('url_repeated'));
                    }
                }
                //当slug为空时生成slug
                if ($form->slug == '') {
                    $form->slug = SitePagesTag::generateSlug($form->name, $id);
                }
                //seo_title不能为空
                if ($form->seo_title == '') {
                    $form->seo_title = $form->name;
                }
            });
            //删除后
            $form->deleted(function (Form $form, $result) {
                $data = $form->model()->toArray();
                $ids = array_column($data, 'id');
                //删除对应关系
                SitePagesTagRelationship::destroyBytagID($ids);
            });
        });
    }
}
