<template>
  <div class="menu-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-s-check" v-if="data.type === 'LINK'"></i>
            {{ node.label }}</span
          >
          <span>
            <el-button
              v-if="data.type === 'MENU'"
              class="btn-table-icon"
              type="text"
              icon="icon icon-plus-act"
              @click="() => append(node, data)"
              title="新增"
            ></el-button>
            <el-button
              class="btn-table-icon"
              type="text"
              icon="icon icon-edit"
              @click="() => edit(node, data)"
              title="修改"
            ></el-button>
            <!-- <el-button
              class="btn-table-icon"
              type="text"
              icon="icon icon-delete"
              @click="() => remove(node, data)"
              title="删除"
            ></el-button> -->
          </span>
        </span>
      </el-tree>
    </div>

    <!-- ModifyMenu -->
    <modify-menu
      ref="ModifyMenu"
      :instance="curMenu"
      @confirm="getList"
    ></modify-menu>
  </div>
</template>

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

export default {
  name: "menu-manage",
  components: { ModifyMenu },
  data() {
    return {
      menus: [],
      curMenu: {},
      defaultProps: {
        label: "name"
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    async getList() {
      this.menus = await menuList();
    },
    toAdd() {
      this.curMenu = {};
      this.$refs.ModifyMenu.open();
    },
    edit(node, data) {
      this.curMenu = {
        ...data,
        parentName: data.parentId && node.parent.data.name
      };
      this.$refs.ModifyMenu.open();
    },
    remove(node, data) {
      const name = node.parent.label
        ? `${node.parent.label}->${data.name}`
        : data.name;
      this.$confirm(`确定要删除【${name}】吗？`, "提示", {
        cancelButtonClass: "el-button--danger is-plain",
        confirmButtonClass: "el-button--primary",
        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(node, data) {
      const sequences = node.parent.childNodes.map(item => item.sequence);
      const maxSequence = Math.max.apply(null, sequences) || 0;

      this.curMenu = {
        parentId: data.id,
        parentName: data.name,
        sequence: maxSequence + 1
      };
      this.$refs.ModifyMenu.open();
    }
  }
};
</script>
