<?php

namespace App\Http\Controllers\Fpdx;

use App\Models\Fpdx\FormAnswerModel;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Core\Auth;
use App\Models\Fpdx\FormModel;
use App\Models\Fpdx\FormQuestionModel;

class FormController extends Controller
{
    //
    public function show(Request $request)
    {
        $uid = Auth::auth();

        $form = FormModel::when($request->filled('id'), function ($query) use ($request) {
            return $query->where('id', $request->id);
        }, function ($query) use ($request) {
            return $query->where('stage_id', $request->stage_id)->where('activity_type', '72h');
        })->firstOrFail();
        if ($form->showed_at > time()) {
            return response([
                'code' => 400,
                'message' => '表单未到开放时间'
            ]);
        }

        // 获取问题列表
        $questions = FormQuestionModel::where('form_id', $form->id)->where('deleted_at', 0)->orderBy(
            'sort',
            'asc'
        )->get();
        foreach ($questions as $question) {
            switch ($question->type) {
                case "checkbox":
                case "radio":
                    $question->options = json_decode($question->options, true);
                    break;
                case "rate":
                    $question->options = (int)$question->options;
                    break;
            }
        }
        $form->questions = $questions;

        if (FormAnswerModel::where('form_id', $form->id)->where('uid', $uid)->first()) {
            $form->submit = 1;
        } else {
            $form->submit = 0;
        }

        return response([
            'code' => 200,
            'message' => 'OK',
            'data' => $form
        ]);
    }


    public function submit(Request $request)
    {
        $this->validate($request, [
            'form_id' => 'required|integer',
            'answers' => 'required|array',
        ]);
        $uid = Auth::auth();
        if (FormAnswerModel::where('form_id', $request->form_id)->where('uid', $uid)->first()) {
            return response([
                'code' => 410,
                'message' => "您已提交过，不能重复提交"
            ]);
        }

        $answers = $request->input('answers');

        // 提交答案
        \DB::beginTransaction();
        $questions = FormQuestionModel::where('form_id', $request->form_id)->where('deleted_at', 0)->get();
        foreach ($questions as $question) {
            // 检测是否必填
            if (!isset($answers[$question->sort])) {
                if ($question->required) {
                    \DB::rollBack();
                    return ['code' => 40002, 'message' => "问题{$question->sort}"];
                } else {
                    continue;
                }
            }
            // 答案类型
            switch ($question->type) {
                case "rate":
                case "input":
                case "textarea":
                case "radio":
                    $answer = $answers[$question->sort];
                    break;
                case "checkbox":
                    $answer = json_encode($answers[$question->sort], JSON_UNESCAPED_UNICODE);
                    break;
            }
            FormAnswerModel::create([
                'uid' => $uid,
                'form_id' => $request->form_id,
                'question_id' => $question->id,
                'type' => $question->type,
                'answer' => $answer
            ]);
        }
        \DB::commit();
        return ['code' => 200, 'message' => 'OK'];
    }
}
