<?php
namespace app\h5\controller;

use think\Controller;
use think\Db;
use think\Exception;

class Wj extends Controller
{

    public function index(){
        $id = input('id');
        if(!$id){
            return $this->fetch('h5/error');
        }else{
            $info = db('questionnaire')
                ->where('id',$id)
                ->find();
            if(!$info || $info['enable'] != 1){
                return $this->fetch('h5/error');
            }else{
                db('questionnaire')
                    ->where('id',$id)
                    ->setInc('view',1);
                $this->assign('info',$info);
                return $this->fetch('index/wj');
            }
        }
    }
    public function answer(){
        $id = input('id');
        $answers = input('answer');
        if($id <= 0 || !$answers){
            $this->error('参数错误');
        }
        $info = db('questionnaire')
            ->where('id',$id)
            ->find();
        if(!$info || $info['enable'] != 1){
            $this->error('问卷停止回收');
        }
        $curTime = date('Y-m-d H:i:s');
        $questions = json_decode($info['questions'],true);
        $qarr = [];
        foreach ($questions as $k=>$v){
            if($v['required'] && empty($answers[$v['id']])){
                $this->error('有必做题未完成');
            }
            if(($v['type'] == 'text'||$v['type'] == 'textarea') && $v['limit'] < mb_strlen($answers[$v['id']])){
                $this->error('答题字数超过限制');
            }

            if($v['type'] == 'checkbox'){
                $answer = empty($answers[$v['id']])?'':implode(',',$answers[$v['id']]);
            }else{
                $answer = empty($answers[$v['id']])?'':$answers[$v['id']];
            }
            $qarr[] = [
                'create_time' => $curTime,
                'question_id' => $v['id'],
                'questionnaire_id' => $id,
                'answer' => $answer,
                'user_id' => 0,
            ];
        }

        $data = [
            'ip' => $_SERVER['REMOTE_ADDR'],
            'questionnaire_id' => $id,
            'user_id' => 0,
            'create_time' => $curTime,
        ];
        Db::startTrans();
        try{

            $questionnaireId =  db('questionnaire_record')->insertGetId($data);
            if(!$questionnaireId){
                throw new Exception('记录保存失败');
            }
            foreach ($qarr as $k=>$v){
                $qarr[$k]['questionnaire_record_id'] = $questionnaireId;
            }

            $res = db('questionnaire_record_answer')
                ->insertAll($qarr);
            if(!$res){
                throw new Exception('问题记录保存失败');
            }
            Db::commit();
            $this->success('操作成功');
        }catch (Exception $e){
           Db::rollback();
            $this->error($e->getMessage());
        }

    }
    public function wjsuccess(){

        $title = db('config')
            ->where('name','web_site_title')
            ->value('value');
        $this->assign('title',$title);
        return $this->fetch('index/success');
    }



}