<template>
  <div class="card-check">
    <div class="part-box part-box-filter">
      <el-form ref="FilterForm" label-position="left" label-width="85px" inline>
        <el-form-item label="审核状态：">
          <el-select
            v-model="filter.auditingStatus"
            style="width: 142px;"
            placeholder="请选择"
            clearable
          >
            <el-option
              v-for="(val, key) in AUDITING_STATUS"
              :key="key"
              :value="key"
              :label="val"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="印刷时间：">
          <el-date-picker
            v-model="filter.printTime"
            type="date"
            placeholder="请选择日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="标题：" label-width="55px">
          <el-input
            style="width: 210px;"
            v-model.trim="filter.title"
            placeholder="请输入内容"
            clearable
          ></el-input>
        </el-form-item>

        <el-form-item label-width="0px">
          <el-button type="primary" icon="icon icon-search" @click="toPage(1)"
            >查询</el-button
          >
          <el-button type="warning" icon="icon icon-plus" @click="toAdd"
            >创建题卡</el-button
          >
        </el-form-item>
      </el-form>
    </div>

    <div class="part-box">
      <el-table ref="TableList" :data="cards" border stripe>
        <el-table-column prop="id" label="题卡ID"></el-table-column>
        <el-table-column label="科目(代码)">
          <template slot-scope="scope">
            <span
              >{{ scope.row.courseName
              }}<i v-if="scope.row.courseCode"
                >({{ scope.row.courseCode }})</i
              ></span
            >
          </template>
        </el-table-column>
        <el-table-column prop="title" label="标题"></el-table-column>
        <el-table-column prop="printTime" label="打印时间"></el-table-column>
        <el-table-column
          prop="cardStatusName"
          label="处理节点"
        ></el-table-column>
        <el-table-column prop="overtime" label="剩余时间"></el-table-column>
        <el-table-column
          prop="auditingTime"
          label="提交审核时间"
        ></el-table-column>
        <el-table-column
          prop="auditingStatusName"
          label="审核状态"
        ></el-table-column>
        <el-table-column label="操作" align="center" width="120">
          <template slot-scope="scope">
            <div v-if="scope.row.cardStatus !== 0">
              <!-- <el-button
                class="btn-table-icon"
                type="text"
                icon="icon icon-download"
                @click="toExport(scope.row)"
                title="导出"
              ></el-button> -->
              <el-button
                class="btn-table-icon"
                type="text"
                icon="icon icon-copy"
                @click="toCopy(scope.row)"
                title="复制"
              ></el-button>
            </div>
            <div v-else>
              <el-button
                class="btn-table-icon"
                type="text"
                icon="icon icon-edit"
                @click="toEdit(scope.row)"
                title="编辑"
              ></el-button>
              <el-button
                class="btn-table-icon"
                type="text"
                icon="icon icon-delete"
                @click="toDelete(scope.row)"
                title="删除"
              ></el-button>
              <el-button
                class="btn-table-icon"
                type="text"
                icon="icon icon-circle-share"
                @click="toPrint(scope.row)"
                title="印刷"
                v-if="scope.row.auditingStatus !== 0"
              ></el-button>
            </div>
          </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"
        >
        </el-pagination>
      </div>
    </div>

    <!-- card-option-dialog -->
    <card-option-dialog
      ref="CardOptionDialog"
      @upload-sample-over="getList"
      @confirm="cardConfirm"
    ></card-option-dialog>
  </div>
</template>

<script>
import { AUDITING_STATUS } from "@/constants/enumerate";
import { cardListPage, copyCard, deleteCard, changeCardStatus } from "../api";
import CardOptionDialog from "../components/CardOptionDialog";

export default {
  name: "card-check",
  components: {
    CardOptionDialog
  },
  data() {
    return {
      filter: {
        title: "",
        auditingStatus: "",
        printTime: ""
      },
      current: 1,
      size: this.GLOBAL.pageSize,
      total: 0,
      visible: false,
      AUDITING_STATUS,
      cards: []
    };
  },
  created() {
    this.getList();
  },
  methods: {
    async getList() {
      const datas = {
        ...this.filter,
        pageNumber: this.current,
        pageSize: this.size
      };
      const data = await cardListPage(datas);
      this.cards = data.records;
      this.total = data.total;
      // cardStatus '处理节点，0：设计题卡，1：提交印刷，2：已完成
    },
    toPage(page) {
      this.current = page;
      this.getList();
    },
    toAdd() {
      this.$refs.CardOptionDialog.open();
    },
    cardConfirm() {
      this.$router.push({
        name: "CardDesign"
      });
    },
    toEdit(row) {
      this.$router.push({
        name: "CardDesign",
        params: {
          cardId: row.id
        }
      });
    },
    toExport() {
      // 暂时不做了
    },
    async toCopy(row) {
      await copyCard(row.id);
      this.$message.success("复制成功！");
      this.getList();
    },
    toPrint(row) {
      this.$confirm(
        "一旦准备印刷，题卡将不可再编辑，确定要提交印刷吗？",
        "警告",
        {
          cancelButtonClass: "el-button--primary",
          confirmButtonClass: "el-button--default-act",
          type: "warning"
        }
      )
        .then(async () => {
          await changeCardStatus({
            cardId: row.id,
            cardStatus: 1
          });
          this.getList();
          this.$message.success("提交成功！");
        })
        .catch(() => {});
    },
    toDelete(row) {
      this.$confirm("确定要删除当前题卡吗？", "删除警告", {
        cancelButtonClass: "el-button--primary",
        confirmButtonClass: "el-button--default-act",
        type: "warning"
      })
        .then(async () => {
          await deleteCard(row.id);
          this.$message.success("删除成功！");
          // 解决最后一项删除后的问题
          this.deletePageLastItem();
        })
        .catch(() => {});
    }
  }
};
</script>
