<template>
  <div class="target-statistics">
    <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="培养方案"
              :clearable="false"
              default-select
              @change="trainingPlanChange"
            ></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"
              :clearable="false"
              default-select
              @default-selected="search"
            ></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 label="课程（代码）">
          <template slot-scope="scope">
            {{ scope.row.courseName | defaultFieldFilter }}（{{
              scope.row.courseCode | defaultFieldFilter
            }}）
          </template>
        </el-table-column>
        <el-table-column prop="semesterName" label="修读学期">
        </el-table-column>
        <el-table-column prop="cultureProgramName" label="所属培养方案">
        </el-table-column>
        <el-table-column prop="userName" label="创建人">
          <span slot-scope="scope">
            {{ scope.row.userName }}（{{ scope.row.userLoginName }}）
          </span>
        </el-table-column>
        <el-table-column
          class-name="action-column"
          label="操作"
          width="140"
          fixed="right"
        >
          <template slot-scope="scope">
            <el-button
              v-if="checkPrivilege('link', 'view')"
              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>
    <!-- DetailTargetStatistics -->
    <detail-target-statistics
      v-if="checkPrivilege('link', 'view')"
      ref="DetailTargetStatistics"
      :course="curRow"
    ></detail-target-statistics>
  </div>
</template>

<script>
import { targetStatisticsListPage } from "../api";
import DetailTargetStatistics from "../components/target-statistics/DetailTargetStatistics.vue";

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

      if (!this.filter.cultureProgramId || !this.filter.courseId) return;

      const datas = {
        ...this.filter,
        pageNumber: this.current,
        pageSize: this.size,
      };
      const data = await targetStatisticsListPage(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;
    },
    toDetail(row) {
      this.curRow = row;
      this.$refs.DetailTargetStatistics.open();
    },
  },
};
</script>
