<template>
  <div >
    <el-dialog
      title="问卷调查"
      :visible.sync="dialogVisiblePwd"
      :append-to-body="true"
      :close-on-click-modal="false"
      @closed="handleClosed"
      width="780px"
      top="5vh"
    >
      <div style="padding: 10px 40px 0;" class="questionnaire-container" id="questionnaire-container">
        <div class="questionnaire-title">
          {{ questionList[0]&&questionList[0].Option?questionList[0].Option[0]:'尊敬的用户，感谢您使用系统，以下是对系统使用的调研问卷，我们期待您真实的反馈' }}
        </div>
        <el-form :model="quesitionForm" ref="questionRef" label-position="top" size="small">
          <el-form-item :label="index+1+'.'+item.Question" :prop="item.QuestionnaireId+''" 
          :rules="item.IsMust?{required:true,message:'该题为必答题',trigger:['blur','change']}:''"
          v-for="(item,index) in questionList.slice(1)" :key="item.QuestionnaireId">
            <el-radio-group v-model="quesitionForm[item.QuestionnaireId]" v-if="item.Type==1">
              <el-radio :label="it" v-for="(it,ind) in item.Option" :key="it">{{ it }}</el-radio>
            </el-radio-group>
            <el-checkbox-group v-model="quesitionForm[item.QuestionnaireId]"
            v-else-if="item.Type==2">
              <el-checkbox  :label="it" v-for="(it,ind) in item.Option" :key="it">{{ it }}</el-checkbox>
            </el-checkbox-group>
            <el-input v-else rows="5" v-model="quesitionForm[item.QuestionnaireId]" type="textarea" placeholder="请输入文本"></el-input>
          </el-form-item>
        </el-form>
        <div style="text-align: center;padding:40px 0 ;">
          <el-button @click="submitQuestion(1)" type="primary" style="width: 120px;" size="40">提交</el-button>
          <el-button @click="submitQuestion(0)" style="width: 120px;margin-left: 28px;" size="40">以后再填</el-button>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {  homeInterface } from "api/api.js";

  export default {
    name:'questionnaireDia',
    data() {
      return {
        quesitionForm:{}
      }
    },
    watch:{
      questionList:{
        handler:function(value,old){
          // console.log(value);
          // 初始化数据
          value.map((item,index) =>{
            if(index ==0)  return
            if(item.Type==2){
              this.$set(this.quesitionForm,item.QuestionnaireId,[])
            }else{
              this.$set(this.quesitionForm,item.QuestionnaireId,'')
            }

          })
          console.log(this.quesitionForm);
        },
        deep:true,
        immediate:true
      }
    },
    props:{
      dialogVisiblePwd: {
        type: Boolean,
        default: false,
      },
      questionList:{
        type: Array,
        default: () =>[],
      }
    },
    methods:{
      submitQuestion(isFill){
        if(isFill!=1){
          homeInterface.questionnaireSubmit({List:[],IsFill:isFill}).then(res=>{
            if(res.Ret == 200){
              this.$emit('update:dialogVisiblePwd',false)
            }
          })
        }else{
          this.$refs.questionRef.validate((valid)=>{
            if(valid){
              let paramList = this.questionList.map((item)=>{
                if(item.Type==4 || this.quesitionForm[item.QuestionnaireId].length==0) return undefined
                return {
                  QuestionnaireId:item.QuestionnaireId,
                  Options:Array.isArray(this.quesitionForm[item.QuestionnaireId])?
                  this.quesitionForm[item.QuestionnaireId].join('~#'):this.quesitionForm[item.QuestionnaireId],
                  Type:item.Type
                }
              }).filter(Boolean)
              // console.log(paramList);
              if(paramList.length==0){
                this.$emit('update:dialogVisiblePwd',false)
                return 
              }
              homeInterface.questionnaireSubmit({List:paramList,IsFill:isFill}).then(res=>{
                if(res.Ret == 200){
                  this.$message.success('提交成功')
                  this.$emit('submitSuccess')
                  this.$emit('update:dialogVisiblePwd',false)
                }
              })
            }
          })
        }
      },
      handleClosed(){
        this.$refs.questionRef.resetFields()
        this.$emit('update:dialogVisiblePwd',false)
      }
    },
    created(){
    }
  }
</script>

<style lang="scss" scoped>
  .questionnaire-container{
    .questionnaire-title{
      font-weight: 500;
      font-size: 16px;
      color: #333333;
      margin-bottom: 15px;
    }

  }
</style>
<style lang="scss">
  #questionnaire-container{
    .el-form-item__label{
      font-weight: 5000;
      font-size: 16px;
      color: #333333;
    }
    .el-radio__label,.el-checkbox__label,.el-textarea__inner{
      color: #666666;
    }
    .el-checkbox-group,.el-radio-group{
      margin-left: -40px;
    }
    .el-radio,.el-checkbox{
      margin-right: 0;
      margin-left: 40px;
    }
  }
</style>