<template>
  <div class="version-manage">
    <div class="part-box part-box-pad">
      <el-button type="primary" @click="addVersion">新增版本</el-button>
    </div>
    <div class="part-box part-box-pad">
      <el-table :data="dataList">
        <el-table-column type="index" label="序号"></el-table-column>
        <el-table-column
          prop="clientVersion"
          label="客户端版本"
        ></el-table-column>
        <el-table-column
          prop="softVersion"
          label="服务端版本"
        ></el-table-column>
        <el-table-column
          class-name="action-column"
          label="操作"
          fixed="right"
          width="100"
        >
          <template slot-scope="scope">
            <el-button
              class="btn-primary"
              type="text"
              @click="toEdit(scope.row)"
              >管理</el-button
            >
            <el-button
              class="btn-danger"
              type="text"
              @click="toDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="part-page">
        <el-pagination
          background
          layout="total, sizes, prev, pager, next, jumper"
          :pager-count="5"
          :current-page="current"
          :total="total"
          :page-size="size"
          @current-change="toPage"
          @size-change="pageSizeChange"
        >
        </el-pagination>
      </div>
    </div>

    <modify-version ref="modifyVersion"></modify-version>
  </div>
</template>

<script>
import ModifyVersion from "../components/ModifyVersion.vue";
import { versionList, versionDelete } from "@/modules/admin/api";

export default {
  name: "version-manage",
  components: {
    ModifyVersion,
  },
  data() {
    return {
      dataList: [],
      current: 1,
      size: this.GLOBAL.pageSize,
      total: 0,
      curRow: {},
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    async getList() {
      const datas = {
        pageNumber: this.current,
        pageSize: this.size,
      };
      const data = await versionList(datas);
      this.dataList = data.records;
      this.total = data.total;
    },
    toPage(page) {
      this.current = page;
      this.getList();
    },
    addVersion() {
      this.curRow = {};
      this.$refs.modifyVersion.open();
    },
    toEdit(row) {
      this.curRow = row;
      this.$refs.modifyVersion.open();
    },
    toDelete(row) {
      this.$confirm("确定删除该版本吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          await versionDelete(row.id);
          this.$message.success("删除成功");
          this.getList();
        })
        .catch(() => {});
    },
  },
};
</script>
