<?php

namespace App\Http\Controllers\Admin\Pair;

use App\Exports\FormExport;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Fpdx\FormModel;
use App\Models\Fpdx\FormQuestionModel;
use Maatwebsite\Excel\Facades\Excel;

class FormController extends Controller
{

    /**
     * 表单信息
     * @param  Request  $request
     * @return array
     */
    public function show(Request $request, $id)
    {
        $form = FormModel::findOrFail($id);
        // 获取问题列表
        $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;
                default:
                    break;
            }
        }
        $form->questions = $questions;
        return response([
            'code' => 200,
            'message' => 'OK',
            'data' => $form
        ]);
    }


    /**
     * 表单列表
     * @param  Request  $request
     * @return array
     */
    public function index(Request $request)
    {
        $forms = FormModel::where('deleted_at', 0)
            ->orderBy('id', 'desc')
            ->paginate($request->get('pre_page', 20));
        return response([
            'code' => 200,
            'message' => 'OK',
            'data' => $forms
        ]);
    }

    /**
     * 创建表单
     * @param  Request  $request
     * @return array
     */
    public function store(Request $request)
    {
        $this->validate($request, [
            'title' => 'required|max:32',
            'stage_id' => 'required|integer',
            'activity_type' => 'in:72h,qbj',
        ]);
        $form = new FormModel();
        $form->fill($request->all());
        $form->save();
        return response([
            'code' => 200,
            'message' => 'OK',
            'data' => $form
        ]);
    }


    /**
     * 修改表单
     * @param  Request  $request
     * @return array
     */
    public function update(Request $request)
    {
        $this->validate($request, [
            'id' => 'required|integer',
            'stage_id' => 'integer',
            'activity_type' => 'in:72h,qbj',
            'title' => 'max:32',
            'describe' => 'max:100',
            'deleted_at' => 'integer',
            'showed_at' => 'integer',
        ]);
        $form = FormModel::findOrFail($request->id);
        $form->fill($request->all());
        $form->save();
        return response([
            'code' => 200,
            'data' => $form
        ]);
    }


    /**
     * 创建问题
     * @param  Request  $request
     * @return array
     */
    public function createQuestion(Request $request, $form_id)
    {
        $this->validate($request, [
            'question' => 'required|max:64',
            'type' => 'required|in:rate,input,textarea,radio,checkbox',
            'required' => 'required|bool',
            'sort' => 'required|integer',
        ]);
        $attributes = $request->all();
        $attributes['form_id'] = $request->form_id;
        switch ($attributes['type']) {
            case "checkbox":
            case "radio":
                $attributes['options'] = json_encode($attributes['options'], JSON_UNESCAPED_UNICODE);
                break;
            case "rate":
                $attributes['options'] = (int)$attributes['options'];
                break;
            default:
                break;
        }
        $question = FormQuestionModel::create($attributes);
        return response([
            'code' => 200,
            'data' => $question
        ]);
    }


    /**
     * 修改问题
     * @param  Request  $request
     * @return array
     */
    public function updateQuestion(Request $request, $form_id, $id)
    {
        $this->validate($request, [
            'form_id' => 'integer',
            'question' => 'max:64',
            'type' => 'in:rate,input,textarea,radio,checkbox',
            'required' => 'bool',
            'sort' => 'integer',
        ]);
        $form = FormModel::findOrFail($form_id);
        $question = FormQuestionModel::where('form_id', $form->id)->where('id', $id)->firstOrFail();

        $attributes = $request->only('form_id', 'sort', 'question', 'type', 'required', 'options', 'deleted_at');
        if ($request->filled('options')) {
            switch ($attributes['type']) {
                case "checkbox":
                case "radio":
                    $attributes['options'] = json_encode($attributes['options'], JSON_UNESCAPED_UNICODE);
                    break;
                default:
                    break;
            }
        }
        $question->update($attributes);
        return response([
            'code' => 200,
            'data' => $question
        ]);
    }


    public function export(Request $request, $form_id)
    {
        return Excel::download(new FormExport($form_id), "form_{$form_id}.xlsx");
    }
}
