<?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 Illuminate\Support\Arr;
use Modules\Cms\Models\Article;
use Modules\Cms\Models\Category;
use Modules\Cms\Models\Tag;

class ArticleController extends AdminController
{

    use WithUploads;

    protected $title = '文章管理';

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

        $grid->model()->with('categories')->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('slug', 'SLUG');
            });
            $filter->column(1 / 3, function (Grid\Filter $filter) {
                $filter->like('categories.title', '所属分类');
                $filter->like('tags.name', '标签');
            });
            $filter->column(1 / 3, function (Grid\Filter $filter) {
                $filter->equal('status', '状态')->select(Article::getStatusMap());
            });
        });

        $grid->column('id', '#ID#');
        $grid->column('categories', '所属分类')->display(function ($categories) {
            return Arr::pluck($categories, 'title');
        })->label();
        //        $grid->column('tags')->pluck('name')->label('warning');
        $grid->column('title', '文章标题');
        //        $grid->column('slug', '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 Article());

        $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('categories', '所属分类')
             ->options(Category::selectOptions(function ($model) {
                 return $model->where('status', 1);
             }, '请选择文章分类'))
             ->required();

//        $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;
    }

}