<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Models\Question;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Layout\Content;
use App\Models\QuestionOrder;
use App\Models\Order;
use Illuminate\Http\Request;
use Encore\Admin\Facades\Admin;
class QuestionOrdersController extends Controller {
	use HasResourceActions;

	/**
	 * Index interface.
	 *
	 * @param Content $content
	 * @return Content
	 */
	public function index(Content $content) {
		return $content->header ( '咨询问答' )->description ( '列表' )->body ( $this->grid () );
	}




	/**
	 * Make a grid builder.
	 *
	 * @return Grid
	 */
	protected function grid() {
		$grid = new Grid ( new QuestionOrder () );
		if(Admin::user()->doctor_id){
			$grid->model ()->where('doctor_id',Admin::user()->doctor_id)->orderBy ( 'updated_at', 'desc' );
		}else{
			$grid->model ()->orderBy ( 'updated_at', 'desc' );
		}
		$grid->id ( 'ID' )->sortable ();
		$grid->no ( '编号' );
		$grid->has_new_ask ( '是否有新提问' )->display ( function ($title) {
			$question = Question::where ( 'order_no', $this->no )->orderBy ( 'created_at', 'desc' )->first ();
			if ($question && $question->type == 'ask') {
				return '是';
			} else {
				return '否';
			}
		} );
		$grid->created_at ( '创建时间' );
		$grid->updated_at ( '更新时间' );
		$grid->actions ( function ($actions) {
			// 去掉删除
			$actions->disableDelete ();

			// 去掉编辑
			$actions->disableEdit ();
		} );

		return $grid;
	}

	/**
	 * Make a show builder.
	 *
	 * @param mixed $id
	 * @return Show
	 */
	protected function detail($id) {
		$show = new Show ( Question::findOrFail ( $id ) );
		return $show;
	}

	/**
	 * Make a form builder.
	 *
	 * @return Form
	 */
	protected function form() {
		$form = new Form ( new Question () );
		$form->text ( 'title', '标题' )->required ();
		$form->ckeditor ( 'content' );
		$form->tools ( function (Form\Tools $tools) {
			// 去掉`删除`按钮
			$tools->disableDelete ();
			$tools->disableView ();
		} );

		// 去掉`重置`按钮
		$form->disableReset ();

		// 去掉`查看`checkbox
		$form->disableViewCheck ();

		// 去掉`继续编辑`checkbox
		$form->disableEditingCheck ();

		$form->disableCreatingCheck ();
		return $form;
	}
	public function show($id, Content $content) {
// 		dd($id);
		$order=QuestionOrder::find($id);
		return $content->header ( '查看咨询' )->
		// body 方法可以接受 Laravel 的视图作为参数
		body ( view ( 'admin.question_order.show', [
				'order' => $order
		] ) );
	}
	public function answer(Request $request){
		$data=$request->all();
		$data['type']='answer';
		$data['content_type']='word';
		Question::create($data);
		return redirect("admin/question_orders/{$request->order_id}");
	}
}
