<template>
  <div class="paper-template-manage">
    <div class="part-box">
      <h2 class="part-box-title">试卷模板管理</h2>
      <!-- 搜索 -->
      <el-form class="part-filter-form" inline :model="searchForm">
        <el-form-item label="版头名称">
          <el-input
            v-model="searchForm.name"
            placeholder="请输入版头名称"
            maxlength="50"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="danger" @click="handleCurrentChange(1)">
            查询
          </el-button>
        </el-form-item>
      </el-form>

      <div class="part-box-action">
        <div></div>
        <el-button type="primary" plain icon="icon icon-edit" @click="toCreate"
          >新建
        </el-button>
      </div>
    </div>

    <div class="part-box">
      <!-- 页面列表 -->
      <el-table ref="table" :data="tableData">
        <el-table-column prop="name" label="模版名称"> </el-table-column>
        <el-table-column prop="creatorName" label="创建人"></el-table-column>
        <el-table-column prop="type" label="模板类型" width="120">
          <span slot-scope="scope">{{
            scope.row.type | paperTempalteTypeFilter
          }}</span>
        </el-table-column>
        <el-table-column width="80" label="状态">
          <template slot-scope="scope">
            <span v-if="scope.row.enable">
              <el-tooltip
                class="item"
                effect="dark"
                content="启用"
                placement="left"
              >
                <i class="icon icon-right"></i>
              </el-tooltip>
            </span>
            <span v-else>
              <el-tooltip
                class="item"
                effect="dark"
                content="禁用"
                placement="left"
              >
                <i class="icon icon-error"></i>
              </el-tooltip>
            </span>
          </template>
        </el-table-column>
        <el-table-column
          prop="creationTime"
          label="创建时间"
          width="170"
        ></el-table-column>
        <el-table-column width="220" label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              plain
              @click="toEdit(scope.row)"
              >编辑
            </el-button>
            <el-button
              size="mini"
              :type="scope.row.enable ? 'danger' : 'primary'"
              plain
              @click="toEnable(scope.row)"
            >
              {{ scope.row.enable ? "禁用" : "启用" }}
            </el-button>
            <el-button
              size="mini"
              type="danger"
              plain
              @click="toDelete(scope.row)"
              >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="part-page">
        <el-pagination
          :current-page="currentPage"
          :page-size="pageSize"
          :page-sizes="[10, 20, 50, 100, 200, 300]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        />
      </div>
    </div>
    <!-- ModifyPaperTemplateInfo -->
    <modify-paper-template-info
      ref="ModifyPaperTemplateInfo"
      :instance="curRow"
      @new-temp="toCreateTemp"
    ></modify-paper-template-info>
  </div>
</template>

<script>
import {
  paperTemplatePageListApi,
  paperTemplateEnableApi,
  paperTemplateDeleteApi,
} from "../api";

import ModifyPaperTemplateInfo from "../components/ModifyPaperTemplateInfo.vue";

export default {
  name: "PaperTemplateManage",
  components: { ModifyPaperTemplateInfo },
  data() {
    return {
      loading: false,
      searchForm: { name: "" },
      tableData: [],
      curRow: {},
      currentPage: 1,
      pageSize: 10,
      total: 10,
    };
  },
  mounted() {
    this.search();
  },
  methods: {
    async search() {
      if (this.loading) return;
      this.loading = true;

      const res = await paperTemplatePageListApi({
        ...this.searchForm,
        curPage: this.currentPage,
        pageSize: this.pageSize,
      }).catch(() => {});

      this.loading = false;
      if (!res) return;

      this.tableData = res.data.content;
      this.total = res.data.totalElements;
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.search();
    },
    handleSizeChange(val) {
      this.currentPage = 1;
      this.pageSize = val;
      this.search();
    },
    toCreate() {
      this.curRow = {};
      this.$refs.ModifyPaperTemplateInfo.open();
    },
    toCreateTemp(tempInfo) {
      window.sessionStorage.setItem("paperTempInfo", JSON.stringify(tempInfo));
      this.$router.push({
        name: "PaperTemplateEdit",
      });
    },
    async toEnable(row) {
      const action = row.enable ? "禁用" : "启用";
      const confirm = await this.$confirm(
        `确定要${action}该试卷模板吗?`,
        "提示",
        {
          type: "warning",
        }
      ).catch(() => {});
      if (confirm !== "confirm") return;

      const enable = !row.enable;
      await paperTemplateEnableApi({
        id: row.id,
        enable,
      });
      this.$message.success("操作成功！");
      this.search();
    },
    toEdit(row) {
      window.sessionStorage.setItem("paperTempInfo", JSON.stringify(row));
      this.$router.push({
        name: "PaperTemplateEdit",
      });
    },
    async toDelete(row) {
      const confirm = await this.$confirm(`确定要删除该试卷模板吗?`, "提示", {
        type: "warning",
      }).catch(() => {});
      if (confirm !== "confirm") return;

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