<template>
  <div>
    <div class="flex-row">
      <div style="flex: 1"></div>
      <el-button @click="showEdit()">add Role</el-button>
    </div>
    <el-table
      :data="tableData"
      style="width: 100%; margin-top: 10px"
      height="600"
    >
      <el-table-column prop="roleName" label="Role Name"></el-table-column>
      <el-table-column prop="number" label="Number"></el-table-column>
      <el-table-column prop="createTime" label="Create Time"> </el-table-column>
      <el-table-column prop="action" label="Operate" width="250px">
        <template slot-scope="scope">
          <span class="role-text" @click="showEdit(scope.row)">edit</span>
          <span class="role-text" @click="jumDetail(scope.row)">detail</span>
        </template>
      </el-table-column>
      <template slot="empty" v-if="tableData.length == 0">
        <span>No data available</span>
      </template>
    </el-table>
    <el-pagination
      v-if="total > 0"
      :page-size="limit"
      :current-page.sync="page"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
      background
      :page-sizes="[10, 20, 50, 100]"
      layout="prev, sizes, pager, next"
    ></el-pagination>
    <edit-item ref="editItem" />
  </div>
</template>
<script>
import editItem from "./components/editItem.vue";
export default {
  components: { editItem },
  data() {
    return {
      tableData: [
        { roleName: "Admin", number: 1, createTime: "2022-2-22" },
        { roleName: "Admin", number: 1, createTime: "2022-2-22" },
        { roleName: "Admin", number: 1, createTime: "2022-2-22" },
        { roleName: "Admin", number: 1, createTime: "2022-2-22" },
        { roleName: "Admin", number: 1, createTime: "2022-2-22" },
        { roleName: "Admin", number: 1, createTime: "2022-2-22" },
        { roleName: "Admin", number: 1, createTime: "2022-2-22" },
      ],
      maxHeight: 0,
      page: 1,
      limit: 10,
      total: 0,
    };
  },
  created() {
    // this.getTableData();
  },
  methods: {
    getTableData() {
      const { page, limit } = this;
      GetGroups({ currentPage: page, pageSize: limit }).then((res) => {
        const { data } = res;
        this.tableData = data.records;
        this.total = data.total;
        if (page > 0 && data.records.length == 0) {
          this.handleCurrentChange(page - 1);
        }
      });
    },
    toDelete(id) {
      DeleteGroup({ groupId: id }).then((res) => {
        if (res.httpCode == 200) {
          this.$message.success("delete success");
          this.handleCurrentChange(this.page);
        } else {
          this.$message.error(res.msg);
        }
      });
    },
    detail(item) {
      this.$router.push({
        path: "/groupManagement/detail",
        query: {
          groupId: item.groupId,
        },
      });
    },
    handleCurrentChange(val) {
      this.page = val;
      this.getTableData();
    },
    handleSizeChange(val) {
      this.limit = val;
      this.handleCurrentChange(1);
    },
    showEdit(item) {
      this.$refs.editItem.show(item);
    },
    jumDetail(item) {
      this.$router.push({
        path: "/roleDetail",
        query: {
          id: item,
          name: item,
        },
      });
    },
  },
};
</script>
<style scoped>
.acition-btn {
  cursor: pointer;
  border-bottom: solid 1px rgb(91, 91, 212);
  color: rgb(91, 91, 212);
}
.role-text {
  margin-right: 10px;
  cursor: pointer;
  color: rgb(11, 44, 85);
  border-bottom: solid 1px rgb(11, 44, 85);
}
</style>
