<?php

namespace App\Admin\Controllers;

use App\Model\Question;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use App\Model\Option;


class QuestionController 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')
            ->row($this->form()->edit($id))
            ->row(Admin::grid(Option::class, function (Grid $grid) use ($id) {
                $grid->setName('options')
                    ->setTitle('选项')
                    ->setRelation(Question::find($id)->options())
                    ->resource('/admin/option');
                $grid->option_id('选项ID');
                $grid->title('标题');
                $grid->is_answer('是否答案');
                $grid->created_at('创建时间');
                $grid->updated_at('更新时间');
            }));
    }

    /**
     * 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 Question);

        $grid->question_id('问题ID');
        $grid->title('标题');
        $grid->is_released('是否发布');
        $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(Question::findOrFail($id));

        $show->question_id('问题ID');
        $show->title('标题');
        $show->is_released('是否发布');
        $show->created_at('创建时间');
        $show->updated_at('更新时间');
        $show->deleted_at('删除时间');

        return $show;
    }

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

        $form->display('question_id', '问题ID');
        $form->text('title', '标题');
        $states = [
            'on'  => ['value' => 1, 'text' => '发布', 'color' => 'success'],
            'off' => ['value' => 0, 'text' => '关闭', 'color' => 'danger'],
        ];
        $form->switch('is_released', '是否发布')->states($states);

        return $form;
    }
}
