<template>
  <div>
    <el-dialog
      class="modify-course-target"
      :visible.sync="modalIsShow"
      :title="title"
      top="10vh"
      width="600px"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      append-to-body
      @open="visibleChange"
    >
      <el-form
        ref="modalFormComp"
        :model="modalForm"
        :rules="rules"
        :key="modalForm.id"
        label-width="100px"
      >
        <el-form-item prop="targetName" label="课程目标：">
          <el-input
            v-model.trim="modalForm.targetName"
            placeholder="请输入课程目标"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item prop="dimensionIdList" label="毕业要求指标：">
          <el-button
            type="text"
            class="btn-act-primary"
            @click="toSelectDimension"
            >选择知识点</el-button
          >
          <el-table
            v-if="targetValue.length"
            :data="targetValue"
            :show-header="false"
          >
            <el-table-column
              prop="dimensionName"
              label="知识点"
            ></el-table-column>
            <el-table-column class-name="action-column" label="操作" width="60">
              <template slot-scope="scope">
                <i
                  class="el-icon-error"
                  @click="toDeleteDimension(scope.$index)"
                ></i>
              </template>
            </el-table-column>
          </el-table>
        </el-form-item>
        <el-form-item prop="degreeRequirement" label="毕业要求：">
          <el-input
            v-model="modalForm.degreeRequirement"
            placeholder="请输入毕业要求"
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 6 }"
            :maxlength="999"
            resize="none"
            show-word-limit
          ></el-input>
        </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>

    <!-- SelectDimensionDialog -->
    <select-dimension-dialog
      ref="SelectDimensionDialog"
      :course-code="instance.courseCode"
      :selected-data="modalForm.dimensionIdList"
      @confirm="dimensionSelected"
    ></select-dimension-dialog>
  </div>
</template>

<script>
import { updateCourseTarget } from "../../api";
import SelectDimensionDialog from "./SelectDimensionDialog.vue";

const initModalForm = {
  id: null,
  courseCode: "",
  targetName: "",
  degreeRequirement: "",
  dimensionIdList: [],
};

export default {
  name: "modify-course-target",
  components: { SelectDimensionDialog },
  props: {
    instance: {
      type: Object,
      default() {
        return {};
      },
    },
  },
  computed: {
    isEdit() {
      return !!this.instance.id;
    },
    title() {
      return (this.isEdit ? "编辑" : "新增") + "课程目标";
    },
  },
  data() {
    return {
      modalIsShow: false,
      isSubmit: false,
      modalForm: { ...initModalForm },
      targetValue: [],
      rules: {
        targetName: [
          {
            required: true,
            message: "请输入课程目标",
            trigger: "change",
          },
          {
            max: 30,
            message: "课程目标不能超过30个字",
            trigger: "change",
          },
        ],
        dimensionIdList: [
          {
            validator: (rule, value, callback) => {
              if (value && value.length) {
                return callback();
              }
              return callback(new Error("请选择毕业要求指标"));
            },
          },
        ],
        degreeRequirement: [
          {
            required: false,
            max: 999,
            message: "课程目标不能超过999个字",
            trigger: "change",
          },
        ],
      },
    };
  },
  methods: {
    initData(val) {
      this.modalForm = this.$objAssign(initModalForm, val);
      if (val.targetValue) {
        this.targetValue = [...val.targetValue];
        this.updateDimensionIds();
      } else {
        this.modalForm.dimensionIdList = [];
      }
    },
    visibleChange() {
      this.initData(this.instance);
    },
    cancel() {
      this.modalIsShow = false;
    },
    open() {
      this.modalIsShow = true;
    },
    toSelectDimension() {
      this.$refs.SelectDimensionDialog.open();
    },
    updateDimensionIds() {
      this.modalForm.dimensionIdList = this.targetValue.map(
        (item) => item.dimensionId
      );
    },
    dimensionSelected(dimensions) {
      this.targetValue = [...dimensions];
      this.updateDimensionIds();
      this.$refs.modalFormComp.validateField("dimensionIdList").catch(() => {});
    },
    toDeleteDimension(index) {
      this.targetValue.splice(index, 1);
      this.updateDimensionIds();
      this.$refs.modalFormComp.validateField("dimensionIdList").catch(() => {});
    },
    async submit() {
      const valid = await this.$refs.modalFormComp.validate().catch(() => {});
      if (!valid) return;

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

      if (!res) return;

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