<?php

namespace App\Admin\Controllers;

use App\Models\Children;
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\Classes;

class ChildrenController 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 Children () );
		$grid->model ()->orderBy ( 'created_at', 'desc' );
		$grid->id ( 'ID' )->sortable ();
		$grid->realname ( '姓名' );
		$grid->sex ( '性别' )->display ( function ($sex) {
			if($sex==1){
				$text='男';
			}elseif($sex==0){
				$text='女';
			}else{
				$text='未填写';
			}	
			return $text;
		} );
		$grid->endtime ( '到期时间' );
		$grid->classes ( '所属班级' )->display ( function ($classes) {
			$names=Classes::whereIn('id',$classes)->pluck('name');
			$classNames=implode ( ',', $names->toArray() );
			return $classNames;
		} );
		$grid->actions ( function ($actions) {
			$actions->disableView ();
		} );

		$grid->filter ( function ($filter) {
			$filter->like ( 'realname', '姓名' );
			// $filter->disableIdFilter ();
		} );
		$grid->disableCreateButton ();
		$grid->disableExport ();
		return $grid;
	}

	/**
	 * Make a show builder.
	 *
	 * @param mixed $id
	 * @return Show
	 */
	// protected function detail($id)
	// {
	// $show = new Show(Children::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 Children () );
		$form->text ( 'realname', '姓名' )->required ();
		$form->number ( 'age', '年龄' )->required ();
		$form->radio ( 'sex', '性别' )->options ( Children::$sexMap );
		$form->checkbox ( 'classes', '所属班级' )->options ( Classes::all ()->pluck ( 'name', 'id' ) );
		
		$form->datetime ( 'starttime', '入学时间' );
		$form->datetime ( 'endtime', '结束时间' );
		return $form;
	}
}
