<template>
  <div
    v-loading="loading"
    class="card-head-manage content"
    element-loading-text="请稍后..."
  >
    <div class="part-box">
      <div class="part-box-header">
        <h2 class="part-box-title">题卡版头管理</h2>
        <el-button type="danger" plain icon="icon icon-back" @click="goBack"
          >返回</el-button
        >
      </div>
      <!-- 搜索 -->
      <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>
          <el-button
            type="danger"
            plain
            icon="icon icon-delete"
            @click="toBatchDelete"
            >删除
          </el-button>
        </div>
        <div>
          <el-button type="primary" icon="icon icon-plus-white" @click="toAdd"
            >新增
          </el-button>
        </div>
      </div>
    </div>

    <div class="part-box">
      <!-- 页面列表 -->
      <el-table
        ref="table"
        :data="tableData"
        resizable
        @selection-change="selectChange"
      >
        <el-table-column
          type="selection"
          width="50"
          align="center"
        ></el-table-column>
        <el-table-column prop="name" label="版头名称"></el-table-column>
        <el-table-column
          prop="creationTime"
          width="170"
          label="创建时间"
        ></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
              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
              v-if="false"
              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="10"
          :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 { cardHeadListApi, cardHeadDeleteApi, cardHeadEnableApi } from "../api";

export default {
  name: "CardHeadManage",
  data() {
    return {
      loading: false,
      searchForm: {
        name: "",
      },
      selectedIds: [],
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      total: 10,
    };
  },
  mounted() {
    const cacheInfo = window.sessionStorage.getItem("card-head-manage");
    if (cacheInfo) {
      const { currentPage, pageSize, searchForm } = JSON.parse(cacheInfo);
      this.searchForm = { ...searchForm };
      this.currentPage = currentPage;
      this.pageSize = pageSize;
      this.handleCurrentChange(this.currentPage);
      window.sessionStorage.removeItem("card-head-manage");
    } else {
      this.handleCurrentChange(1);
    }
  },
  methods: {
    async search() {
      if (this.loading) return;
      this.loading = true;

      const res = await cardHeadListApi({
        ...this.searchForm,
        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.selectedIds = [];
      this.currentPage = val;
      this.search();
    },
    handleSizeChange(val) {
      this.selectedIds = [];
      this.currentPage = 1;
      this.pageSize = val;
      this.search();
    },
    selectChange(selections) {
      this.selectedIds = selections.map((item) => item.id);
    },
    async toBatchDelete() {
      if (!this.selectedIds.length) {
        this.$message.error("请选择数据");
        return;
      }

      const confirm = await this.$confirm(
        `确定要删除选择中的这些数据吗?`,
        "提示",
        {
          type: "warning",
        }
      ).catch(() => {});
      if (confirm !== "confirm") return;

      await cardHeadDeleteApi(this.selectedIds);
      this.$message.success("删除成功！");
      this.deletePageLastItem(this.selectedIds.length);
    },
    toAdd() {
      this.cacheSearchInfo();
      this.$router.push({
        name: "CardHeadEdit",
      });
    },
    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 cardHeadEnableApi({
        id: row.id,
        enable,
      });
      this.$message.success("操作成功！");
      this.search();
    },
    toEdit(row) {
      this.cacheSearchInfo();
      this.$router.push({
        name: "CardHeadEdit",
        params: {
          cardHeadId: row.id,
        },
      });
    },
    async toDelete(row) {
      const confirm = await this.$confirm(
        `确定要删除版头【${row.name}】吗?`,
        "提示",
        {
          type: "warning",
        }
      ).catch(() => {});
      if (confirm !== "confirm") return;

      await cardHeadDeleteApi([row.id]);
      this.$message.success("删除成功！");
      this.deletePageLastItem();
    },
    goBack() {
      window.history.go(-1);
    },
    cacheSearchInfo() {
      window.sessionStorage.setItem(
        "card-head-mamange",
        JSON.stringify({
          searchForm: this.searchForm,
          currentPage: this.currentPage,
          pageSize: this.pageSize,
        })
      );
    },
  },
};
</script>
