<?php

namespace App\Admin\Controllers\Article;

use App\Admin\Selectable\CategorySelectAble;
use App\Models\Advert;
use App\Models\Article;
use App\Models\Category;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;

class IndexController extends AdminController
{

    protected $title = '内容管理';

    public function grid()
    {
        $grid = new Grid(new Article);
        $grid->model()->orderBy('created_at', 'desc');
        $grid->filter(function ($filter) {
            $filter->column(1 / 2, function ($filter) {
                $filter->like('title', '文章标题');
                $filter->equal('categories.id', '所属分类')
                    ->select(Category::selectOptions(function ($model) {
                        return $model->where('status', 1)
                            ->whereIn('type', [Category::TYPE_ARTICLE, Category::TYPE_SHOW, Category::TYPE_PERSON]);
                    }, '所有分类'));

            });

            $filter->disableIdFilter();
        });

        $grid->column('id', '#ID#');
        $grid->column('cover', '封面图片')->image('', 100);
        $grid->column('所属分类')->display(function () {
            return $this->categories()->pluck('title');
        })->label();
        $grid->column('title', '文章标题');
        $grid->column('sort', '序号')->editable();
        $states = [
            'on'  => ['value' => 1, 'text' => '打开', 'color' => 'primary'],
            'off' => ['value' => 2, 'text' => '关闭', 'color' => 'default'],
        ];
        $grid->column('status', '状态')->switch($states);
        $grid->column('created_at', '创建时间');

        return $grid;
    }

    public function form(): Form
    {
        $form = new Form(new Article);

        $form->text('title', '文章标题')->rules('min:2')->required();

        $form->select('position', '推荐')
            ->options(Article::POSITIONS);

        $form->belongsToMany('categories', CategorySelectAble::class, __('关联分类'))->required();

        $form->textarea('description', '内容简介');

        $form->text('duty', '职务')->help('领导班子需要添加此数据');
        $form->text('rank', '职称')->help('领导班子需要添加此数据');

        $form->image('cover', '封面')
            ->move('images/'.date('Y/m/d'))
            ->removable()
            ->uniqueName();

        $form->ueditor('content', '文章内容')->rules('required', ['required' => '详情不能为空']);
        $form->number('sort', '序号')->default(0)->rules('required', ['required' => '序号必须填写'])->help('倒序优先');
        $states = [
            'on'  => ['value' => 1, 'text' => '打开', 'color' => 'success'],
            'off' => ['value' => 0, 'text' => '关闭', 'color' => 'danger'],
        ];
        $form->datetime('created_at', '发布时间');

        $form->switch('status', '状态')->states($states)->default(1);
        $form->url('url', '外链地址');

        //        $form->saved(function (Form $form) {
        //            $category_ids = request()->category_ids;
        //            if ($category_ids) {
        //                $categories = [];
        //                foreach ($category_ids as $key => $category_id) {
        //                    if ($category_id) {
        //                        $form->model()->article_categories()->updateOrCreate([
        //                            'category_id' => $category_id,
        //                        ]);
        //                        $categories[] = $category_id;
        //                    }
        //                }
        //                $form->model()->article_categories()->whereNotIn('category_id', $categories)->delete();
        //            }
        //        });

        return $form;
    }

}
