<?php

namespace App\Admin\Controllers;

use App\Model\Option;
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;

class OptionController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('首页')
            ->description('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('description')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('创建')
            ->description('description')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Option);

        $grid->option_id('选项ID');
        $grid->title('标题');
        $grid->question_id('问题ID');
        $grid->is_answer('是否答案');
        $grid->created_at('创建时间');
        $grid->updated_at('更新时间');
        $grid->deleted_at('删除时间');

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Option::findOrFail($id));

        $show->option_id('选项ID');
        $show->title('标题');
        $show->question_id('问题ID');
        $show->is_answer('是否答案');
        $show->created_at('创建时间');
        $show->updated_at('更新时间');
        $show->deleted_at('删除时间');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Option);

        $form->number('option_id', '选项ID');
        $form->text('title', '标题');
        if(request('question_id')){
            $form->text('question_id','问题ID')->attribute('readonly','readonly')->value(request('question_id'));
        }else{
            $form->text('question_id', '问题ID');
        }
        $form->switch('is_answer', '是否答案');

        return $form;
    }
}
