<template>
  <div class="app-container">
    <!--筛选条件-->
    <div class="search-div">
      <el-form ref="selectForm" :inline="true" :model="listQuery" class="form-container">
        <el-form-item class="selectForm-container-item" prop="roleName">
          <el-input v-model.trim="listQuery.keyword" placeholder="角色名称" clearable/>
        </el-form-item>
        <el-button class="filter-item" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
        <el-button v-if="hasPerm('/role/add')" class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="add">新增</el-button>
      </el-form>
    </div>
    <!--查询结果Table显示-->
    <tree-table v-loading="listLoading" :data="list" :columns="columns" rowkey="id" class="treetable" border stripe>
      <el-table-column v-if="showOperate" label="操作" width="250">
        <template slot-scope="scope">
          <el-button v-if="hasPerm('/role/update')" type="warning" size="mini" @click="edit(scope.row)">修改</el-button>
          <el-button v-if="hasPerm('/role/delete')" type="danger" size="mini" @click="del(scope.row)">删除</el-button>
          <el-button v-if="hasPerm('/role/funcAuthor')" type="primary" size="mini" @click="funcPermission(scope.row)">功能权限</el-button>
          <el-button v-if="hasPerm('/role/dataAuthor')" type="success" size="mini" @click="dataPermission(scope.row)">数据权限</el-button>
        </template>
      </el-table-column>
    </tree-table>

    <!--编辑资源的对话框-->
    <edit-role v-show="editShow" ref="editrole" @watchChild="fetchData"/>
    <!--功能权限-->
    <function-perm v-show="functionShow" ref="funcperm" />
    <!--数据权限-->
    <data-perm v-show="dataShow" ref="dataperm" />

  </div>
</template>

<script>

import treeTable from '@/components/TreeTable'
import editRole from '@/views/system/role/editrole'
import functionPerm from '@/views/system/role/functionPerm'
import dataPerm from '@/views/system/role/dataPerm'
import { getRoleList, delRole } from '@/api/system/role'
import { toTreeList } from '@/utils/structure'

export default {
  name: 'ListRole',
  components: {
    treeTable,
    functionPerm,
    dataPerm,
    editRole
  },
  data() {
    return {
      listQuery: {
        keyword: ''
      },
      columns: [
        {
          text: '角色名称',
          value: 'name'
        },
        {
          text: '所在组织机构',
          value: 'deptName',
          width: 150
        },
        {
          text: '排序',
          value: 'num',
          width: 50
        },
        {
          text: '别名',
          value: 'tips'
        }
      ],
      list: [],
      listLoading: true,
      dialogFormVisible: false,
      dialogStatus: '',
      functionShow: false, // 功能权限配置是否启用
      dataShow: false, // 数据权限配置是否启用
      editShow: false // 编辑角色权限是否启用
    }
  },
  computed: {
    'showOperate': function() { // 判断是否显示操作列，如果编辑和删除的权限都没有则不显示操作列
      return this.hasPerm('/role/update') || this.hasPerm('/role/delete') || this.hasPerm('/role/funcPerm') || this.hasPerm('/role/dataPerm')
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    add() {
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.editShow = true
      this.$refs.editrole.initDialog(this.dialogStatus, this.dialogFormVisible)
    },
    edit(row) {
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.editShow = true
      console.log('row:' + row)
      this.$refs.editrole.initDialog(this.dialogStatus, this.dialogFormVisible, row)
    },
    del(row) {
      this.$confirm(
        '确定要删除' + row.name + '吗？',
        '确认删除',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).then(() => {
        delRole(row.id).then(response => {
          if (response.code === 200) {
            this.$message.success('删除成功')
            this.deleteItem(this.list, row)// 前端删除指定项
          }
        })
      })
    },
    search() {
      this.fetchData()
    },
    fetchData() {
      console.log('fetchData')
      this.listLoading = true
      getRoleList(this.listQuery).then(response => {
        // console.log(response.data.list)
        this.list = toTreeList(response.data.list)
        console.log(this.list)
        this.listLoading = false
      })
    },
    // 在嵌套列表list中删除指定元素
    deleteItem(list, des) {
      list.forEach((value, index) => {
        if (value.id === des.id) {
          list.splice(index, 1)
        } else {
          if (value.children && value.children.length > 0) {
            // this.deleteItem(value.children, des)
            this.fetchData()
          }
        }
      })
    },
    // 功能权限配置
    funcPermission(row) {
      this.dialogFormVisible = true
      this.functionShow = true
      this.$refs.funcperm.initDialog(this.dialogFormVisible, row)
    },
    // 数据权限配置
    dataPermission(row) {
      this.dialogFormVisible = true
      this.dataShow = true
      this.$refs.dataperm.initDialog(this.dialogFormVisible, row)
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .treetable{
    margin-bottom: 50px;
  }
</style>
