<?php

namespace App\Admin\Controllers;

use App\Models\Article;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Illuminate\Http\Request;

class ArticleController extends Controller {
	use HasResourceActions;

	/**
	 * Index interface.
	 *
	 * @param Content $content
	 * @return Content
	 */
	public function index(Content $content) {
		return $content->header ( '文章' )->description ( '列表' )->body ( $this->grid () );
	}

	/**
	 * Show interface.
	 *
	 * @param mixed $id
	 * @param Content $content
	 * @return Content
	 */
	public function show($id, Content $content) {
		return $content->header ( '文章' )->description ( 'description' )->body ( $this->detail ( $id ) );
	}

	/**
	 * Edit interface.
	 *
	 * @param mixed $id
	 * @param Content $content
	 * @return Content
	 */
	public function edit($id, Content $content) {
		return $content->header ( '文章' )->description ( '编辑' )->body ( $this->form ()->edit ( $id ) );
	}

	/**
	 * Create interface.
	 *
	 * @param Content $content
	 * @return Content
	 */
	public function create(Content $content) {
		return $content->header ( '文章' )->description ( '新增' )->body ( $this->form () );
	}

	/**
	 * Make a grid builder.
	 *
	 * @return Grid
	 */
	protected function grid() {
		$grid = new Grid ( new Article () );
		$grid->model()->orderBy('created_at','desc');
		$grid->id ( 'ID' )->sortable ();

		$grid->head_img ( '主图' )->display ( function ($value) {
			if (isset ( $value )) {
				$imgUrl = env ( 'OSS_URL' ) . $value;
				return "<image style='max-height: 50px;' src='$imgUrl' />";
			}
		} );
		$grid->title ( '标题' );
		$grid->actions ( function ($actions) {
			$actions->disableView ();
		} );

		$grid->filter ( function ($filter) {
			$filter->like('title', '标题');
			// $filter->disableIdFilter ();
		} );
		$grid->disableExport();
		return $grid;
	}

	/**
	 * Make a show builder.
	 *
	 * @param mixed $id
	 * @return Show
	 */
	// protected function detail($id)
	// {
	// $show = new Show(Article::findOrFail($id));
	//
	// $show->id('Id');
	// $show->name('Name');
	// $show->image('Image');
	// $show->start_at('Start at');
	// $show->end_at('End at');
	// $show->created_at('Created at');
	// $show->updated_at('Updated at');
	//
	// return $show;
	// }

	/**
	 * Make a form builder.
	 *
	 * @return Form
	 */
	protected function form() {
		$form = new Form ( new Article () );		
		$form->text ( 'title', '标题' )->rules ( 'required' );
		$form->select('category_id','类型')->options(Article::$typeArr);
		$form->image ( 'head_img', '文章主图' );
		$form->UEditor ( 'content', '正文' );
		return $form;
	}
}
