<template>
  <div class="course-target-manage">
    <div class="part-box part-box-pad box-justify">
      <p>请根据《课程教学大纲》中的课程对“毕业要求”进行填写</p>
      <div>
        <el-button type="success" @click="toImport">导入课程知识点</el-button>
        <el-button type="primary" @click="toAdd">新增目标</el-button>
      </div>
    </div>
    <div class="part-box part-box-pad">
      <el-table :data="dataList">
        <el-table-column type="index" label="序号" width="70"></el-table-column>
        <el-table-column prop="targetName" label="课程目标"></el-table-column>
        <el-table-column prop="targetValue" label="毕业要求指标">
          <template slot-scope="scope">
            <p v-for="item in scope.row.targetValue" :key="item.dimensionId">
              {{ item.dimensionName }}（{{ item.dimensionCode }}）
            </p>
          </template>
        </el-table-column>
        <el-table-column
          prop="degreeRequirement"
          label="毕业要求"
        ></el-table-column>
        <el-table-column class-name="action-column" label="操作" width="120px">
          <template slot-scope="scope">
            <el-button
              class="btn-primary"
              type="text"
              @click="toEdit(scope.row)"
              >编辑</el-button
            >
            <el-button
              class="btn-danger"
              type="text"
              @click="toDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- ModifyCourseTarget -->
    <modify-course-target
      ref="ModifyCourseTarget"
      :instance="curRow"
      @modified="getList"
    ></modify-course-target>
    <!-- ImportFile -->
    <import-file
      ref="ImportFile"
      title="导入课程知识点"
      :upload-url="uploadUrl"
      :format="['xls', 'xlsx']"
      :download-handle="() => downloadTemplate('courseProperty')"
      :download-filename="dfilename"
      :auto-upload="false"
      @upload-success="getList"
    ></import-file>
  </div>
</template>

<script>
import { courseTargetList, deleteCourseTarget } from "../../api";
import ModifyCourseTarget from "./ModifyCourseTarget.vue";
import ImportFile from "@/components/ImportFile.vue";

export default {
  name: "CourseTargetManage",
  components: { ModifyCourseTarget, ImportFile },
  props: {
    course: {
      type: Object,
      default() {
        return {};
      },
    },
  },
  data() {
    return {
      dataList: [],
      curRow: {},
      // import
      uploadUrl: "/api/admin/teach/coure_target/dimension_import",
      dfilename: "课程知识点导入模板.xlsx",
    };
  },
  mounted() {
    // this.getList();
  },
  methods: {
    async getList() {
      const res = await courseTargetList(this.course.courseCode);
      this.dataList = res || [];
    },
    toImport() {
      this.$refs.ImportFile.open();
    },
    toAdd() {
      this.curRow = { courseCode: this.course.courseCode };
      this.$refs.ModifyCourseTarget.open();
    },
    toEdit(row) {
      this.curRow = { ...row, courseCode: this.course.courseCode };
      this.$refs.ModifyCourseTarget.open();
    },
    async toDelete(row) {
      const confirm = await this.$confirm(
        `确定要删除课程目标【${row.targetName}】吗?`,
        "提示",
        {
          type: "warning",
        }
      ).catch(() => {});
      if (confirm !== "confirm") return;

      await deleteCourseTarget(row.id);
      this.$message.success("删除成功！");
      this.dataList = this.dataList.filter((item) => item.id !== row.id);
    },
  },
};
</script>
