<!--
 * @desc 复制试卷-确认弹窗
 * @author linyuanjie
 * @date 2023/10/19
-->

<template>
  <el-dialog
    title="复制试卷"
    :visible.sync="showDialog"
    width="700px"
    center
    :before-close="handleClose">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      class="demo-ruleForm">
      <el-form-item label="标题:" prop="title">
        <el-input v-model="formData.title" placeholder="请输入标题" maxlength="30" show-word-limit></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="handleClose">取 消</el-button>
      <el-button :loading="loading" type="primary" @click="submitFn">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import {addQuiz, batchAddQuizItem, getQuizDetail, updateQuiz} from "@/api/test";

export default {
  name: "copyQuizDialog",
  props:{
    showDialog: {
      type: Boolean,
      required: true,
      default:  false
    },
    currentRow: {
      type: Object,
      default: () => ({})
    }
  },
  data () {
    return {
      loading: false,
      formData: {
        title: ''
      },
      formRules: {
        title: [{ required: true, message: '请输入标题', trigger: 'blur' }]
      },
      quizSerialNum: '',
      oldQuizId: null,
      oldQuizInfo: null
    }
  },
  methods: {
    submitFn () {
      this.$refs.formRef.validate(async (valid) => {
        if (valid) {
          this.loading = true
          try {
            // 复制试卷step1: 创建试卷
            const params = {
              title: this.formData.title,
              introduction: '',
              sourceType: 'BLANK'
            }
            const res = await addQuiz(params)
            if (res.data.code === 0) {
              this.quizSerialNum = res.data.data
            }
            // 复制试卷step2: 批量创建试卷项（添加试题）
            const params1 = this.oldQuizInfo.items.map((ele, index) => {
              const { label, options, answer, widget, mark } = ele
              return {
                label,
                quizSerialNum: this.quizSerialNum,
                options,
                answer,
                widget,
                mark
              }
            })
            const res1 = await batchAddQuizItem(params1)
            if (res1.data.code === 0) {

            }
            // 复制试卷step3: 保存试卷
            const quiz = this.oldQuizInfo.quiz || { expand: {} }
            const params2 = {
              sn: this.quizSerialNum,
              expand: {
                ...quiz.expand
              },
              title: this.formData.title,
              introduction: ''
            }
            const res2 = await updateQuiz(params2)
            if (res2.data.code === 0) {
              this.$message.success('复制成功')
              this.handleClose()
            }
          } catch (e) {
            this.loading = false
          }
        } else {
          return false
        }
      })
    },
    handleClose () {
      this.loading = false
      this.$emit('update:showDialog', false)
      this.formData.title = ''
      this.uuid = ''
      this.$emit('getList')
    },
    async getQuizDetailFn () {
      const res = await getQuizDetail({ quizId: this.oldQuizId })
      if (res.data.code === 0) {
        this.oldQuizInfo = res.data.data || {}
      }
    }
  },
  watch: {
    showDialog (newVal) {
      if (newVal) {
        this.oldQuizId = this.currentRow.quizId
        this.getQuizDetailFn()
        this.formData.title = this.currentRow.title + '_副本1'
      }
    }
  }
}
</script>
