//自定义
<template>
  <div class="white-body-view app-container">
    <!-- <el-button type="primary" @click="addTreeItem('', 1)" v-if="role===3">新增</el-button> -->
    <custom-tree
      ref="customTree"
      :tree-data="treeData"
      :tree-expand-all="treeExpandAll"
      :tree-node-key="treeNodeKey"
      @addItem="addTreeItem"
      @deleteItem="deleteTreeItem"
      @editItem="editTreeItem"
    />
    <!-- 地点弹窗 -->
    <place-dialog ref="placeDialog" @addData="addData" @editData="editData" />
  </div>
</template>
<script>
import CustomTree from './components/Tree'
import PlaceDialog from './components/PlaceDialog'
import {
  getRecursionList,
  departmentDelete,
  departmentEdit,
  getAuthRecursionList
} from '@/api/user_manage'
export default {
  components: {
    CustomTree,
    PlaceDialog
  },
  data() {
    return {
      role: this.$store.getters.role,
      treeData: [],
      treeExpandAll: true,
      treeNodeKey: 'id',
      data: {
        a: null,
        b: null
      },
      clickData: {}
      // options: [],
      //   value1: [],
      //   value: '',
    }
  },

  created() {
    this.initTreeData()
  },

  methods: {
    fetchData() {},
    // 初始化列表
    initTreeData() {
      if (this.role == 4) {
        getAuthRecursionList().then((res) => {
          this.treeData = res.data
        })
      } else {
        getRecursionList().then((res) => {
          this.treeData = res.data
        })
      }
    },

    // 新增表单数据
    addData(data) {
      if (this.clickData.departmentId) {
        data.parentId = this.clickData.departmentId
      } else {
        data.adminId = this.clickData.adminId
      }
      // 新增树节点
      departmentEdit(data).then((res) => {
        this.initTreeData()
        this.$message({
          type: 'success',
          message: '添加成功!'
        })
      })
      // this.$refs.customTree.treeAddItem(data);
    },

    // 修改表单数据
    editData(data) {
      console.log(data)
      departmentEdit(data).then((res) => {
        this.initTreeData()
        this.$message({
          type: 'success',
          message: '修改成功!'
        })
      })
      // 修改树节点
      // this.$refs.customTree.treeEditItem(data);
    },

    // 增加树节点
    addTreeItem(data, type) {
      this.clickData = data
      // 打开地点弹窗，设置默认父级节点
      this.$refs.placeDialog.openDialog(false, data)
    },

    // 删除树节点
    deleteTreeItem(data) {
      this.$confirm('确定删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          // 删除树节点
          departmentDelete(data.departmentId).then((res) => {
            this.initTreeData()
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
          })
          // this.$refs.customTree.treeDeleteItem(data);
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },

    // 修改树节点
    editTreeItem(data) {
      // 打开地点编辑弹窗
      this.$refs.placeDialog.openDialog(true, data.parentId, data)
    }
  }
}
</script>

<style scoped>
button {
  margin-bottom: 20px;
}
.margin-left {
  margin: 10px 0 0 10px;
}
.input {
  width: 150px;
}
</style>
