<template>
  <div class="analysis-batch-manage">
    <div class="part-box part-box-filter part-box-flex">
      <el-form ref="FilterForm" label-position="left" label-width="85px" inline>
        <el-form-item label="分析批次：">
          <el-input
            style="width:100%;"
            v-model.trim="filter.gradeBatchName"
            placeholder="请输入批次名称"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="toPage(1)">查询</el-button>
        </el-form-item>
      </el-form>
      <div class="part-box-action">
        <el-button type="danger" :loading="loading" @click="toBatchDelete"
          >批量删除</el-button
        >
        <el-button type="primary" @click="toAdd">新建分析批次</el-button>
      </div>
    </div>
    <div class="part-box part-box-pad">
      <el-table
        ref="TableList"
        :data="dataList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          type="selection"
          width="55"
          align="center"
        ></el-table-column>
        <el-table-column prop="batchName" label="分析批次"></el-table-column>
        <el-table-column prop="semesterName" label="学期"></el-table-column>
        <el-table-column prop="status" label="状态"> </el-table-column>
        <el-table-column
          class-name="action-column"
          label="操作"
          width="240px"
          align="center"
        >
          <template slot-scope="scope">
            <el-button
              class="btn-primary"
              type="text"
              :disabled="loading"
              @click="toAddCourse(scope.row)"
              >添加分析试卷</el-button
            >
            <el-button
              class="btn-primary"
              type="text"
              :disabled="loading"
              @click="toCalc(scope.row)"
              >开始计算</el-button
            >
            <el-button
              class="btn-danger"
              type="text"
              :disabled="loading"
              @click="toDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="part-page">
        <el-pagination
          background
          layout="total,prev, pager, next"
          :current-page="current"
          :total="total"
          :page-size="size"
          @current-change="toPage"
        >
        </el-pagination>
      </div>
    </div>

    <!-- ModifyAnalysisBatch -->
    <modify-analysis-batch
      ref="ModifyAnalysisBatch"
      :instance="curRow"
      @modified="getList"
    ></modify-analysis-batch>
    <!-- ModifyAnalysisBatchPaper -->
    <modify-analysis-batch-paper
      ref="ModifyAnalysisBatchPaper"
      :instance="curRow"
    ></modify-analysis-batch-paper>
  </div>
</template>

<script>
import { analysisBatchList, deleteAnalysisBatch } from "../api";

import ModifyAnalysisBatch from "../components/ModifyAnalysisBatch.vue";
import ModifyAnalysisBatchPaper from "../components/ModifyAnalysisBatchPaper.vue";

export default {
  name: "analysis-batch-manage",
  components: { ModifyAnalysisBatch, ModifyAnalysisBatchPaper },
  data() {
    return {
      filter: {
        gradeBatchName: ""
      },
      current: 1,
      size: this.GLOBAL.pageSize,
      total: 0,
      dataList: [],
      curRow: {},
      batchs: [],
      loading: false,
      multipleSelection: []
    };
  },
  mounted() {
    this.toPage(1);
  },
  methods: {
    async getList() {
      const datas = {
        ...this.filter,
        pageNumber: this.current,
        pageSize: this.size
      };
      const data = await analysisBatchList(datas);
      this.dataList = data.records;
      this.total = data.total;
    },
    toPage(page) {
      this.multipleSelection = [];
      this.current = page;
      this.getList();
    },
    handleSelectionChange(val) {
      this.multipleSelection = val.map(item => item.id);
    },
    async toBatchDelete() {
      if (!this.multipleSelection.length) {
        this.$message.error("请选择要删除的批次！");
        return;
      }

      const confirm = await this.$confirm(`确定要删除选中的批次吗?`, "提示", {
        type: "warning"
      }).catch(() => {});
      if (confirm !== "confirm") return;

      await deleteAnalysisBatch(this.multipleSelection);
      this.$message.success("删除成功！");
      this.deletePageLastItem();
    },
    toAdd() {
      this.curRow = {};
      this.$refs.ModifyAnalysisBatch.open();
    },
    toAddCourse(row) {
      this.curRow = row;
      this.$refs.ModifyAnalysisBatchPaper.open();
    },
    toCalc(row) {
      console.log(row);
    },
    async toDelete(row) {
      const confirm = await this.$confirm(
        `确定要删除批次【${row.batchName}】吗?`,
        "提示",
        {
          type: "warning"
        }
      ).catch(() => {});
      if (confirm !== "confirm") return;

      await deleteAnalysisBatch([row.id]);
      this.$message.success("删除成功！");
      this.deletePageLastItem();
    }
  }
};
</script>
