<template>
  <div class="target-score-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="培养方案：">
            <training-plan-select
              v-model="filter.cultureProgramId"
              placeholder="培养方案"
              @change="trainingPlanChange"
            ></training-plan-select>
          </el-form-item>
          <el-form-item label="学期：">
            <semester-select
              v-model="filter.semesterId"
              placeholder="学期"
              style="width: 220px"
            ></semester-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>
        </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>

    <div class="part-box part-box-pad">
      <el-table ref="TableList" :data="dataList">
        <el-table-column
          type="index"
          label="序号"
          width="70"
          :index="indexMethod"
        ></el-table-column>
        <el-table-column
          prop="cultureProgramName"
          label="培养方案"
          min-width="160"
        >
        </el-table-column>
        <el-table-column prop="semesterName" label="学期" min-width="160">
        </el-table-column>
        <el-table-column label="课程（代码）" min-width="300">
          <template slot-scope="scope">
            {{ scope.row.courseName | defaultFieldFilter }}（{{
              scope.row.courseCode | defaultFieldFilter
            }}）
          </template>
        </el-table-column>
        <el-table-column prop="userName" label="创建人" min-width="200">
          <span slot-scope="scope">
            {{ scope.row.userName }}（{{ scope.row.userLoginName }}）
          </span>
        </el-table-column>
        <el-table-column
          class-name="action-column"
          label="操作"
          width="100"
          fixed="right"
        >
          <template slot-scope="scope">
            <el-button
              v-if="checkPrivilege('link', 'Score')"
              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>
    <!-- DetailTargetScore -->
    <detail-target-score
      ref="DetailTargetScore"
      :course="curRow"
    ></detail-target-score>
  </div>
</template>

<script>
import { targetScoreListPage } from "../api";
import DetailTargetScore from "../components/target-score/DetailTargetScore.vue";

export default {
  name: "target-score-manage",
  components: { DetailTargetScore },
  data() {
    return {
      filter: {
        semesterId: "",
        professionalId: "",
        cultureProgramId: "",
        courseId: "",
      },
      current: 1,
      size: this.GLOBAL.pageSize,
      total: 0,
      dataList: [],
      curRow: {},
      defaultKeys: [],
    };
  },
  mounted() {
    this.search();
  },
  methods: {
    async getList() {
      if (!this.checkPrivilege("list", "list")) return;

      const datas = {
        ...this.filter,
        pageNumber: this.current,
        pageSize: this.size,
      };
      const data = await targetScoreListPage(datas);
      this.dataList = data.records;
      this.total = data.total;
    },
    toPage(page) {
      this.current = page;
      this.getList();
    },
    search() {
      this.toPage(1);
    },
    trainingPlanChange(val) {
      this.filter.professionalId = val?.professionalId;
    },
    defaultFilterChange(key) {
      if (!this.defaultKeys.includes(key)) {
        this.defaultKeys.push(key);
      }
      if (this.defaultKeys.length === 2) this.search();
    },
    toDetail(row) {
      this.curRow = row;
      this.$refs.DetailTargetScore.open();
    },
  },
};
</script>
