<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Response\ResponseJson;
use App\Models\OrderSurvey;
use App\Models\Survey;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class SurveyController extends Controller
{
    use ResponseJson;

    /**
     * 问卷调查列表
     * @param Request $request
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Http\JsonResponse
     */
    public function surveys(Request $request)
    {
        $surveys = Survey::keyword()->orderByDesc('id')->paginate();
        return $this->success('ok', $surveys);
    }

    /**
     * 创建问卷调查
     * @param Request $request
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Http\JsonResponse
     */
    public function storeSurveys(Request $request)
    {
        $title = $request->input('title');
        if (empty($title)) return $this->failure("请输入问卷调查标题");
        $content = $request->input('content', []);
        if (empty($content) || count($content) == 0) return $this->failure("请输入问卷调查内容");
        $note = $request->input('note');
        DB::beginTransaction();
        $survey = Survey::create(['title'=>$title, 'content'=> json_encode($content), 'note'=>$note]);
        $order_id = $request->input('order_id');
        if ($order_id) {
            OrderSurvey::create(['survey_id'=>$survey->id, 'order_id'=>$order_id]);
        }
        DB::commit();
        return $this->success('ok');
    }

    /**
     * 修改问卷调查
     * @param Request $request
     * @param Survey $survey
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Http\JsonResponse
     */
    public function updateSurveys(Request $request, Survey $survey)
    {
        if ($request->has('title') ) {
            $survey->title = $request->title;
        }
        if ($request->has('content') && is_array($request->input('content'))) {
            $survey->content =  json_encode($request->input('content'));
        }
        if ($request->has('note') ) {
            $survey->note = $request->note;
        }
        $survey->save();
        return $this->success('ok');
    }

    /**
     * 问卷调查详情
     * @param Request $request
     * @param Survey $survey
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Http\JsonResponse
     */
    public function survey(Request $request, Survey $survey)
    {
        return $this->success('ok', $survey);
    }

    /**
     * 删除问卷调查
     * @param Request $request
     * @param Survey $survey
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Http\JsonResponse
     */
    public function deleteSurvey(Request $request, Survey $survey)
    {
        $survey->delete();
        return $this->success('ok');
    }

    /**
     * 绑定订单
     * @param Request $request
     * @param Survey $survey
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Http\JsonResponse
     */
    public function bindOrderSurvey(Request $request, Survey $survey)
    {
        $order_id = $request->order_id;
        if (empty($order_id)) return $this->failure("请选择订单");
        OrderSurvey::firstOrCreate(['survey_id'=>$survey->id, 'order_id'=>$order_id]);
        return $this->success('ok');
    }

    /**
     * 解绑订单
     * @param Request $request
     * @param OrderSurvey $order_survey
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Http\JsonResponse
     */
    public function unbindOrderSurvey(Request $request, OrderSurvey $order_survey)
    {
        $order_survey->delete();
        return $this->success('ok');
    }

    /**
     * 订单列表
     * @param Request $request
     * @param Survey $survey
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Http\JsonResponse
     */
    public function orderSurveys(Request $request, Survey $survey)
    {
        $order_surveys = $survey->orderSurveys()->with('order')->keyword()->orderByDesc('id')->paginate();
        return $this->success('ok', $order_surveys);
    }

    /**
     * 订单问卷详情
     * @param Request $request
     * @param OrderSurvey $order_survey
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Http\JsonResponse
     */
    public function orderSurvey(Request $request, OrderSurvey $order_survey)
    {
        return $this->success('ok', $order_survey);
    }
}
