<template>
  <div class="container">
    <div class="top">
      <el-button type="success" @click="addRole(2)">新增角色组</el-button>
    </div>

    <el-table
      border
      stripe
      :data="departmentData"
      highlight-current-row
      style="width: 100%"
    >
      <el-table-column type="index" align="center" label="序号" width="50px" />
      <el-table-column prop="rname" label="角色名称" align="center" />
      <el-table-column prop="rCreateTime" label="创建时间" align="center" />
      <el-table-column prop="rremark" label="备注" align="center" />
      <el-table-column
        prop="operation"
        label="操作"
        align="center"
        width="200px"
        fixed="right"
        auto-expand-parent="false"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleEdit(3, scope.$index, scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            @click="handleDelete(scope.$index, scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
        <el-pagination
          class="right"
          :current-page.sync="listQuery.page"
          :page-size="listQuery.limit"
          :page-sizes="[10,20,30,40]"
          :total="total"
          @size-change="sizeChange"
          @current-change="pageChange"
          layout="total, sizes, prev, pager, next, jumper"
        ></el-pagination>
    </div>
  </div>
</template>
<script>
import Pagination from '@/components/Pagination'
// 引入接口
import {
  getUserInfo,
  addUserInfo,
  updateUserInfo,
  userIsExit,
  deleteUserInfo,
  loadResourceDefine
} from '@/api/system/userManagement/role'
export default {
  name: 'DepartManagement',
  components: { Pagination },
  data() {
    return {
      // 列表数据
      departmentData: [],

      // 分页
      total: 30,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 10,
        importance: undefined,
        title: undefined,
        type: undefined,
        sort: '+id'
      }
    }
  },

  mounted() {
    this.getList()
  },

  methods: {
    getList() {
      var data = {
        pageIndex: this.listQuery.page,
        pageSize: this.listQuery.limit
      }
      getUserInfo(data)
        .then((res) => {
          console.log(data, 'data')
          console.log(res.data, ',,,,,,')

          if (res.code == 200) {
            console.log(res.data.records, 'res.data.records')
            this.departmentData = res.data.records

            this.listQuery.limit = res.data.size
            this.total = res.data.total
          }
        })
        .catch((err) => {
          console.log(err)
        })
    },
    sizeChange(v) {
      this.listQuery.limit = v;
      this.getList();
    },
    pageChange(v) {
      this.listQuery.page = v;
      this.getList();
    },

    // 删除用户

    deleteList() {
      console.log('shancuu')
      this.$confirm('确定删除该项记录?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          console.log(11111)
          var params = {
            id: this.id
          }
          console.log(params, '删除id')
          deleteUserInfo(params)
            .then((res) => {
              if (res.code == 200) {
                loadResourceDefine().then(response => {
                  if(response.code == 200){
                    this.getList()
                    this.$message({
                      type: 'success',
                      message: '删除成功!'
                    })
                  }else{
                    this.$message({
                      type: 'error',
                      message: response.msg
                    })
                  }
                })
                
              }
            })
            .catch(() => {
              this.$message({
                type: 'info',
                message: '已取消删除'
              })
            })
        })
    },

    // 新增角色
    addRole(type) {
      this.$router.push({ path: '/roleManagement/addRole', query: { type: type }})
    },
    // 编辑
    handleEdit(type, index, row) {
      console.log(row)
      this.$router.push({ path: '/roleManagement/addRole', query: { type: type, id: row.rid }})
    },
    // 删除
    handleDelete(index, row) {
      this.id = row.rid
      this.deleteList()
    }

    /* ----- 格式化表格序号 -----*/
    // indexMethod(index) {
    //   console.log(index,"index..")
    //   return (
    //     (this.listQuery.page - 1) * this.listQuery.limit + index + 1
    //   );
    // },
  }
}
</script>
<style lang="scss" scoped>
.container {
  width: 100%;
  .top {
    height: 50px;
    line-height: 50px;
    text-align: right;
    margin-bottom: 20px;
  }
}
.pagination-container {
  margin-top:10px;
  margin-right:20px;
  text-align: right;
}
</style>
