<!--
 * @desc 创建试卷-标题
 * @author linyuanjie
 * @date 2023/10/17
-->

<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 type="primary" @click="submitFn">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import {addQuiz} from "@/api/test";

export default {
  name: "addQuizTitleDialog",
  props:{
    showDialog: {
      type: Boolean,
      required: true,
      default:  false
    }
  },
  data () {
    return {
      formData: {
        title: ''
      },
      formRules: {
        title: [{ required: true, message: '请输入标题', trigger: 'blur' }]
      }
    }
  },
  methods: {
    submitFn () {
      this.$refs.formRef.validate(async (valid) => {
        if (valid) {
          const params = {
            title: this.formData.title,
            introduction: '',
            sourceType: 'BLANK'
          }
          const res = await addQuiz(params)
          if (res.data.code === 0) {
            this.$message.success("创建试卷成功");
            this.$emit('setTitle', this.formData.title)
            this.$emit('setUUID', res.data.data)
            this.$emit('update:showDialog', false)
          }
        } else {
          return false
        }
      })
    },
    handleClose () {
      this.$router.back()
    }
  }
}
</script>
