<template>
  <div>
    <el-dialog
      title="分配角色菜单"
      :destroy-on-close="true"
      :visible.sync="Visible"
      width="30%"
      @close="handleClose"
      :close-on-click-modal="false"
      top="10vh"
    >
      <el-form ref="form" :model="form" size="medium" label-width="100px">
        <el-form-item label="角色名称">
          <el-input readonly :value="roleInfo.name"></el-input>
        </el-form-item>
        <el-form-item label="菜单权限">
          <el-checkbox v-model="isExpand" @change="handleCheckedTreeExpand"
            >展开/折叠</el-checkbox
          >
          <el-checkbox v-model="isNodeAll" @change="handleCheckedTreeNodeAll"
            >全选/全不选</el-checkbox
          >
          <el-checkbox v-model="checkStrictly">父子联动</el-checkbox>
          <el-tree
            class="tree-border"
            :data="menuTree"
            show-checkbox
            ref="menu"
            node-key="id"
            :check-strictly="!checkStrictly"
            :props="defaultProps"
          ></el-tree>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button size="medium" @click="handleRest">重 置</el-button>
        <el-button
          size="medium"
          :loading="loading"
          type="primary"
          @click="handleOk"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { addRoleMenu, getRoleMenuList } from "@/api/system";
export default {
  components: {},
  props: {
    show: {
      type: Boolean,
      required: true,
      default: false,
    },
    roleInfo: {
      type: Object,
      required: true,
      default: () => {},
    },
    menuTree: {
      type: Array,
      required: true,
      default: () => [],
    },
  },
  data() {
    return {
      loading: false,
      userMenuList: [], // 用户资源列表
      //   menuTree: [], // 菜单树
      isExpand: false,
      isNodeAll: false,
      checkStrictly: true,
      defaultProps: {
        label: "label",
        children: "children",
      },
      form: {
        menuIdList: [],
        roleId: "",
      },
    };
  },
  computed: {
    Visible: {
      get() {
        return this.show;
      },
      set(val) {
        this.$emit("update:show", val);
      },
    },
  },
  methods: {
    // 获取角色菜单列表
    async getRoleMenuList() {
      let res = await getRoleMenuList({
        roleId: this.form.roleId,
        page: 1,
        size: -1,
      });
      if (res.data.code == 1) {
        let { data } = res.data;
        let ids = this.getRoleMenuIdList(data);
        ids.forEach((v) => {
          this.$nextTick(() => {
            this.$refs.menu.setChecked(v, true, false);
          });
        });
      }
    },
    // 确定
    async handleOk() {
      this.loading = true;
      let res = await addRoleMenu({
        roleId: this.form.roleId,
        menuIdList: this.getMenuAllCheckedKeys(),
      });
      let { code, msg } = res.data;
      if (code === 1) {
        this.$message.success(msg || "添加成功");
        this.$emit("success");
        this.handleClose();
      }
      this.loading = false;
    },
    // 所有菜单节点数据
    getMenuAllCheckedKeys() {
      // 目前被选中的菜单节点
      let checkedKeys = this.$refs.menu.getCheckedKeys();
      // 半选中的菜单节点
      let halfCheckedKeys = this.$refs.menu.getHalfCheckedKeys();
      return [...halfCheckedKeys, ...checkedKeys];
    },
    // 树权限（展开/折叠）
    handleCheckedTreeExpand(value) {
      let treeList = this.menuTree;
      for (let i = 0; i < treeList.length; i++) {
        this.$refs.menu.store.nodesMap[treeList[i].id].expanded = value;
      }
    },
    // 树权限（全选/全不选）
    handleCheckedTreeNodeAll(value) {
      this.$refs.menu.setCheckedNodes(value ? this.menuTree : []);
    },
    // 新增
    async onAdd(row) {
      Object.assign(this.form, this.$options.data().form);
      this.form.roleId = row.id;
      this.getRoleMenuList();
    },

    // 重置
    handleRest() {
      this.isNodeAll = false;
      this.$refs.menu.setCheckedNodes([]);
    },
    // 关闭
    handleClose() {
      this.handleRest();
      this.isExpand = false;
      this.isNodeAll = false;
      this.checkStrictly = true;
      this.Visible = false;
    },
    // 获取角色菜单id列表
    getRoleMenuIdList(data) {
      let result = [];
      let getData = (list) => {
        list.forEach((item) => {
          item.checked && result.push(item.id);
          if (item.children && item.children.length > 0) {
            getData(item.children);
          }
        });
      };
      getData(data);
      return result;
    },
  },
};
</script>

<style lang="less" scoped>
:deep(.el-dialog__body) {
  max-height: 620px;
  overflow: auto;
}
.tree-border {
  margin-top: 5px;
  border: 1px solid #e5e6e7;
  background: #fff none;
  border-radius: 4px;
}
</style>
