<template>
  <div class="organization-manage">
    <div class="page-head">
      <h2>
        <span>组织架构</span>
      </h2>
    </div>
    <div class="part-box part-box-pad">
      <el-tree
        :data="orgs"
        node-key="id"
        default-expand-all
        :expand-on-click-node="false"
        :props="defaultProps"
      >
        <span class="custom-tree-node" slot-scope="{ node, data }">
          <span>{{ node.label }}</span>
          <span v-if="data.isRoot">
            <el-button
              class="btn-table-icon"
              type="text"
              icon="icon icon-plus-act"
              @click="toAdd"
              title="新增子机构"
            ></el-button>
          </span>
          <span v-else>
            <el-button
              class="btn-table-icon"
              type="text"
              icon="icon icon-plus-act"
              @click="() => append(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>

    <!-- ModifyOrganization -->
    <modify-organization
      ref="ModifyOrganization"
      :instance="curOrg"
      @confirm="getList"
    ></modify-organization>
  </div>
</template>

<script>
import { organizationList, deleteOrganization } from "../api";
import ModifyOrganization from "../components/ModifyOrganization";

export default {
  name: "organization-manage",
  components: { ModifyOrganization },
  data() {
    return {
      orgs: [],
      curOrg: {},
      defaultProps: {
        label: "name"
      }
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    async getList() {
      const orgs = await organizationList();
      if (orgs[0]) orgs[0].isRoot = true;
      this.orgs = orgs;
    },
    toAdd() {
      this.curOrg = {};
      this.$refs.ModifyOrganization.open();
    },
    edit(node, data) {
      this.curOrg = {
        ...data,
        parentName: data.parentId && node.parent.data.name
      };
      this.$refs.ModifyOrganization.open();
    },
    remove(node, data) {
      this.$confirm(`确定要删除【${data.name}】吗？`, "提示", {
        cancelButtonClass: "el-button--danger is-plain",
        confirmButtonClass: "el-button--primary",
        type: "warning"
      })
        .then(async () => {
          await deleteOrganization(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) {
      this.curOrg = { parentId: data.id, parentName: data.name };
      this.$refs.ModifyOrganization.open();
    }
  }
};
</script>
