<!--
 * @desc 创建试卷-保存
 * @author linyuanjie
 * @date 2023/10/18
-->

<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">
        <span>{{ formData.title }}</span>
      </el-form-item>
      <el-form-item label="总题数:" prop="itemQty">
        <span>{{ formData.expand.itemQty }}</span>
      </el-form-item>
      <el-form-item label="总分数:" prop="totalMark">
        <span>{{ formData.expand.totalMark }}</span>
      </el-form-item>
      <el-form-item label="及格分数:" prop="expand.passingMark">
        <el-input-number v-model="formData.expand.passingMark" controls-position="right" :min="0" class="w100 input-num-box" />
      </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 {updateQuiz} from "@/api/test";

export default {
  name: "addQuizTitleDialog",
  props:{
    showDialog: {
      type: Boolean,
      required: true,
      default:  false
    },
    saveQuizParams: {
      type: Object,
      default: () => ({})
    }
  },
  data () {
    return {
      formData: {
        expand: { passingMark: '' }
      },
      formRules: {
        'expand.passingMark': [{ required: true, message: '请设置及格分数', trigger: 'blur' }]
      }
    }
  },
  methods: {
    submitFn () {
      this.$refs.formRef.validate(async (valid) => {
        if (valid) {
          const res = await updateQuiz(this.formData)
          if (res.data.code === 0) {
            this.$message.success('保存成功')
            this.$emit('backFn')
          }
        } else {
          return false
        }
      })
    },
    handleClose () {
      this.$emit('update:showDialog', false)
      this.formData.expand.passingMark = ''
    }
  },
  watch: {
    showDialog (newVal) {
      if (newVal) {
        this.formData = {
          ...this.saveQuizParams
        }
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.input-num-box {
  ::v-deep .el-input__inner {
    text-align: left;
  }
}
</style>
