<template>
  <el-dialog
    :visible.sync="modalIsShow"
    title="课程目标预期值"
    top="10vh"
    width="300px"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    append-to-body
    @open="visibleChange"
  >
    <el-form ref="modalFormComp" :model="modalForm" :rules="rules">
      <el-form-item prop="expectValue" label="预期值：">
        <el-input-number
          v-model="modalForm.expectValue"
          :min="0.01"
          :max="1"
          :step="0.01"
          step-strictly
          :controls="false"
        ></el-input-number>
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button type="primary" :disabled="isSubmit" @click="submit"
        >确认</el-button
      >
      <el-button @click="cancel">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { updateCourseOutlineTargetPredict } from "../../api";

const initModalForm = {
  obeCourseOutlineId: null,
  expectValue: "",
};

export default {
  name: "modify-course-outline-target-predict",
  props: {
    rowData: {
      type: Object,
      default() {
        return {};
      },
    },
  },
  data() {
    return {
      modalIsShow: false,
      isSubmit: false,
      modalForm: { ...initModalForm },
      rules: {
        expectValue: [
          {
            required: true,
            message: "请输入课程目标预期值",
            trigger: "change",
          },
        ],
      },
    };
  },
  methods: {
    visibleChange() {
      this.modalForm = {
        obeCourseOutlineId: this.rowData.id,
        expectValue: this.rowData.expectValue,
      };
    },
    cancel() {
      this.modalIsShow = false;
    },
    open() {
      this.modalIsShow = true;
    },
    async submit() {
      const valid = await this.$refs.modalFormComp.validate().catch(() => {});
      if (!valid) return;

      if (this.isSubmit) return;
      this.isSubmit = true;
      const data = await updateCourseOutlineTargetPredict(this.modalForm).catch(
        () => {}
      );
      this.isSubmit = false;

      if (!data) return;

      this.$message.success("修改成功！");
      this.$emit("modified");
      this.cancel();
    },
  },
};
</script>
