<?php

namespace App\Admin\Controllers\ArticleManagement;

use App\Models\Article;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class ArticleController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '文章列表';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Article());
        $grid->model()->orderBy('id','desc');
        $grid->column('id', __('Id'));
        $grid->column('type', __('分类'))->using([1=>'健康科普',2=>'疫苗科普',3=>'常见问题']);
        $grid->column('title', __('标题'))->limit(10,'...')->width(100);
        $grid->column('content', __('内容'))->limit(20,'...')->width(200);
        $grid->column('banner_url', __('图片'))->lightbox(['width' =>'', 'height' => 30]);
        $grid->column('created_at', __('创建时间'));
        $grid->column('updated_at', __('更新时间'));
        $grid->filter(function($filter){

            // Remove the default id filter
            $filter->disableIdFilter();

            // Add a column filter
            $type = [''=>'全部'];
            $type = array_merge($type,Article::$_post_type);
            $filter->equal('type', '类别')->select(Article::$_post_type);

        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Article::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('type', __('分类'));
        $show->field('title', __('标题'));
        $show->field('content', __('内容'));
        $show->field('banner_url', __('图片'))->image('',100,100);
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Article());

        $form->select('type', __('分类'))->options(Article::$_post_type)->default('1');
        $form->text('title', __('标题'))->rules('required|min:3|max:255',['required'=>'请填写标题','min'=>'标题字符不能少于3个','max'=>'标题长度过长']);
        $form->editor('content', __('内容'))->rules('required|min:3',['required'=>'请填写标题','min'=>'标题字符不能少于3个']);
        $form->image('banner_url', __('图片'))->rules('required' ,['required'=>'请选择图片！']);

        return $form;
    }
}
