<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>
          <el-button type="danger" @click="handleCurrentChange(1)">
            查询
          </el-button>
        </el-form-item>
      </el-form>

      <div class="part-box-action">
        <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="createTime"
          label="创建时间"
          width="170"
        ></el-table-column>
        <el-table-column prop="creator" label="创建人"></el-table-column>
        <el-table-column width="50" 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 width="170" label="操作">
          <template slot-scope="scope">
            <el-button
              v-if="!onlyAssignTeacher"
              size="mini"
              :type="scope.row.enable ? 'danger' : 'primary'"
              plain
              @click="toEnable(scope.row)"
            >
              {{ scope.row.enable ? "禁用" : "启用" }}
            </el-button>
            <el-dropdown>
              <el-button type="primary" plain size="mini">
                更多<i class="el-icon-more el-icon--right"></i>
              </el-button>
              <el-dropdown-menu slot="dropdown" class="action-dropdown">
                <el-dropdown-item>
                  <el-button
                    size="mini"
                    type="primary"
                    plain
                    @click="toEdit(scope.row)"
                    >编辑模板
                  </el-button>
                </el-dropdown-item>
                <el-dropdown-item v-if="!onlyAssignTeacher">
                  <el-button
                    size="mini"
                    type="danger"
                    plain
                    @click="toDelete(scope.row)"
                    >删除
                  </el-button>
                </el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </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>
  </div>
</template>

<script>
import { paperTemplateListApi } from "../api";

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

      const res = await paperTemplateListApi({
        pageNumber: 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() {},
    toEnable(row) {
      console.log(row);
    },
    toEdit(row) {
      console.log(row);
    },
    toDelete(row) {
      console.log(row);
    },
  },
};
</script>
