<template>
  <div class="course-outline-manage">
    <div class="part-box part-box-filter part-box-flex">
      <el-form ref="FilterForm" label-position="left" label-width="85px" inline>
        <template v-if="checkPrivilege('condition', 'condition')">
          <el-form-item label="专业：">
            <professional-select
              v-model="filter.professionalId"
              placeholder="专业"
            ></professional-select>
          </el-form-item>
          <el-form-item label="培养方案：">
            <training-plan-select
              v-model="filter.cultureProgramId"
              placeholder="培养方案"
              :professional-id="filter.professionalId"
              professional-required
            ></training-plan-select>
          </el-form-item>
          <el-form-item label="课程：">
            <training-plan-course-select
              v-model="filter.courseId"
              placeholder="课程"
              :professional-id="filter.professionalId"
              :culture-program-id="filter.cultureProgramId"
            ></training-plan-course-select>
          </el-form-item>
          <el-form-item label="课程大纲名称：">
            <el-input
              v-model.trim="filter.name"
              placeholder="课程大纲名称"
              clearable
            ></el-input>
          </el-form-item>
        </template>
        <el-form-item label-width="0px">
          <el-button
            v-if="checkPrivilege('button', 'select')"
            type="primary"
            @click="search"
            >查询</el-button
          >
        </el-form-item>
      </el-form>
      <div class="part-box-action">
        <el-button
          v-if="checkPrivilege('button', 'Save')"
          type="primary"
          icon="el-icon-circle-plus-outline"
          @click="toAdd"
          >新增</el-button
        >
      </div>
    </div>

    <div class="part-box part-box-pad">
      <el-table ref="TableList" :data="dataList">
        <el-table-column
          type="index"
          label="序号"
          width="55"
          :index="indexMethod"
        ></el-table-column>
        <el-table-column prop="outlineName" label="课程大纲" min-width="300">
        </el-table-column>
        <el-table-column label="课程（代码）" min-width="240">
          <template slot-scope="scope">
            {{ scope.row.courseName | defaultFieldFilter }}（{{
              scope.row.courseCode | defaultFieldFilter
            }}）
          </template>
        </el-table-column>
        <el-table-column prop="credit" label="学分" width="80">
          <template slot-scope="scope">
            {{ scope.row.credit | defaultFieldFilter }}
          </template>
        </el-table-column>
        <el-table-column prop="evaluationMode" label="考核方式" width="100">
          <span slot-scope="scope">
            {{ scope.row.evaluationMode | evaluationModeFilter }}
          </span>
        </el-table-column>
        <el-table-column prop="courseType" label="课程类别" width="160">
          <span slot-scope="scope">
            {{ scope.row.courseType | courseTypeFilter }}
          </span>
        </el-table-column>
        <el-table-column prop="semesterName" label="修读学期" width="200">
        </el-table-column>
        <el-table-column
          prop="cultureProgramName"
          label="所属培养方案"
          min-width="240"
        >
        </el-table-column>
        <el-table-column
          prop="targetCount"
          label="课程目标数量"
          width="110"
        ></el-table-column>
        <el-table-column prop="weightSetting" label="权重设置状态" width="110">
          <template slot-scope="scope">
            <span :class="scope.row.weightSetting ? '' : 'color-gray-2'">
              {{ scope.row.weightSetting ? "已完成" : "未完成" }}
            </span>
          </template>
        </el-table-column>
        <el-table-column
          class-name="action-column"
          label="操作"
          width="200"
          fixed="right"
        >
          <template slot-scope="scope">
            <el-button
              v-if="checkPrivilege('link', 'edit')"
              class="btn-primary"
              type="text"
              @click="toEdit(scope.row)"
              >编辑</el-button
            >
            <el-button
              v-if="
                checkPrivilege('link', 'audit') ||
                checkPrivilege('link', 'AuditAndRationality')
              "
              class="btn-primary"
              type="text"
              @click="toAudit(scope.row)"
              >审核</el-button
            >
            <el-button
              v-if="checkPrivilege('link', 'delete')"
              class="btn-danger"
              type="text"
              @click="toDelete(scope.row)"
              >删除</el-button
            >
            <el-button
              v-if="checkPrivilege('link', 'detail')"
              class="btn-primary"
              type="text"
              @click="toDetail(scope.row)"
              >查看详情</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="part-page">
        <el-pagination
          background
          layout="total, sizes, prev, pager, next, jumper"
          :pager-count="5"
          :current-page="current"
          :total="total"
          :page-size="size"
          @current-change="toPage"
          @size-change="pageSizeChange"
        >
        </el-pagination>
      </div>
    </div>
    <!-- ModifyCourseOutline -->
    <modify-course-outline
      v-if="checkPrivilege('button', 'Save') || checkPrivilege('link', 'edit')"
      ref="ModifyCourseOutline"
      :instance="curRow"
      @modified="getList"
    ></modify-course-outline>
    <!-- DetailCourseOutline -->
    <detail-course-outline
      v-if="checkPrivilege('link', 'detail')"
      ref="DetailCourseOutline"
      :row-data="curRow"
      @modified="getList"
    ></detail-course-outline>
    <!-- AuditCourseOutlineDialog -->
    <audit-course-outline-dialog
      v-if="
        checkPrivilege('link', 'audit') ||
        checkPrivilege('link', 'AuditAndRationality')
      "
      ref="AuditCourseOutlineDialog"
      :row-data="curRow"
    ></audit-course-outline-dialog>
  </div>
</template>

<script>
import { courseOutlineListPage, deleteCourseOutline } from "../api";
import ModifyCourseOutline from "../components/course-outline/ModifyCourseOutline.vue";
import DetailCourseOutline from "../components/course-outline/DetailCourseOutline.vue";
import AuditCourseOutlineDialog from "../components/course-outline/AuditCourseOutlineDialog.vue";

export default {
  name: "course-outline-manage",
  components: {
    ModifyCourseOutline,
    DetailCourseOutline,
    AuditCourseOutlineDialog,
  },
  data() {
    return {
      filter: {
        professionalId: "",
        cultureProgramId: "",
        courseId: "",
        name: "",
      },
      current: 1,
      size: this.GLOBAL.pageSize,
      total: 0,
      dataList: [],
      curRow: {},
    };
  },
  mounted() {
    this.toPage(1);
  },
  methods: {
    async getList() {
      if (!this.checkPrivilege("list", "list")) return;

      const datas = {
        ...this.filter,
        pageNumber: this.current,
        pageSize: this.size,
      };
      const data = await courseOutlineListPage(datas);
      this.dataList = data.records;
      this.total = data.total;
    },
    toPage(page) {
      this.current = page;
      this.getList();
    },
    search() {
      this.toPage(1);
    },
    toAdd() {
      this.curRow = {};
      this.$refs.ModifyCourseOutline.open();
    },
    toEdit(row) {
      this.curRow = row;
      this.$refs.ModifyCourseOutline.open();
    },
    toAudit(row) {
      this.curRow = row;
      this.$refs.AuditCourseOutlineDialog.open();
    },
    async toDelete(row) {
      const confirm = await this.$confirm(
        `确定要删除课程大纲【${row.outlineName}】吗?`,
        "提示",
        {
          type: "warning",
        }
      ).catch(() => {});
      if (confirm !== "confirm") return;

      await deleteCourseOutline(row.id);
      this.$message.success("删除成功！");
      this.deletePageLastItem();
    },
    toDetail(row) {
      this.curRow = row;
      this.$refs.DetailCourseOutline.open();
    },
  },
};
</script>
