<?php

namespace App\Admin\Controllers;

use App\Models\Leave;
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 LeaveController 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 Leave () );

		$grid->id ( 'ID' )->sortable ();
		$grid->child_name ( '孩子姓名' )->display ( function () {
			return $this->child?$this->child->realname:'此学员已被删除';
		} );
		$grid->reason ( '请假原因' );
		$grid->class_date ( '上课日期' );
		$grid->created_at ( '请假时间' );
		$grid->disableCreateButton ();		
		$grid->actions ( function ($actions) {
			$actions->disableView ();
			$actions->disableDelete();
			$actions->disableEdit();
		} );

		$grid->filter ( function ($filter) {

			// $filter->disableIdFilter ();
		} );

		return $grid;
	}

	/**
	 * Make a show builder.
	 *
	 * @param mixed $id
	 * @return Show
	 */
	// protected function detail($id)
	// {
	// $show = new Show(Leave::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 Leave () );		
		$form->text ( 'title', '标题' )->rules ( 'required' );
		$form->select('category_id','类型')->options(Leave::$typeArr);
		$form->image ( 'head_img', '请假主图' );
		$form->UEditor ( 'content', '正文' );
		return $form;
	}
}
