<?php

namespace Modules\Cms\Http\Controllers\Admin;

use App\Admin\Traits\WithUploads;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Modules\Cms\Models\Page;
use Modules\Cms\Models\Tag;

class PageController extends AdminController
{

    use WithUploads;

    protected $title = '单页管理';

    public function grid(): Grid
    {
        $grid = new Grid(new Page());

        $grid->model()->withCount('versions');

        $grid->filter(function (Grid\Filter $filter) {
            $filter->scope('trashed', '回收站')->onlyTrashed();

            $filter->column(1 / 3, function (Grid\Filter $filter) {
                $filter->like('title', '页面标题');
                $filter->like('tags.name', '标签');
            });
            $filter->column(1 / 3, function (Grid\Filter $filter) {
                $filter->like('slug', 'SLUG');
            });
            $filter->column(1 / 3, function (Grid\Filter $filter) {
                $filter->equal('status', '状态')->select(Page::getStatusMap());
            });
        });

        $grid->column('id', '#ID#');
        $grid->column('title', '页面标题');
        $grid->column('tags')->pluck('name')->label('warning');
        $grid->column('slug', '英文别名');
        $grid->column('status', '状态')->bool();
        $grid->column('clicks', '浏览量');
        $grid->column('versions_count', '历史版本')->link(function () {
            return route('admin.cms.versions', [
                'model' => get_class($this),
                'key'   => $this->id,
            ]);
        }, '_self');
        $grid->column('created_at', '创建时间');

        return $grid;
    }

    /**
     * Notes   : 内容表单
     * @Date   : 2021/3/15 5:21 下午
     * @Author : < Jason.C >
     * @return \Encore\Admin\Form
     */
    public function form(): Form
    {
        $form = new Form(new Page());

        $form->text('title', '页面标题')
             ->rules([
                 'required',
                 'max:255',
             ], [
                 'max' => '标题最大长度不能超过 :max 个字符',
             ])
             ->required();
        $form->text('sub_title', '副标题')
             ->rules([
                 'nullable',
                 'max:255',
             ], [
                 'max' => '标题最大长度不能超过 :max 个字符',
             ]);
        $form->text('slug', '英文别名')
             ->rules([
                 'nullable',
                 'alpha_dash',
             ], [
                 'alpha_dash' => '别名中包含非法字符',
             ])
             ->help('字母、数字、下划线组成，用来展示简短的URL');

        $form->multipleSelect('tags', '页面标签')
             ->options(function () {
                 return Tag::pluck('name', 'id');
             });

        $this->cover($form);

        $this->pictures($form);

        $form->textarea('description', '页面简介')
             ->rules([
                 'nullable',
                 'max:255',
             ], [
                 'max' => '标题最大长度不能超过 :max 个字符',
             ]);

        $form->ueditor('content', '页面内容')
             ->rules([
                 'required',
             ], [
                 'required' => '页面内容不能为空',
             ])
             ->required();

        $this->attachments($form);

        $form->switch('status', '状态')
             ->default(1);

        return $form;
    }

}