<template>
  <div class="data-return-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 prop="semesterId" label="学期：">
          <semester-select
            ref="SemesterSelect"
            v-model="filter.semesterId"
            :clearable="false"
            manual-fetch
            select-default
          ></semester-select>
        </el-form-item>
        <el-form-item prop="examTypeId" label="考试类型：">
          <exam-type-select v-model="filter.examTypeId"></exam-type-select>
        </el-form-item>
        <el-form-item label="学院：">
          <college-select
            v-model="filter.collegeId"
            :disabled="collegeDisabled"
          ></college-select>
        </el-form-item>
        <el-form-item label="课程代码：">
          <course-select
            v-model="filter.courseCode"
            style="width: 300px"
            :semester-id="filter.semesterId"
            :exam-type-id="filter.examTypeId"
            :college-id="filter.collegeId"
          ></course-select>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            :disabled="!filter.semesterId || !filter.examTypeId"
            @click="toPage(1)"
            >查询</el-button
          >
          <el-button
            type="primary"
            :disabled="
              !filter.semesterId || !filter.examTypeId || !filter.collegeId
            "
            @click="toExport()"
            >导出</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <div class="part-box part-box-pad">
      <el-table ref="TableList" :data="dataList">
        <el-table-column prop="course" label="课程（代码）">
          <span slot-scope="scope">
            {{ scope.row.courseName }}（{{ scope.row.courseCode }}）
          </span>
        </el-table-column>
        <el-table-column prop="openCollege" label="开课学院"></el-table-column>
        <el-table-column prop="actualCount" label="有效人数"></el-table-column>
        <el-table-column prop="formulaStr" label="公式"></el-table-column>
        <el-table-column prop="statusStr" label="计算状态"></el-table-column>
        <el-table-column prop="actualCount" label="发布状态">
          <span
            slot-scope="scope"
            :class="{ 'color-success': scope.row.publish }"
          >
            {{ scope.row.publish ? "已发布" : "未发布" }}
          </span>
        </el-table-column>
        <el-table-column prop="resultStr" label="结果"></el-table-column>
        <el-table-column class-name="action-column" label="操作" width="140">
          <template slot-scope="scope">
            <el-button
              class="btn-primary"
              type="text"
              @click="toCalc(scope.row)"
            >
              试算</el-button
            >
            <el-button
              v-if="isNotAssignUser"
              class="btn-primary"
              type="text"
              @click="toPublish(scope.row)"
            >
              发布</el-button
            >
            <el-button
              class="btn-primary"
              type="text"
              @click="toExport(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"
          :page-sizes="[20, 50, 100]"
          @current-change="toPage"
        >
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import {
  assignmentDataList,
  assignmentPublish,
  downloadAssignmentData,
} from "../api";
import { downloadByApi } from "@/plugins/download";

export default {
  name: "assignment-calculate",
  data() {
    return {
      filter: {
        semesterId: "",
        examTypeId: "",
        courseCode: "",
        collegeId: "",
      },
      collegeDisabled: false,
      current: 1,
      size: this.GLOBAL.pageSize,
      total: 0,
      dataList: [],
      publishing: false,
      isAssignUser: [],
      downloading: false,
    };
  },
  async mounted() {
    const user = this.$ls.get("user", { orgId: "", roleList: [] });
    if (user.orgId) {
      this.filter.collegeId = user.orgId;
      this.collegeDisabled = true;
    }
    this.isNotAssignUser = user.roleList.some((item) => item !== "ASSIGN");
    await this.$refs.SemesterSelect.search();
  },
  methods: {
    async getList() {
      const datas = {
        ...this.filter,
        pageNumber: this.current,
        pageSize: this.size,
      };

      const data = await assignmentDataList(datas);
      this.dataList = data.records;
      this.total = data.total;
    },
    toPage(page) {
      this.current = page;
      this.getList();
    },
    toCalc(row) {
      let curAssignInfo = this.$objAssign(
        {
          semesterId: "",
          examTypeId: "",
          courseCode: "",
          courseName: "",
          openCollege: "",
        },
        row
      );
      curAssignInfo.collegeId = this.filter.collegeId;
      this.$ls.set("curAssignInfo", curAssignInfo);
      this.$router.push({
        name: "AssignmentCalculateDetail",
      });
    },
    async toPublish(row) {
      const canPublish =
        !row.id || (row.id && row.formula && row.status === "FINISH");
      if (!canPublish) {
        this.$message.error("赋分计算未完成，暂无法发布成绩！");
        return;
      }

      const result = await this.$confirm(`确定要发布赋分成绩吗?`, "提示", {
        type: "warning",
      }).catch(() => {});
      if (result !== "confirm") return;

      if (this.publishing) return;
      this.publishing = true;

      let curAssignInfo = this.$objAssign(
        {
          id: "",
          semesterId: "",
          examTypeId: "",
          courseCode: "",
          courseName: "",
          openCollege: "",
        },
        row
      );
      curAssignInfo.collegeId = this.filter.collegeId;

      const res = await assignmentPublish(curAssignInfo).catch(() => {});
      this.publishing = false;
      if (!res) return;
      this.$message.success("发布成功！");
    },
    async toExport(row) {
      if (this.downloading) return;
      this.downloading = true;

      let curAssignInfo = this.$objAssign(
        {
          semesterId: "",
          examTypeId: "",
          courseCode: "",
          collegeId: "",
          openCollege: "",
        },
        row ? { ...row, collegeId: this.filter.collegeId } : this.filter
      );

      const res = await downloadByApi(() => {
        return downloadAssignmentData(curAssignInfo);
      }).catch((e) => {
        this.$message.error(e || "下载失败，请重新尝试！");
      });
      this.downloading = false;

      if (!res) return;
      this.$message.success("下载成功！");
    },
  },
};
</script>
