<?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;
use App\Models\Teacher;
use App\Models\Classes;

class TeacherController 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 Teacher() );
		$grid->model()->orderBy('id','desc');
		$grid->id ( 'ID' )->sortable ();
		$grid->realname ( '姓名' );
		$grid->mobile ( '手机号' );
		$grid->join_at ( '入职时间' );
		$grid->sex ( '性别' )->display ( function ($value) {
			return Teacher::$sexMap[$value];
		} );
		$grid->age ( '年龄' );
		$grid->actions ( function ($actions) {
			$actions->disableView ();
		} );

		$grid->filter ( function ($filter) {
			$filter->like ( 'realname', '姓名' );
			$filter->like ( 'mobile', '电话' );
			// $filter->disableIdFilter ();
		} );

		return $grid;
	}


	/**
	 * Make a form builder.
	 *
	 * @return Form
	 */
	protected function form() {
		$form = new Form ( new Teacher() );		
		$form->text ( 'realname', '姓名' )->rules ( 'required' );
		$form->mobile( 'mobile', '手机号' )->rules ( 'required' );
		$form->password( 'password', '密码' )->rules ( 'required' );
		$form->number( 'age', '年龄' )->rules ( 'required' );
		$form->radio('sex','性别')->options(Teacher::$sexMap);
		$form->datetime('join_at','入职时间');
		$form->checkbox('class','负责班级')->options(Classes::all()->pluck('name','id'));
		$form->saving ( function (Form $form) {
			$form->password=bcrypt($form->password);
		});
		return $form;
	}
}
