<template>
  <el-dialog
    v-model="dialogFormVisible"
    :title="title"
    width="500px"
    @close="close"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="角色授权" prop="menuIds">
        <el-tree
          ref="tree"
          empty-text="暂无数据"
          :expand-on-click-node="false"
          :props="defaultProps"
          :default-checked-keys="node"
          node-key="value"
          :data="parentData"
          show-checkbox
          default-expand-all
          @check="currentChecked"
        ></el-tree>
      </el-form-item>
    </el-form>
    <div class="dialog-footer">
      <el-button type="primary" size="small" @click="save">确 定</el-button>
      <el-button size="small" @click="close">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { assignMenu, getMenuIdsByRoleId } from '@/api/role';
import { getCustom } from '@/api/menu';
export default {
  name: 'RoleGrant',
  data() {
    return {
      form: {
        roleId: '',
        menuIds: '',
      },
      defaultProps: {
        id: 'value',
        label: 'label',
        children: 'children',
      },
      node: [],
      menuIds: '',
      clearable: true,
      parentData: [],
      dialogFormVisible: false,
      parentIdData: [],
    };
  },
  created() {},
  methods: {
    async getParentData() {
      const { data } = await getCustom();
      this.parentData = data;
    },
    async getNodeData() {
      const { data } = await getMenuIdsByRoleId({ roleId: this.form.roleId });
      this.parentData.forEach(function (item) {
        if (data.includes(item.value)) {
          data.splice(data.indexOf(item.value), 1);
        }
      });
      this.$refs.tree.setCheckedKeys(data);
      // this.node = data;
    },
    currentChecked() {
      const checkedNodes = this.$refs.tree.getCheckedNodes(false, true);
      const menuIds = [];
      checkedNodes.forEach(nodes => {
        menuIds.push(nodes.value, nodes.value);
      });
      // 利用set没有重复的值这一特性, 实现数组去重。Array.form方法可以将 Set 结构转为数组。
      this.form.menuIds = Array.from(new Set(menuIds));
    },
    roleGrant(row) {
      this.title = '角色授权';
      this.form = Object.assign({}, row);
      this.form.roleId = row.id;
      this.dialogFormVisible = true;
      this.getNodeData();
      this.getParentData();
    },
    close() {
      this.$refs['form'].resetFields();
      this.form = this.$options.data().form;
      this.dialogFormVisible = false;
      this.$emit('fetch-data');
    },
    save() {
      this.$refs['form'].validate(async valid => {
        if (valid) {
          const { msg } = await assignMenu({
            menuIds: this.form.menuIds,
            roleId: this.form.roleId,
          });
          this.$notify({
            title: msg,
            type: 'success',
          });
          this.dialogFormVisible = false;
        } else {
          return false;
        }
      });
    },
  },
};
</script>
<style>
.select {
  width: 100%;
}
</style>
