<template>
  <div v-loading="isLoading" class="role-info">
    <div style="margin-bottom: 30px;">
      <span style="font-size: 23px; margin-right: 20px; vertical-align: middle;">{{ info.name }}</span>
      <el-button v-show="info.name !== '超级管理员'" v-power="'addRole'" type="normal" @click="$refs.changeRole.openDialog()">编辑角色</el-button>
    </div>
    <h5 v-show="info.name == '超级管理员'">“超级管理员”为默认角色, 无法编辑且无法更改</h5>
    <div>
      <div v-show="info.name !== '超级管理员'" style="width: 100%; background-color: #f5f6f6; height: 50px; line-height: 50px; padding-left: 20px;">
        <el-button v-power="'addUser'" type="normal" @click="$refs.addPerson.show = true">添加成员</el-button>
        <el-button v-power="'batchDeleteUser'" type="normal" @click="deletePerson">批量移除</el-button>
      </div>
      <el-table
        :data="tableData"
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column v-if="info.name !== '超级管理员'" type="selection"></el-table-column>
        <el-table-column prop="name" label="姓名" show-overflow-tooltip></el-table-column>
        <el-table-column prop="relativeName" label="部门" show-overflow-tooltip></el-table-column>
        <!-- <el-table-column prop="jobNumber" label="工号" show-overflow-tooltip></el-table-column> -->
        <el-table-column prop="mobile" label="手机号" show-overflow-tooltip></el-table-column>
      </el-table>
    </div>
    <cy-dialog ref="addPerson" title="选择人员">
      <el-form ref="personForm" :model="personForm" :rules="personRules" label-width="120px" label-position="left">
        <el-form-item label="选择人员" prop="ids" style="width: 100%;" :show-message="false" class="permission-tree">
          <el-tree
            ref="personTree"
            :load="loadNode"
            show-checkbox
            lazy
            node-key="person-tree"
            :default-checked-keys="personForm.ids"
            :props="personProps"
          >
          </el-tree>
        </el-form-item>
      </el-form>
      <template slot="footer">
        <el-button type="primary" @click="confirm('personForm')">保存</el-button>
        <el-button type="normal" @click="$refs.addPerson.show = false">取消</el-button>
      </template>
    </cy-dialog>
    <edit-role ref="changeRole" :info="roleInfo" @close="submitRole"></edit-role>
  </div>
</template>

<script>
import request from '@/utils/request'
import editRole from './editRole.vue'

export default {
  name: 'RoleInfo',
  components: { editRole },
  props: ['info'],
  data() {
    return {
      tableData: [],
      selectedIds: [],
      personForm: {
        ids: []
      },
      personRules: {
        ids: [{ required: true, trigger: 'change', message: '请选择人员' }]
      },
      personProps: {
        label: 'name',
        children: 'children',
        isLeaf: data => {
          return data.type
        },
        disabled: data => {
          return !data.type && !data.children
        }
      },
      roleInfo: {},
      isLoading: false
    }
  },
  watch: {
    info: {
      handler: function(val) {
        if (val && val.id) {
          this.getList(val.id)
          this.roleInfo = val
        }
      },
      immediate: true,
      deep: true
    }
  },
  mounted() {},
  methods: {
    getList(id) {
      this.isLoading = true
      request({
        url: '/user/do_listByRole',
        method: 'get',
        params: {
          page: 1,
          rows: 1300,
          roleId: id
        }
      }).then(({ rows }) => {
        this.tableData = rows || []
        this.isLoading = false
      }, () => {
        this.isLoading = false
      })
    },
    addPerson() {
      const data = new FormData()
      data.append('roleId', this.info.id)
      data.append('userIds', this.personForm.ids.join(','))
      request({
        url: '/role/do_addUserRole',
        method: 'post',
        data
      }).then(() => {
        this.$message.success('添加成功')
        this.getList(this.info.id)
        this.$refs.addPerson.show = false
      })
    },
    deletePerson() {
      const data = new FormData()
      data.append('roleId', this.info.id)
      data.append('userIds', this.selectedIds.join(','))
      request({
        url: '/role/do_removeUserRole',
        method: 'post',
        data
      }).then(() => {
        this.$message.success('删除成功')
        this.getList(this.info.id)
      })
    },
    handleSelectionChange(val) {
      this.selectedIds = val.map(item => {
        return item.id
      })
    },
    confirm(key) {
      this.personForm.ids = this.$refs.personTree.getCheckedNodes(true).map(item => {
        return item.id
      })
      this.$refs[key].validate(flag => {
        if (flag) {
          this.addPerson()
        } else {
          this.$message.error('必填项不能为空')
        }
      })
    },
    loadNode(node, resolve) {
      const params = node.level !== 0 ? { parentId: node.data.id } : {}
      if (node && node.data && node.data.type) {
        resolve([])
        return
      }
      request({
        url: '/org/findOrgAndUserTreeNode',
        method: 'get',
        params
      }).then(({ data }) => {
        if (data && data.length && node.level !== 0) {
          node.data.children = data
        }
        resolve(data)
      })
    },
    submitRole(val) {
      this.$emit('close', val)
    }
  }
}
</script>

<style lang="scss">
.role-info {
  padding: 20px;
  width: 100%;
  h5 {
    height: 40px;
    line-height: 40px;
    font-weight: 400;
    background-color: #fffbdb;
    color: #333;
    padding: 0 10px;
    margin-top: -10px;
  }
  .permission-tree .el-form-item__content {
    height: 200px;
    overflow: auto;
  }
}
</style>
