<template>
  <div class="role-wrap table-box">
    <div
      class="btn"
      style="margin-bottom: 20px;">
      <el-button
        type="primary"
        size="mini"
        @click="showDialog('add')"
      >
        新增角色
      </el-button>
    </div>

    <div class="block-title">
      <!-- 角色列表 -->
    </div>

    <el-table
      border
      stripe
      :data="tableData">
      <!-- <el-table-column
        prop="id"
        label="ID"
        width="60"
      >
      </el-table-column> -->

      <el-table-column
        prop="name"
        label="角色名"
        width="140"
      >
      </el-table-column>

      <el-table-column
        prop="managers"
        label="角色成员"
      >
        <template slot-scope="scope">
          <el-tag
            v-for="(item,index) in scope.row.adminList"
            :key="index"
          >
            {{ item.loginName }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="creator"
        label="创建人"
        width="140"
      >
      </el-table-column>
      <el-table-column
        label="创建时间"
        width="140"
      >
        <template slot-scope="scope">
          {{ scope.row.createTime | formatTime }}
        </template>
      </el-table-column>

      <el-table-column
        fixed="right"
        label="操作"
        width="120"
      >
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            @click="showDialog('edit', scope.row)"
          >
            编辑
          </el-button>
          <el-button
            type="text"
            size="small"
            @click="delRole(scope.row.id)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 账号弹框 -->
    <ElDialog
      title="添加角色"
      width="660px"
      v-model="addVisible"
    >
      <AddDialog @updateList="getRoleList"></AddDialog>
    </ElDialog>

    <ElDialog
      title="编辑角色"
      width="660px"
      v-model="editVisible"
    >
      <EditDialog
        :role-item="currentItem"
        @updateList="getRoleList"
      ></EditDialog>
    </ElDialog>
  </div>
</template>

<script>
import { getRoleList, delRole } from '@/api/systemSetting/role'
import { cloneDeep } from 'lodash'
import ElDialog from '@/components/Dialog'
import AddDialog from './role/AddDialog'
import EditDialog from './role/EditDialog'

export default {
  components: {
    ElDialog,
    AddDialog,
    EditDialog
  },
  created () {
    this.getRoleList()
  },
  data () {
    return {
      loading: false,
      addVisible: false,
      editVisible: false,
      tableData: [],
      // 当前角色对象
      currentItem: {}
    }
  },
  methods: {
    showDialog (type, item) {
      this[`${type}Visible`] = true
      this.currentItem = cloneDeep(item)
    },
    handleClose () {
      this.editVisible = false
      this.addVisible = false
      this.currentItem = {}
    },
    getRoleList () {
      this.loading = true
      const params = {
        export: false,
        pageSize: 10,
        pageNum: 1,
        roleName: ''
      }
      getRoleList(params).then(res => {
        this.tableData = res?.data?.records || []
      }).finally(() => {
        this.loading = false
      })

      // this.$store.dispatch('GetInfo')
    },
    delRole (id) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delRole(id).then(res => {
          this.$message.success('删除成功')
          this.getRoleList()
        })
      })
    },
    handlePage (pageNum) {
      this.params.pageNum = pageNum
      this.getAccountList()
    }
  }
}
</script>

<style lang="scss" scoped>
.role-wrap {
   ::v-deep .el-tag {
    margin-right: 5px;
  }
}

</style>
