<template>
  <div v-loading="loading" style="min-height: 500px;">
    <el-form
      v-if="isRender"
      ref="questions"
      size="small"
      :model="questionForm"
    >
      <template>
        <QuestionFormItem
          v-for="question of questions"
          :key="question.Id"
          :question="question"
          :question-form="questionForm"
          :is-system-criterion="isSystemCriterion"
          :reading-task-state="readingTaskState"
          :criterion-id="criterionId"
          :calculation-list="CalculationList"
          @resetFormItemData="resetFormItemData"
          @setFormItemData="setFormItemData"
        />
      </template>
    </el-form>
  </div>

</template>

<script>

import { getTrialReadingQuestion, getCustomQuestionPreview, getCustomTableQuestionPreview, getQuestionCalculateRelation} from '@/api/trials'
import QuestionFormItem from './QuestionFormItem'
export default {
  name: 'QuestionsPreview',
  components: {
    QuestionFormItem
  },
  props: {
    isSystemCriterion: {
      type: Boolean,
      default() { return false }
    },
    criterionId: {
      type: String,
      required: true
    },
    formType: {
      type: Number,
      required: true
    }
  },
  data() {
    return {
      loading: false,
      questions: [],
      questionForm: {},
      publicQuestions: [],
      isRender: false,
      readingTaskState: 0,
      activeName: 0,
      CalculationList: [],
      classArr: []
    }
  },
  mounted() {
    this.getQuestionCalculateRelation()
    this.getCustomTableQuestionPreview()
  },
  methods: {
    getQuestionCalculateRelation() {
      getQuestionCalculateRelation({
        TrialReadingCriterionId: this.criterionId
      }).then(res => {
        this.CalculationList = res.Result
      })
    },
    getCustomTableQuestionPreview() {
      this.loading = true
      getCustomTableQuestionPreview({
        TrialReadingCriterionId: this.criterionId
      }).then(res => {
        res.Result.SinglePage.map((v) => {
          if (v.Type === 'group' && v.Childrens.length === 0) return
          if (!v.IsPage && v.Type !== 'group' && v.Type !== 'summary' && v.Type !== 'table') {
            this.$set(this.questionForm, v.Id, v.Answer)
          }
          if (v.Type === 'table') {
            this.$set(this.questionForm, v.Id, [])
          }
          if (v.Childrens.length > 0) {
            this.setChild(v.Childrens)
          }
        })
        this.questions = res.Result.SinglePage
        this.isRender = true
        this.loading = false
      })
    },
    getQuestions() {
      this.loading = true
      var param = {
        readingQuestionCriterionTrialId: this.criterionId,
        formType: this.formType
      }
      getTrialReadingQuestion(param).then(res => {
        if (res.OtherInfo.FormType === 2) {
          if (res.Result.MultiPage.length > 0) {
            res.Result.MultiPage.map((v) => {
              // if (v.Type === 'group' && v.Childrens.length === 0 && i.Type !== 'table' && i.Type !== 'basicTable') return
              if (!v.IsPage && v.Type !== 'group' && v.Type !== 'summary' && i.Type !== 'table' && i.Type !== 'basicTable') {
                this.$set(this.questionForm, v.Id, v.Answer)
              }
              if (v.Childrens.length > 0) {
                this.setChild(v.Childrens)
              }
            })
            this.questions = res.Result.MultiPage
            this.activeName = res.Result.MultiPage[0].PageName
          }
          if (res.Result.PublicPage.length > 0) {
            res.Result.PublicPage.map((v) => {
              // if (v.Type === 'group' && v.Childrens.length === 0 && i.Type !== 'table') return
              if (!v.IsPage && v.Type !== 'group' && v.Type !== 'summary' && i.Type !== 'table' && i.Type !== 'basicTable') {
                this.$set(this.questionForm, v.Id, v.Answer)
              }
              if (v.Type === 'table') {
                this.$set(this.questionForm, v.Id, [])
              }
              if (v.Type === 'class') {
                this.classArr.push({triggerId: v.ClassifyQuestionId, classId: v.Id, classifyAlgorithms: v.ClassifyAlgorithms, classifyType: v.ClassifyType})
              }
              if (v.Childrens.length > 0) {
                this.setChild(v.Childrens)
              }
            })
            this.publicQuestions = res.Result.PublicPage
          }
        } else {
          res.Result.SinglePage.map((v) => {
            // if (v.Type === 'group' && v.Childrens.length === 0 && i.Type !== 'table') return
            if (!v.IsPage && v.Type !== 'group' && v.Type !== 'summary' && i.Type !== 'table' && i.Type !== 'basicTable') {
              this.$set(this.questionForm, v.Id, v.Answer)
            }
            if (v.Type === 'table') {
              this.$set(this.questionForm, v.Id, [])
            }
            if (v.Type === 'class') {
              this.classArr.push({triggerId: v.ClassifyQuestionId, classId: v.Id, classifyAlgorithms: v.ClassifyAlgorithms, classifyType: v.ClassifyType})
            }
            if (v.Childrens.length > 0) {
              this.setChild(v.Childrens)
            }
          })
          this.questions = res.Result.SinglePage
        }
        this.isRender = true
        this.loading = false
      }).catch(() => { this.loading = false })
    },
    setChild(obj) {
      obj.forEach(i => {
        if (i.Type !== 'group' && i.Type !== 'summary' && i.Id && i.Type !== 'table' && i.Type !== 'basicTable') {
          this.$set(this.questionForm, i.Id, i.Answer)
        }
        if (i.Type === 'table') {
          this.$set(this.questionForm, i.Id, [])
        }
        // if (i.Type === 'number') {
        //   this.$set(this.questionForm, i.Id, i.Answer)
        // }
        console.log('setChild')
        if (i.Type === 'class') {
          this.classArr.push({triggerId: i.ClassifyQuestionId, classId: i.Id, classifyAlgorithms: i.ClassifyAlgorithms, classifyType: i.ClassifyType})
        }
        if (i.Childrens && i.Childrens.length > 0) {
          this.setChild(i.Childrens)
        }
      })
    },
    resetFormItemData(v) {
      this.questionForm[v] = ''
    },
    setFormItemData(obj) {
      if (obj.type && obj.type === 'table') {
        let arr = this.questionForm[obj.key] || []
        arr.push(JSON.parse(JSON.stringify(obj.val)))
        this.$set(this.questionForm, obj.key, arr)
      } else {
        this.$set(this.questionForm, obj.key, JSON.parse(JSON.stringify(obj.val)))
        this.classArr.map(i=>{
          if (i.triggerId === obj.key) {
            let answer = null
            let list = JSON.parse(i.classifyAlgorithms)
            if (i.classifyType === 0) {
              let o = list.find(v => {
                return (
                  parseFloat(obj.val) >= parseFloat(v.gt) &&
                  parseFloat(obj.val) < parseFloat(v.lt)
                )
              })
              answer = o ? o.label : 'NE' 
            } else if (i.classifyType === 1) {
              let o = list.find(v => {
                return v.val.includes(obj.val)
              })
              answer = o ? o.label : null 
            }
            this.$set(this.questionForm, i.classId, answer)
          }
        })
        // if (this.classArr.length > 0) {
        //   let qs = this.classArr.find(i=>i.triggerId === obj.key)
        //   if (!qs) return
        //   let answer = null
        //   let list = JSON.parse(qs.classifyAlgorithms)
        //   if (qs.classifyType === 0) {
        //     let o = list.find(v => {
        //       return (
        //         parseFloat(obj.val) >= parseFloat(v.gt) &&
        //         parseFloat(obj.val) < parseFloat(v.lt)
        //       )
        //     })
        //     answer = o ? o.label : null 
        //   } else if (qs.classifyType === 1) {
        //     let o = list.find(v => {
        //       return v.val.includes(obj.val)
        //     })
        //     answer = o ? o.label : null 
        //   }
        //   if (answer !== null) {
        //     this.$set(this.questionForm, qs.classId, answer)
        //   }
        // }
      }
    }
  }
}
</script>
<style lang="scss" scoped>
::-webkit-scrollbar {
    width: 7px;
    height: 7px;
  }
  ::-webkit-scrollbar-thumb {
    border-radius: 10px;
    background: #d0d0d0;
  }
.tabContent{
  height:300px;
  overflow-y: auto;

}
</style>
