<?php

namespace App\Distributor\Controllers;

use App\Distributor\Repositories\SitePages;
use App\Models\SitePages as Model;
use App\Distributor\Repositories\SitePagesTag;
use App\Libraries\CommonHelper;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Models\Permission;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Admin;

class SitePagesController extends AdminDistController
{
    /**
     * page index
     */
    public function index(Content $content)
    {
        return $content
            ->header('列表')
            ->description('全部')
            ->breadcrumb(['text'=>'列表','url'=>''])
            ->body($this->grid());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        // 检查是否支持 pcntl 扩展
        if (!function_exists('pcntl_fork')) {
            die('pcntl_fork() is not available on this system.');
        } else {
            die('pcntl_fork() supported on this system.');
        }
        exit;
        return Grid::make(SitePages::with(['pagesTag']), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('title');
            $grid->column('pagesTag',admin_trans_label('tags'))->pluck('name')->label();
            $grid->column('cover_image')->display(function ($image) {
                // 开始生成 HTML
                $dataImages = [$image];
                return CommonHelper::displayImage($dataImages,100);
            });
            $grid->column('post_date');
            $grid->column('status')->using(config('dictionary.pages_status'));
            $grid->column('updated_at')->sortable();
            $grid->column('created_at');
            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->expand();
                $filter->like('title')->width(2);
            });
            //权限与条件
            $grid->model()->where('dist_id', getDistributorId())->orderBy('id', 'desc');
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, Model::with(['pagesTag']), function (Show $show) {
            $pagesTag  = $show->model()->pagesTag->toArray();
            $show->field('id');
            $show->field('title');
            $show->field('pagesTag',admin_trans_label('tags'))->as(function ($value) use ($pagesTag) {
                return array_column($pagesTag,'name');
            })->label();
            $show->field('author');
            $show->field('post_date');
            $show->field('cover_image')->as(function ($image) {
                // 开始生成 HTML
                $dataImages = [$image];
                return CommonHelper::displayImage($dataImages,150);
            })->unescape();
            $show->field('content')->unescape();
            $show->field('status')->using(config('dictionary.pages_status'));
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(SitePages::with(['pagesTag']), function (Form $form) {
            $form->text('title')->required();
            $form->text('author');
            $form->date('post_date')->format('YYYY-MM-DD')->required();
            $form->tags('pagesTag', '文章标签')
                ->setView('admin.form_custom.tags')
                ->pluck('name', 'name') // name 为需要显示的 Tag 模型的字段，id 为主键
                ->options(SitePagesTag::all())
                ->saving(function ($value) {
                    $value = SitePagesTag::handleTags($value);
                    return $value;
                });
            $form->image("cover_image")
                ->autoUpload()
                ->uniqueName()
                ->accept(config('admin.upload.oss_image.accept'))
                ->maxSize(config('admin.upload.oss_image.max_size'))
                ->dir('images/pages/'.date("Ymd"));
            $form->editor('content')->required();
            $form->switch('status',admin_trans_label('publish'))->default(1);
            $form->hidden('slug');
            $form->hidden('dist_id');
            //保存前，强制写死dist_id
            $form->saving(function (Form $form) {
                //$form->pagesTag = [1,2];
                $form->dist_id = getDistributorId();
                //生成slug
                if ($form->isCreating()) {
                    $form->slug = SitePages::generateSlug($form->title);
                }
            });
        });
    }
}
