<template>
  <div class="privilege-manage">
    <div class="part-box">
      <el-button type="primary" size="mini" icon="el-icon-edit" @click="toAdd"
        >新增</el-button
      >
    </div>
    <div class="part-box part-box-pad part-box-border">
      <el-tree
        :data="menus"
        node-key="id"
        default-expand-all
        :expand-on-click-node="false"
        :props="defaultProps"
      >
        <span class="custom-tree-node" slot-scope="{ node, data }">
          <span
            ><i class="el-icon-link" v-if="data.type === 'URL'"></i>
            {{ node.label }}</span
          >
          <span>
            <el-button
              v-if="data.type === 'MENU'"
              class="btn-primary"
              type="text"
              icon="icon icon-plus-act"
              @click="() => append(data)"
              title="新增"
            ></el-button>
            <el-button
              class="btn-primary"
              type="text"
              icon="icon icon-edit"
              @click="() => edit(node, data)"
              title="修改"
            ></el-button>
            <el-button
              class="btn-primary"
              type="text"
              icon="icon icon-delete"
              @click="() => remove(node, data)"
              title="删除"
            ></el-button>
          </span>
        </span>
      </el-tree>
    </div>

    <!-- ModifyPrivilege -->
    <modify-privilege
      ref="ModifyPrivilege"
      :instance="curMenu"
      @confirm="menuModified"
    ></modify-privilege>
  </div>
</template>

<script>
import { menuList, deleteMenu } from "../api";
import ModifyPrivilege from "../components/ModifyPrivilege";

export default {
  name: "privilege-manage",
  components: { ModifyPrivilege },
  data() {
    return {
      menus: [],
      curMenu: {},
      parentNode: null,
      parentData: null,
      defaultProps: {
        label: "name",
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    async getList() {
      this.menus = await menuList();
    },
    toAdd() {
      this.curMenu = {
        sequence: this.menus.length + 1,
      };
      this.$refs.ModifyPrivilege.open();
    },
    edit(node, data) {
      this.curMenu = {
        ...data,
        parentName: data.parentId && node.parent.data.name,
      };
      this.parentData = data;
      this.$refs.ModifyPrivilege.open();
    },
    remove(node, data) {
      const name = node.parent.label
        ? `${node.parent.label}->${data.name}`
        : data.name;
      this.$confirm(`确定要删除【${name}】吗？`, "提示", {
        type: "warning",
      })
        .then(async () => {
          await deleteMenu(data.id);

          const parent = node.parent;
          const children = parent.data.children || parent.data;
          const index = children.findIndex((d) => d.id === data.id);
          children.splice(index, 1);

          this.$message.success("删除成功！");
        })
        .catch(() => {});
    },
    append(data) {
      const sequences = data.children
        ? data.children.map((item) => item.sequence)
        : [];
      const maxSequence = sequences.length
        ? Math.max.apply(null, sequences)
        : 0;

      this.curMenu = {
        parentId: data.id,
        parentName: data.name,
        sequence: maxSequence + 1,
      };
      this.parentData = data;
      this.$refs.ModifyPrivilege.open();
    },
    menuModified(curNode) {
      if (!this.curMenu.parentName) {
        this.getList();
        return;
      }

      if (this.curMenu.id) {
        this.parentData.name = curNode.name;
        this.parentData.url = curNode.url;
        this.parentData.type = curNode.type;
        this.parentData.sequence = curNode.sequence;
        this.parentData.remark = curNode.remark;
      } else {
        if (!this.parentData.children) {
          this.$set(this.parentData, "children", []);
        }
        this.parentData.children.push(curNode);
      }
    },
  },
};
</script>
