<template>
  <div>
    <DbExam :type="$route.query.type" :data="examData" @submit="handlerSubmit"></DbExam>
  </div>
</template>

<script>
import DbExam from '@/components/DbExam/index'
import { practiceStartAnswer, practiceExecuteSubmit } from '@/api/practice/practice'
import { reqExamExam, reqExamSubmit } from '@/api/myexam/exam.js'
import {
  reqSimulateTestDetail,
  reqOperationDetail,
  reqOperationSubmit
} from '@/api/mycourse/index'
import _ from 'lodash'
export default {
  components: { DbExam },
  data () {
    return {
      examData: {},
      query: {}
    }
  },
  mounted () {
    this.query = this.$route.query
    this.$nextTick(() => {
      this.initData()
    })
  },
  methods: {
    initData () {
      const { type } = this.query
      if (type === 'practice') {
        this.getPracticeDetail()
      }
      if (type === 'exam') {
        this.getExamDetail()
      }
      if (type === 'test') {
        this.getTestDetail()
      }
      if (type === 'operation') {
        this.getOperationDetail()
      }
    },
    // 获取作业详情
    getOperationDetail () {
      const query = this.query
      const { person } = this.$store.state.user
      reqOperationDetail({ projectId: Number(query.projectId), personId: person.id }).then(res => {
        this.examData = res.data
      })
    },
    // 获取测试详情
    getTestDetail () {
      const query = this.query
      const { person } = this.$store.state.user
      reqSimulateTestDetail({ projectId: Number(query.projectId), personId: person.id }).then(
        res => {
          this.examData = res.data
        }
      )
    },
    // 获取练习详情
    getPracticeDetail () {
      const query = this.query
      practiceStartAnswer({
        projectId: Number(query.projectId),
        courseId: Number(query.courseId),
        type: Number(query.practiceType || 1)
      }).then(res => {
        this.examData = res.data
      })
    },
    // 获取考试详情
    getExamDetail () {
      const { person } = this.$store.state.user
      reqExamExam({ personId: person.id, projectId: this.query.projectId }).then(res => {
        this.examData = res.data
      })
    },
    // 提交
    handlerSubmit (result) {
      const { type } = this.query
      if (type === 'practice' || type === 'operation') {
        this.submitPractice(result)
      }
      if (type === 'exam') {
        this.submitExam(result)
      }
      // if (type === 'operation') {
      //   this.submitOperation(result)
      // }
    },
    // 格式化题目答案
    formatSubmit (result) {
      const data = { ...result }
      const allQuestion = []
      const question = data.question
      if (question && question.length > 0) {
        question.map(item => {
          if (item.questionList && item.questionList.length > 0) {
            item.questionList.map(j => {
              let myAnswers = j.myAnswers
              // const myAnswers = j.myAnswers
              // if (item.type != 2 && _.isArray(myAnswers)) {
              //   myAnswers = myAnswers.join(',')
              // }
              if (Array.isArray(myAnswers)) {
              } else {
                myAnswers = [myAnswers]
              }
              // if (['1', '3'].indexOf(item.questionType) !== -1 && myAnswers) {
              //   console.log('item.type', item.questionType)
              //   myAnswers = [myAnswers]
              // }
              allQuestion.push({
                ...j,
                questionId: j.questionId,
                answerTime: 0,
                myAnswers: myAnswers,
                myAnswerList: myAnswers
              })
            })
          }
        })
      }
      return allQuestion
    },
    // 提交作业
    submitOperation (result) {
      const query = this.query
      const params = {
        projectId: query.projectId,
        courseId: query.courseId,
        answerTime: 0,
        type: query.practiceType,
        questionList: this.formatSubmit(result)
      }
      reqOperationSubmit(params).then(() => {
        this.$message.success('交卷成功!')
        this.$router.go(-1)
      })
    },
    // 提交练习
    submitPractice (result) {
      const query = this.query
      const params = {
        projectId: query.projectId,
        courseId: query.courseId,
        answerTime: 0,
        type: query.practiceType,
        questionList: this.formatSubmit(result)
      }
      practiceExecuteSubmit(params).then(() => {
        this.$message.success('交卷成功!')
        this.$router.go(-1)
      })
    },
    // 提交考试
    submitExam (result) {
      const query = this.query
      const params = {
        projectId: query.projectId,
        courseId: query.courseId,
        answerTime: 0,
        type: query.practiceType,
        questionList: this.formatSubmit(result)
      }
      reqExamSubmit(params).then(() => {
        this.$message.success('交卷成功!')
        this.$router.go(-1)
      })
    }
  }
}
</script>

<style lang="less" scoped></style>
