<?php

namespace App\Http\Controllers\Admin\Game;

use App\Models\Game\CycAnserModel;
use App\Models\Game\CycQuestionModel;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class CycController extends Controller
{


    /**
     * 列表
     * @param  Request  $request
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     */
    public function index(Request $request)
    {
        $questions = CycQuestionModel::get();
        return response([
            'code' => 200,
            'message' => 'OK',
            'data' => $questions
        ]);
    }


    /**
     * 展示某一个
     * @param  Request  $request
     * @param $id
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     */
    public function show(Request $request, $id)
    {
        $question = CycQuestionModel::find($id);
        return response([
            'code' => 200,
            'message' => 'OK',
            'data' => $question
        ]);
    }


    /**
     * 创建
     * @param  Request  $request
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     */
    public function store(Request $request)
    {
        $this->validate($request, [
            'title' => 'required|max:64',
            'questions' => 'array',
            'count' => 'integer',
            'rate' => 'integer',
            'showed_at' => 'integer',
        ]);
        $question = CycQuestionModel::create($request->all());
        return response([
            'code' => 200,
            'message' => 'OK',
            'data' => $question
        ]);
    }


    /**
     * 修改
     * @param  Request  $request
     * @param $id
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     */
    public function update(Request $request, $id)
    {
        $this->validate($request, [
            'title' => 'max:64',
            'questions' => 'array',
            'answers' => 'array',
            'count' => 'integer',
            'rate' => 'integer'
        ]);
        $question = CycQuestionModel::find($id);
        $question->update($request->all());
        return response([
            'code' => 200,
            'message' => 'OK',
            'data' => $question
        ]);
    }


    /**
     * 问题的答案列表
     * @param  Request  $request
     * @param $qurstion_id
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     */
    public function answers(Request $request, $qustion_id)
    {
        $answers = CycAnserModel::where('question_id', $qustion_id)->get();
        return response([
            'code' => 200,
            'message' => 'OK',
            'data' => $answers
        ]);
    }


    /**
     * 创建答案
     * @param  Request  $request
     * @param $question_id
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     */
    public function createAnswer(Request $request, $question_id)
    {
        $this->validate($request, [
            'keyword' => 'required|max:16',
            'content' => 'required|max:64'
        ]);
        $answer = CycAnserModel::create([
            'question_id' => $question_id,
            'keyword' => $request->input('keyword'),
            'content' => $request->input('content')
        ]);
        return response([
            'code' => 200,
            'message' => 'OK',
            'data' => $answer
        ]);
    }


    /**
     * 修改答案
     * @param  Request  $request
     * @param $question_id
     * @param $answer_id
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     */
    public function updateAnswer(Request $request, $question_id, $answer_id)
    {
        $this->validate($request, [
            'content' => 'required|max:64'
        ]);
        $answer = CycAnserModel::find($answer_id);
        $answer->fill($request->all());
        $answer->save();
        return response([
            'code' => 200,
            'message' => 'OK',
            'data' => $answer
        ]);
    }


    /**
     * 删除答案
     * @param  Request  $request
     * @param $id
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     * @throws \Exception
     */
    public function delAnswer(Request $request, $question_id, $answer_id)
    {
        CycAnserModel::delete($answer_id);
        return response([
            'code' => 200,
            'message' => 'OK',
        ]);
    }
}
