<template>
  <div class=" layout-no-cols right-list-toolbar" >
    <div>
      <el-row >
        <el-col :span="10">
          <el-toolbar>
            <el-button @click="addRoleClickHandler" priv="Platform.Role.Add">
              <i class="fa fa-plus"></i>新建
            </el-button>
           </el-toolbar>
        </el-col>
        <el-col :span="14" align="right">
          <tree-select
            :value.sync="defaultBranch"
            :items="branchTree"
            :itemsOptions="treeItemsOptions"
            placeholder="请选择机构"
            :style="{width:'200px','display':'inline-block'}"
            @click-change="branchClickChange">
          </tree-select>
        </el-col>
      </el-row>
      <el-table ref="roleList" class="role-list" :data-read-url="getData" :auto-get-data="false">
        <el-table-column type="index" width="80" label="序号" align="center"></el-table-column>
        <el-table-column label="角色名称">
          <template slot-scope="scope">
            <a class="list-card-title" href="javascript:void(0)" @click.stop="gotoDetailPage(scope.row)">
              <i class="fa fa-user-o" aria-hidden="true"></i> {{scope.row.name}}
            </a>
          </template>
        </el-table-column>
        <el-table-column  label="所属机构">
          <template slot-scope="scope">
            <span><i class="fa fa-university" aria-hidden="true"></i> {{scope.row.branch}}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="120" align="right">
          <template slot-scope="scope">
            <el-button @click.stop="gotoDetailPage(scope.row)" type="text" priv="Platform.Role.Edit" title="编辑"  class="table-operate-icon">
              <i class="fa fa-edit f16" aria-hidden="true"></i>
            </el-button>
            <el-button @click.stop="deleteClickHandler(scope.row)" type="text" priv="Platform.Role.Delete" title="删除"  class="table-operate-icon">
              <i class="fa fa-remove f16" aria-hidden="true"></i>
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>
<script>
import util from '@src/common/util.js'
import TreeSelect from '@src/components/TreeSelect.vue'
export default {
  components: {
    'tree-select': TreeSelect
  },
  data() {
    return {
      branchTree: [], // 机构
      treeItemsOptions: {
        children: 'children',
        label: 'name',
        key: 'branchInnerCode'
      },
      defaultBranch: ''// 默认选中的机构
    }
  },
  created() {
    this.loadBranch()
  },
  methods: {
    async loadBranch() {
      let res = await axios.get('/ui/branches')
      this.branchTree = res.data.status === 1 ? res.data.data : []
      this.defaultBranch = res.data.status === 1 && res.data.data.length ? res.data.data[0].branchInnerCode : ''
      this.$refs.roleList.getData()
    },
    getData() {
      return ['/ui/roles',
        {
          params: { branchInnercode: this.defaultBranch }
        }
      ]
    },
    branchClickChange() {
      this.$refs.roleList.getData()
    },
    addRoleClickHandler() {
      this.$router.push('/platform/role/new')
    },
    async deleteClickHandler(row) {
      if (!row || !row.roleCode) {
        return
      }
      try {
        await this.$confirm('确定删除吗？删除后无法恢复。是否继续删除？', '删除确认', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        let res = await axios.delete(`/ui/roles/${row.roleCode}`)
        util.showMessage(res)
        if (res.data.status === 1) {
          this.$refs.roleList.getData()
        }
      } catch (e) {

      }
    },
    gotoDetailPage(row) {
      if (!row || !row.roleCode) {
        return
      }
      this.$router.push(`/platform/role/${row.roleCode}`)
    }
  }
}
</script>
<style scope>

</style>
