<template>
  <div class="student-target">
    <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="培养方案"
            ></training-plan-select>
          </el-form-item>
          <el-form-item label="学生学号/姓名：">
            <el-input
              v-model.trim="filter.studentCodeOrName"
              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', 'Calculate')"
          type="primary"
          @click="toCalculate"
          >重新计算</el-button
        >
      </div>
    </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="studentName" label="姓名"> </el-table-column>
        <el-table-column prop="studentCode" label="学号"> </el-table-column>
        <el-table-column prop="collegeName" label="学院"> </el-table-column>
        <el-table-column prop="majorName" label="专业"> </el-table-column>
        <el-table-column
          class-name="action-column"
          label="操作"
          width="90"
          fixed="right"
        >
          <template slot-scope="scope">
            <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>
    <!-- DetailStudentTarget -->
    <detail-student-target
      v-if="checkPrivilege('link', 'Detail')"
      ref="DetailStudentTarget"
      :row-data="curRow"
    ></detail-student-target>
  </div>
</template>

<script>
import { studentTargetListPage, studentTargetCalculate } from "../api";
import DetailStudentTarget from "../components/student-target/DetailStudentTarget.vue";

export default {
  name: "student-target",
  components: { DetailStudentTarget },
  data() {
    return {
      filter: {
        cultureProgramId: "",
        studentCodeOrName: "",
      },
      current: 1,
      size: this.GLOBAL.pageSize,
      total: 0,
      dataList: [],
      curRow: {},
    };
  },
  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 studentTargetListPage(datas);
      this.dataList = data.records;
      this.total = data.total;
    },
    toPage(page) {
      this.current = page;
      this.getList();
    },
    search() {
      this.toPage(1);
    },
    toDetail(row) {
      this.curRow = row;
      this.$refs.DetailStudentTarget.open();
    },
    async toCalculate() {
      const confirm = await this.$confirm(`确定要重新计算吗?`, "提示", {
        type: "warning",
      }).catch(() => {});
      if (confirm !== "confirm") return;

      await studentTargetCalculate({
        cultureProgramId: this.filter.cultureProgramId,
      });
      this.getList();
    },
  },
};
</script>
