//自定义
<template>
  <div class="white-body-view app-container">
    <!-- <el-button type="primary" @click="addNewRecord()">新增</el-button> -->
    <!-- 搜索 -->
    <!-- <el-select
      class="margin-left input"
      v-model="value"
      placeholder="请选择设备类型"
    >
      <el-option
        v-for="item in options"
        :key="item.value"
        :label="item.label"
        :value="item.value"
      >
      </el-option>
    </el-select>
    <el-select
      class="margin-left input"
      v-model="value"
      placeholder="请选择设备型号"
    >
      <el-option
        v-for="item in options"
        :key="item.value"
        :label="item.label"
        :value="item.value"
      >
      </el-option>
    </el-select>
    <el-select
      class="margin-left input"
      v-model="value1"
      multiple
      placeholder="请选择设备功能"
    >
      <el-option
        v-for="item in options"
        :key="item.value"
        :label="item.label"
        :value="item.value"
      >
      </el-option>
    </el-select>
    <el-button
      type="primary"
      icon="el-icon-search"
      @click="fetchData"
      class="margin-left"
      >搜索</el-button
    > -->
    <!-- <custom-tree
      ref="customTree"
      :tree-data="treeData"
      :tree-expand-all="treeExpandAll"
      :tree-node-key="treeNodeKey"
      @addItem="addTreeItem"
      @deleteItem="deleteTreeItem"
      @editItem="editTreeItem"
    /> -->
    <custom-tree
      ref="customTree"
      :tree-data="treeData"
      :tree-expand-all="treeExpandAll"
      :tree-node-key="treeNodeKey"
    />
    <!-- 地点弹窗 -->
    <!-- <place-dialog ref="placeDialog" @addData="addData" @editData="editData" /> -->
  </div>
</template>
<script>
import CustomTree from "./components/Tree";
import PlaceDialog from "./components/PlaceDialog";
import { getList, deletePut, edit } from "@/api/putStorage";
import { getOrganizationa } from "@/api/user";
export default {
  components: {
    CustomTree,
    PlaceDialog,
  },
  data() {
    return {
      treeData: [],
      treeExpandAll: true,
      treeNodeKey: "adminId",
      data: {
        a: null,
        b: null,
      },
      // options: [],
      //   value1: [],
      //   value: '',
    };
  },

  created() {
    this.initTreeData();
  },

  methods: {
    fetchData() {},
    // 初始化列表
    initTreeData() {
      getOrganizationa().then((res) => {
        console.log(res);
        // console.log(res.data[0]);
        this.treeData = [res.data];
        console.log(this.treeData);
      });
      // this.treeData = [
      //   {
      //     children: [
      //       {
      //         children: [
      //           { name: "三级", desc: "设备功能", parentId: "3", id: "4" },
      //         ],
      //         name: "二级",
      //         desc: "设备型号",
      //         parentId: "2",
      //         id: "2",
      //       },
      //     ],
      //     desc: "设备类型",
      //     name: "一级",
      //     parentId: "1",
      //     id: "1",
      //   },
      // ];
    },

    // 添加新记录
    addNewRecord() {
      this.$refs.placeDialog.openDialog(false);
    },

    // 新增表单数据
    addData(data) {
      console.log(data);
      // 新增树节点
      edit(data).then((res) => {
        this.initTreeData();
        this.$message({
          type: "success",
          message: "添加成功!",
        });
      });
      // this.$refs.customTree.treeAddItem(data);
    },

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

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

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

    // 修改树节点
    editTreeItem(data) {
      console.log(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>
