<?php

namespace App\Http\Controllers\API\H5;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class QuestionController extends Controller
{
  public function GetList()
  {
    $hospital_id = request('hospital_id');
    $q_type = request('q_type');
    if (!isset($hospital_id)) return \Yz::echoError1("医院id不能为空");
    if (!isset($q_type)) return \Yz::echoError1("问卷类型不能为空");
    $list = DB::table('questions')
      ->where(['hospital_id'=>$hospital_id,
        'q_type'=>$q_type,
        'status'=>1,
        'is_del'=>0,
      ])->orderBy('order','asc')->get();
    foreach ($list as $key=>$item){
      $item->content=json_decode($item->content,true);
    }
    return \Yz::Return(true,"查询成功",['list'=>$list]);
  }
  //提交调查
  public function SubmitAnswer()
  {
    $openid = request('openid');
    $content = request('content');
    $q_type = request('q_type');

    $user = DB::table('web_users')->where(['openid' => $openid, 'status' => 1, 'is_del' => 0])->first();
    if (!$user) return \Yz::echoError1('用户不存在');
    if(!isset($q_type)) return \Yz::echoError1('问卷类型不能为空');
    if(!isset($content) or empty($content)) return \Yz::echoError1('内容不能为空');
    $data=[
      'userid'=>$user->id,
      'q_type'=>$q_type,
      'content'=>json_encode($content,JSON_UNESCAPED_UNICODE)
    ];
    $i=DB::table('questions_log')->insert($data);
    if($i){
      return \Yz::Return(true,"提交成功",[]);
    }else{
      return \Yz::echoError1('提交失败');
    }

  }
}
