<template>
  <div class="rule-examroom">
    <div class="part-box">
      <el-table ref="TableList" :data="dataList" border stripe>
        <el-table-column prop="schoolId" label="学校ID"></el-table-column>
        <el-table-column prop="schoolName" label="学校名称"></el-table-column>
        <el-table-column prop="createTime" label="上传时间"></el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button
              class="btn-table-icon"
              type="text"
              icon="icon icon-upload-act"
              @click="toUpload(scope.row)"
              title="上传登记表"
            ></el-button>
            <el-button
              class="btn-table-icon"
              type="text"
              icon="icon icon-circle-right"
              @click="toView(scope.row.path)"
              title="预览登记表"
              v-if="scope.row.id"
            ></el-button>
            <el-button
              class="btn-table-icon"
              type="text"
              icon="icon icon-delete"
              @click="toDelete(scope.row)"
              title="删除登记表"
              v-if="scope.row.id"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="part-page">
        <el-pagination
          background
          layout="prev, pager, next"
          :current-page="current"
          :total="total"
          :page-size="size"
          @current-change="toPage"
          hide-on-single-page
        >
        </el-pagination>
      </div>
    </div>

    <upload-file-dialog
      :paper-attachment="curAttachment"
      :format="['pdf']"
      @confirm="uploadConfirm"
      ref="UploadFileDialog"
    ></upload-file-dialog>
  </div>
</template>

<script>
import { checkinExamList, saveCheckinExam, deleteCheckinExam } from "../api";
import UploadFileDialog from "@/components/UploadFileDialog";

export default {
  name: "rule-examroom",
  components: { UploadFileDialog },
  data() {
    return {
      schoolId: this.$ls.get("schoolId"),
      dataList: [],
      current: 1,
      size: this.GLOBAL.pageSize,
      total: 0,
      curAttachment: {},
      curCheckinExam: {}
    };
  },
  mounted() {
    this.toPage(1);
  },
  methods: {
    async getList() {
      const datas = {
        schoolId: this.schoolId,
        pageNumber: this.current,
        pageSize: this.size
      };
      const data = await checkinExamList(datas);
      this.dataList = data.records;
      this.total = data.total;
    },
    toPage(page) {
      this.current = page;
      this.getList();
    },
    toUpload(row) {
      this.curCheckinExam = row;
      this.curAttachment = {
        attachmentId: row.attachmentId,
        filename: ""
      };
      this.$refs.UploadFileDialog.open();
      console.log(row);
    },
    toDelete(row) {
      this.$confirm("确定要删除当前学校登记表吗？", "删除警告", {
        cancelButtonClass: "el-button--danger is-plain",
        confirmButtonClass: "el-button--primary",
        type: "warning"
      })
        .then(async () => {
          await deleteCheckinExam(row.id);
          this.$message.success("删除成功！");
          this.deletePageLastItem();
        })
        .catch(() => {});
    },
    toView(path) {
      window.open(path);
    },
    async uploadConfirm(attachment) {
      const datas = {
        attachmentId: attachment.attachmentId,
        createId: "",
        id: this.curCheckinExam.id,
        schoolId: this.curCheckinExam.schoolId,
        updateId: ""
      };
      const userId = this.$ls.get("user", { id: "" }).id;
      const modInfo = this.curCheckinExam.id
        ? { updateId: userId }
        : { createId: userId };
      await saveCheckinExam(Object.assign(datas, modInfo));

      this.$message.success("编辑成功！");
      this.getList();
    }
  }
};
</script>
