<template>
  <div class="box-center">
        <div id="app">
    <div class="content-left">
        <div>
            <span style="color: red">*</span> 楼盘选择：
                <el-select v-model="houseId" size="mini" @change="selectChange" clearable :disabled="disabledcet" placeholder="请选择">
                    <el-option v-for="item in houseList" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
            </el-select>
        </div>
      <div class="select" style="display: flex;margin-top: 40px;">
          <div style="font-size:18px;font-weight: bold;">添加模板</div>
            <el-row>
              <el-col :offset="18" :span="6">
                <el-button size="mini" type="primary" @click="addBigNode">添加大类</el-button>
              </el-col>
            </el-row>

          </div>
      <input ref="input" style="display:none" value='${tempId}' />

      <el-tree ref="tree"
	      :data="treeList"
	      :default-checked-keys="defaultCheck"  show-checkbox node-key="id"
          default-expand-all :check-on-click-node="true" :expand-on-click-node="false"
          draggable :allow-drop="allowDrop" :allow-drag="allowDrag"
               @node-drop="Peerdrag">
        <span class="custom-tree-node" slot-scope="{ node, data }">
          <span> <b v-if="data.sign==0 && data.houseId==''" style="color: red">*</b> {{ node.label }}</span>

          <span>
          <el-button style="color:red;" v-if="node.level==1" type="text" size="mini" @click.stop="() => append(node,data)">
            添加子类
          </el-button>
            <span v-if="data.houseId!=''">
            <el-button v-if="node.level==2" type="text" size="mini" @click.stop="() => append1(node,data)">  添加子类    </el-button>
            <el-button type="text" size="mini" @click.stop="() => remove(node,data)"> 删除   </el-button>
            <el-button type="text" size="mini" @click.stop="() => ddeditor(node,data)">  编辑  </el-button>
            </span>
            <el-button v-if="data.sign!==1" type="text" >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</el-button>
          </span>
        </span>
      </el-tree>
      <div class="update-btn">
        <el-button type="primary" size="mini" @click="getCheckedNode">获取选中节点</el-button>
      </div>
      <!-- 删除确认弹框 -->
      <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
        <span>确定删除该条内容吗</span>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="checkDelete">确 定</el-button>
        </span>
      </el-dialog>
      <!-- 新增弹框 -->
      <el-dialog title="新增" :visible.sync="dialogFormVisible">
        <el-form :model="addForm">
          <el-row :gutter="20">
            <el-col :span="12">
             <el-form-item label="一级大类名称:" v-if="level==0" :label-width="formLabelWidth">
				<el-input v-model="addForm.name" autocomplete="off"></el-input>
			</el-form-item>
			<el-form-item label="二级小类名称:" v-if="level==1" :label-width="formLabelWidth">
				<el-input v-model="addForm.name" autocomplete="off"></el-input>
			</el-form-item>
			<el-form-item label="三级小类名称:" v-if="level==2" :label-width="formLabelWidth">
				<el-input v-model="addForm.name" autocomplete="off"></el-input>
			</el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="addSubmit">确 定</el-button>
        </div>
      </el-dialog>
    </div>

    <!-- 评分 -->
    <div class="content-right">
      <el-row>
        <el-col :span="20">
            	(提示：拖拽大类可以排序)
        </el-col>
      </el-row>

      <draggable
	   class="list-group"
	   tag="ul"
	   v-model="newlist"
	   v-bind="dragOptions"
	   @start="drag = true"
	   @end="drag = false"
	   @change="onDragCallback">
	 <div style="border: 2px dashed #CCCCCC;margin-top: 30px;padding:6px;border-radius: 6px;font-size: 14px;"
	  v-for="(item,index) in newlist" :key="item.id">
	   <div style="display: flex;align-items: center;height: 40px;">
	   <div >{{item.name}}：</div>
	     <div>
	   <el-input @input="inputYiji($event,index)" style="width: 100px;" v-model.number="item.fraction" type="number" size="mini"  ></el-input>
	     </div>
	     <div style="margin-left: 10px;">
	      <el-button type="danger" size="mini" @click="Deletecategories(item,index)">删除</el-button>
	     </div>
	    </div>
	    <div class="erji" style="margin-left: 30px;" v-for="(asd,idx) in item.children" :key="idx">
	     <div style="display: flex;align-items: center;height: 40px;">
	       <div>{{asd.name}}：</div>
	    <div>

	     <el-input @input="inputBlur($event,index,idx)" style="width: 100px;"  v-model.number="asd.fraction" type="number" size="mini"  ></el-input>

	    </div>
	    <div style="margin-left: 10px;">
	      <el-button type="primary" size="mini" @click="Removethesecondary(index,idx,asd)">删除</el-button>
	    </div>
	      </div>
			   <div class="sanji" style="width: 900px;" >
				<div style="display: flex;align-items: center;height: 40px;" v-for="(sanji,indx) in asd.children" :key="indx">
				 <div style="width:85% ;margin-left: 30px;">{{sanji.name}}</div>
				 <div style="margin-left: 10px;">
				  <el-button type="info" size="mini" @click="Deletethelevel(index,idx,indx,sanji)">删除</el-button>
				 </div>
				 </div>
			   </div>
	    </div>
	 </div>
 </draggable>

 <!-- <div style="display: flex;align-items: center;margin-top:30px;"  v-if="">
  <el-button type="primary" size="mini" @click="cancel">取消</el-button>
  <el-button style="margin-left: 30px;" type="primary" size="mini" @click="submitForm">保存</el-button>
 </div> -->

    </div>
    		<!--?编辑弹框?-->
		<el-dialog title="编辑" :visible.sync="isshowage">
				<div style="display: flex;align-items: center;height: 40px;">
					<div style="margin-left: 160px;">类名：</div>
				     <div>
				      <input v-model="frosasd.name" style="width: 280px;" type="text">
				     </div>
				 </div>

					<div style="display: flex;justify-content: center;">
					  <button style="width: 100px;height: 40px;"  @click="submicll()">取消</button>
					  <button style="width: 100px;height: 40px;margin-left: 60px;"  @click="submiadd()">提交</button>
					</div>
		</el-dialog>
  </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      drag: false,
      houseList: [],
      houseId: "", // 楼盘
      treeList: [], //树形数据
      newlist: [],
      nodelist: [],
      dialogFormVisible: false, // 弹框
      dialogVisible: false,
      isshowage: false,
      frosasd: {
        name: "",
        id: "",
      },
      addForm: {
        // 新增节点表单
        name: "",
      },
      rightform: {
        formData: [],
      },
      pid: 0,
      level: 1, //操作节点的层级
      treeData: null,
      formLabelWidth: "120px",
      deleteChildren: null,
      deleteIndex: null,
      deleteType: "form",
      checklist: [], // 树图，设置初始选中值
      defaultCheck: [], // 选中的id
      addid: null, // 新增的节点id（接口生成）
      deleteId: null, // 要删除的表单节点的ID
      disabledcet: false,
    };
  },
  created() {
    this.allTreeListId = []; // 存储的当前树图所有的接单的ID集合,如：[1,3,4,6,7]
    this.noIds = []; // 左侧树没有，但右侧有的数据
    this.rateMap = new Map();
    this.sortMap = new Map();
  },
  mounted() {
    // this.setInitCheck()
    // //获取楼盘
    // this.getHouse()

  },
  computed: {
    dragOptions() {
      return {
        animation: 200,
        group: "description",
        disabled: false,
        ghostClass: "ghost",
      };
    },
  },
  methods: {
    // 楼盘选择
    selectChange(e) {
      this.houseId = e;
      this.getTreeData();
    },
    // 获取楼盘下拉的数据
    getHouse() {
      axios({
        url: `${jypath}/template/getHouse`,
        method: "get",
        params: {},
      }).then((res) => {
        if (res.data.res === 1) {
          this.houseList = res.data.obj.map((item) => {
            return {
              value: item.id,
              label: item.propertyName,
            };
          });
          if (this.houseId == "") {
            this.houseId = this.houseList[0].value;
          }
          this.getTreeData();
        }
      });
    },
    // 获取楼盘下拉的数据
    getHouse2() {
      axios({
        url: `${jypath}/template/getHouse`,
        method: "get",
        params: {},
      }).then((res) => {
        if (res.data.res === 1) {
          this.houseList = res.data.obj.map((item) => {
            return {
              value: item.id,
              label: item.propertyName,
            };
          });
          this.getTreeData();
        }
      });
    },
    // getMarketingTree 获取层级树
    getTreeData() {
      axios({
        url: `${jypath}/marketing/getMarketingTree?houseId=` + this.houseId,
        method: "get",
        params: {
          // houseId: this.house
        },
      }).then((res) => {
        if (res.data.res === 1) {
          this.treeList = res.data.obj;
          let data = res.data.obj;
          data = JSON.stringify(data).replace(/name/g, "label");
          data = JSON.parse(data);

          this.treeList = data;
          this.getAllTreeListId(this.treeList);
        }
      });
    },
    inputYiji(e, index) {
      if (e < 0) {
        this.$message({
          message: "评分小于0，请重新评分",
          type: "warning",
        });
        this.newlist[index].fraction = Number(0);
      }
    },
    inputBlur(e, index, idx) {
      if (e < 0) {
        this.$message({
          message: "评分小于0，请重新评分",
          type: "warning",
        });
        this.newlist[index].children[idx].fraction = 0;
        var he = 0;
        this.newlist[index].children.forEach((item) => {
          he += item.fraction;
        });
        this.newlist[index].fraction = he;
      } else {
        var he = 0;
        this.newlist[index].children.forEach((item) => {
          he += Number(item.fraction);
        });
        this.newlist[index].fraction = he;

        var zohe = 0;
        this.newlist.forEach((item) => {
          zohe += item.fraction;
        });
        if (zohe > 100) {
          this.$message({
            message: "评分大于100，请重新评分",
            type: "warning",
          });
          this.newlist[index].children[idx].fraction = 0;
          var fenhe = 0;
          this.newlist[index].children.forEach((item) => {
            fenhe += item.fraction;
          });
          this.newlist[index].fraction = fenhe;
        }
      }
    },
    //编辑提交
    submiadd() {
      var that = this;
      axios({
        url: `${jypath}/marketing/update`,
        method: "get",
        params: {
          name: that.frosasd.name,
          id: that.frosasd.id,
        },
      }).then((res) => {
        if (res.data.res == 1) {
          that.treeList.forEach((item, index) => {
            if (item.id == that.frosasd.id) {
              that.treeList[index].label = that.frosasd.name;
              that.isshowage = false;
              that.$message({
                message: "修改成功",
                type: "success",
              });
              return;
            } else {
              item.children.forEach((asd, indexesd) => {
                if (asd.id == that.frosasd.id) {
                  that.treeList[index].children[indexesd].label =
                    that.frosasd.name;
                  that.isshowage = false;
                  that.$message({
                    message: "修改成功",
                    type: "success",
                  });
                  return;
                } else {
                  asd.children.forEach((zxc, indexzxc) => {
                    if (zxc.id == that.frosasd.id) {
                      that.treeList[index].children[indexesd].children[
                        indexzxc
                      ].label = that.frosasd.name;
                      that.isshowage = false;
                      that.$message({
                        message: "修改成功",
                        type: "success",
                      });
                      return;
                    }
                  });
                }
              });
            }
          });
        } else {
          this.$message({
            message: "修改失败",
            type: "error",
          });
        }
      });
    },
    //编辑
    ddeditor(node, data) {
      this.frosasd.name = data.label;
      this.frosasd.id = data.id;
      this.isshowage = true;
    },
    submicll() {
      this.isshowage = false;
    },
    //拖拽排序
    Peerdrag(before) {
      let arr = [];

      this.treeList.forEach((item, index) => {
        if (before.data.pid == item.pid) {
          arr.push(item);
        } else {
          if (item.children.length == 0) {
            return;
          } else {
            item.children.forEach((ite, inde) => {
              if (before.data.pid == ite.pid) {
                arr.push(ite);
              } else {
                if (ite.children.length == 0) {
                  return;
                } else {
                  ite.children.forEach((it, ins) => {
                    if (before.data.pid == it.pid) {
                      arr.push(it);
                    }
                  });
                }
              }
            });
          }
        }
      });
      arr.forEach((item, index) => {
        item.sort = index;
      });

      let params = {
        list: [],
      };
      params.list = arr.map((item) => {
        return {
          id: item.id,
          sort: item.sort,
        };
      });
      this.dragSort(params.list);
    },
    //排序提交
    dragSort(list) {
      axios({
        url: `${jypath}/marketing/updateSort`,
        method: "post",
        data: {
          children: list,
        },
      }).then((res) => {});
    },
    //控制那个可拖拽节点
    allowDrag(draggingNode, dropNode, type) {
      if (draggingNode.data.sign == 0) {
        return false;
      } else {
        return true;
      }
    },
    //同级节点才可以拖拽
    allowDrop(draggingNode, dropNode, type) {
      if (draggingNode.level === dropNode.level) {
        if (draggingNode.parent.id === dropNode.parent.id) {
          // 向上拖拽 || 向下拖拽
          return type === "prev" || type === "next";
        }
      } else {
        // 不同级进行处理
        return false;
      }
    },

    // 树图，设置初始选中值及分数回显--编辑跳转过来的
    setInitCheck() {
      if (this.$refs.input.value) {
        console.log("zolemang");
        this.disabledcet = true;
        axios({
          url: `${jypath}/template/findByTempId`,
          method: "get",
          params: {
            tempId: this.$refs.input.value,
          },
        }).then((res) => {
          if (res.data.res === 1) {
            this.houseId = res.data.obj.houseId;
            this.checklist = res.data.obj.list;
            this.nodelist = res.data.obj.list;
            this.getHouse2();
            this.checklist.forEach((item) => {
              this.rateMap.set(item.id, item.fraction); // map维护分数
              this.sortMap.set(item.id, item.sort); // 排序维护
              if (item.pid !== 0) {
                this.defaultCheck.push(item.id);
              }
            });
            this.listCheckedchaifen();
          }
        });
      } else {
        //获取楼盘
        this.getHouse();
      }
    },
    // 拖拽变化事件
    onDragCallback(evt) {
      this.newlist.forEach((item, index) => {
        item.sort = index + 1;
      });
    },
    // 递归获取所有的ID
    getAllTreeListId(arr) {
      const setSelected = (arr) => {
        arr.forEach((item) => {
          this.allTreeListId.push(item.id);
          if (item.children) {
            setSelected(item.children);
          }
        });
      };
      setSelected(arr);
    },
    //删除评分大类
    Deletecategories(item, index) {
      if (this.newlist[index].children.length == 0) {
        this.newlist.splice(index, 1);
        this.nodelist.forEach((it, i) => {
          if (it.id == item.id) {
            this.nodelist.splice(i, 1);
          }
        });

        item.children.forEach((it, ia) => {
          this.nodelist.forEach((ite, i) => {
            if (ite.id == it.id) {
              this.nodelist.splice(i, 1);
            }
          });
        });

        item.children.forEach((it, ia) => {
          it.children.forEach((asz, ina) => {
            this.nodelist.forEach((ite, i) => {
              if (ite.id == asz.id) {
                this.nodelist.splice(i, 1);
              }
            });
          });
        });
      } else {
        this.$message({
          message: "请先清空子级",
          type: "warning",
        });
      }
    },
    //删除评分二级
    Removethesecondary(index, idx, asd) {
      if (this.newlist[index].children[idx].children.length == 0) {
        this.newlist[index].children.splice(idx, 1);
        asd.children.forEach((it, ia) => {
          this.nodelist.forEach((item, i) => {
            if (item.id == it.id) {
              this.nodelist.splice(i, 1);
            }
          });
        });
        this.nodelist.forEach((item, i) => {
          if (item.id == asd.id) {
            this.nodelist.splice(i, 1);
          }
        });
      } else {
        this.$message({
          message: "请先清空子级",
          type: "warning",
        });
      }
    },
    //删除评分三级
    Deletethelevel(index, idx, indx, sanji) {
      this.newlist[index].children[idx].children.splice(indx, 1);
      this.nodelist.forEach((item, i) => {
        if (item.id == sanji.id) {
          this.nodelist.splice(i, 1);
        }
      });
    },
    //删除评分同时删除this.nodelist里的单数据

    // ------获取选中的节点
    getCheckedNode() {
      let res = this.$refs.tree
        .getCheckedKeys()
        .concat(this.$refs.tree.getHalfCheckedKeys());
      let arr = [];
      let arr1 = JSON.parse(JSON.stringify(this.treeList));

      res.forEach((resd, index) => {
        var arrarys = this.familyTree(arr1, resd);
        arr.push(arrarys[0]);
      });

      arr.forEach((item, index) => {
        item.name = item.label;
        this.nodelist.push(item);
      });
      this.listCheckedchaifen();
    },
    //拆分数据
    listCheckedchaifen() {
      let arr = [];
      let level2 = [];
      this.nodelist.forEach((item, index) => {
        if (item.pid == 0) {
          item.children = [];
          arr.push(item);
        } else {
          level2.push(item);
        }
      });
      let resultArr = []; //去重后的数组
      var flag;
      for (var i in arr) {
        flag = true;
        for (var j in resultArr) {
          if (resultArr[j].id == arr[i].id) {
            flag = false;
            break;
          }
        }
        if (flag) {
          resultArr.push(arr[i]);
        }
      }
      let resultArrlist = []; //去重后的数组
      var flags;
      for (var i in level2) {
        flags = true;
        for (var j in resultArrlist) {
          if (resultArrlist[j].id == level2[i].id) {
            flags = false;
            break;
          }
        }
        if (flags) {
          resultArrlist.push(level2[i]);
        }
      }
      let level3 = [];
      resultArrlist.forEach((res, index) => {
        resultArr.forEach((asd) => {
          if (res.pid == asd.id) {
            res.children = [];
            asd.children.push(res);
          }
        });
      });
      resultArrlist.forEach((xzd) => {
        resultArr.forEach((asd) => {
          asd.children.forEach((qwe) => {
            if (xzd.pid == qwe.id) {
              qwe.children.push(xzd);
            }
          });
        });
      });
      this.newlist = resultArr;
    },
    //递归处理选择的数据
    familyTree(arr1, id) {
      var temp = [];
      var forFn = function (arr, id) {
        for (var i = 0; i < arr.length; i++) {
          var item = arr[i];
          if (item.id === id) {
            temp.push(item);
            forFn(arr1, item.parentId);
            break;
          } else {
            if (item.children) {
              forFn(item.children, id);
            }
          }
        }
      };
      forFn(arr1, id);
      return temp;
    },
    //提交评分
    cancel() {
      window.history.back();
    },
    submitForm() {
      let list = this.newlist;

      if (list.length == 0) {
        this.$message({
          message: "评分不能为空，请选择！",
          type: "warning",
        });
        return;
      }

      //一级
      for (var i = 0; i < list.length; i++) {
        if (list[i].fraction == "") {
          this.$message({
            message: "(" + list[i].name + ")评分不能是空值，请重新评分",
            type: "warning",
          });
          return;
        } else if (list[i].fraction == null) {
          this.$message({
            message: "(" + list[i].name + ")评分不能是空值，请重新评分",
            type: "warning",
          });
          return;
        } else if (list[i].fraction < 0) {
          this.$message({
            message: "评分不能是负数，请重新评分",
            type: "warning",
          });
          return;
        }
      }

      //二级
      for (var i = 0; i < list.length; i++) {
        for (var j = 0; j < list[i].children.length; j++) {
          if (list[i].children[j].fraction == "") {
            this.$message({
              message:
                "(" + list[i].children[j].name + ")评分不能是空值，请重新评分",
              type: "warning",
            });
            return;
          } else if (list[i].children[j].fraction == null) {
            this.$message({
              message:
                "(" + list[i].children[j].name + ")评分不能是空值，请重新评分",
              type: "warning",
            });
            return;
          } else if (list[i].children[j].fraction < 0) {
            this.$message({
              message: "评分不能是负数，请重新评分",
              type: "warning",
            });
            return;
          }
        }
      }
      //三级
      // for(var i=0;i<list.length;i++){
      // 	for(var j=0;j<list[i].children.length;j++){
      // 		for(var g=0; g<list[i].children[j].children.length;g++){
      // 			 if(list[i].children[j].children[g].fraction ==  ""){
      //                 this.$message({
      //                   message: '(' + list[i].children[j].children[g].name + ')评分不能是空值，请重新评分',
      //                   type: 'warning'
      //                 });
      //                 return
      //
      //             }else if(list[i].children[j].children[g].fraction ==  null){
      //             	 this.$message({
      //                      message: '(' + list[i].children[j].children[g].name + ')评分不能是空值，请重新评分',
      //                      type: 'warning'
      //                    });
      //                    return
      //             }else if(list[i].children[j].children[g].fraction<0){
      //           	  this.$message({
      //                     message: '评分不能是负数，请重新评分',
      //                     type: 'warning'
      //                   });
      //           		return
      //             }
      // 		}
      // 	}
      // }
      this.suanzongfen(list);
    },
    suanzongfen(list) {
      //二级下三级总分
      // for(var i=0;i<list.length;i++){
      // 	for(var j=0;j<list[i].children.length;j++){
      // 		let sanjinum=0;
      // 		if(list[i].children[j].children.length==0){
      // 			sanjinum=list[i].children[j].fraction;
      // 		}else{
      // 			for(var g=0; g<list[i].children[j].children.length;g++){
      //    			 sanjinum += list[i].children[j].children[g].fraction;
      //    		}
      // 		}
      // 		if (list[i].children[j].fraction != sanjinum) {
      //           this.$message({
      //             message: '(' +list[i].children[j].name + ')的二级小类评分错误，请重新评分',
      //             type: 'warning'
      //           });
      //           return ;
      //         }
      // 	}
      // }

      //大类下二级总分
      for (var i = 0; i < list.length; i++) {
        let subRate = 0;
        for (var j = 0; j < list[i].children.length; j++) {
          subRate += list[i].children[j].fraction;
        }
        if (list[i].fraction != subRate) {
          this.$message({
            message: "(" + list[i].name + ")的大类评分错误，请重新评分",
            type: "warning",
          });
          return;
        }
      }

      // 总分
      let level1Rate = 0;
      for (let i = 0; i < list.length; i++) {
        level1Rate += list[i].fraction; // 大类总分
      }
      if (level1Rate < 100) {
        this.$message({
          message: "您当前的大类总分小于100，请重新评分",
          type: "warning",
        });
        return;
      } else if (level1Rate > 100) {
        this.$message({
          message: "您当前的大类总分大于100，请重新评分",
          type: "warning",
        });
        return;
      }

      let params = {
        houseId: this.houseId,
        children: [],
      };

      list.forEach((item) => {
        params.children.push({
          id: item.id,
          pid: 0,
          sort: item.sort,
          fraction: item.fraction,
          name: item.name,
          level: 0,
        });
        item.children.forEach((child) => {
          params.children.push({
            pid: item.id,
            id: child.id,
            fraction: child.fraction,
            name: child.name,
            level: 1,
          });

          child.children.forEach((childse) => {
            params.children.push({
              pid: child.id,
              id: childse.id,
              name: childse.name,
              level: 2,
            });
          });
        });
      });
      if (this.houseId == "") {
        this.$message({
          message: "请选择楼盘",
          type: "warning",
        });
        return;
      }

      this.saveRate(params);
    },
    // 评分保存 addTemplate 新增模板
    saveRate(params) {
      if (this.$refs.input.value) {
        params.tempId = this.$refs.input.value;
      }
      axios({
        url: `${jypath}/marketing/addTemplate`,
        method: "post",
        data: params,
      }).then((res) => {
        if (res.data.res === 1) {
          this.$message({
            message: "保存成功",
            type: "success",
          });
          window.history.back();
        } else {
          this.$message({
            message: res.data.resMsg,
            type: "warning",
          });
        }
      });
    },
    // 添加二级小类节点入口
    append(node, data) {
      this.addForm.name = "";
      this.pid = data.id;
      this.dialogFormVisible = true;
      this.level = node.level;
      this.treeData = data;
    },
    // 添加三级小类节点入口
    append1(node, data) {
      this.addForm.name = "";
      this.pid = data.id;
      this.dialogFormVisible = true;
      this.level = node.level;
      this.treeData = data;
    },
    // 添加大类入口
    addBigNode() {
      this.level = 0;
      this.pid = 0;
      this.addForm.name = "";
      this.dialogFormVisible = true;
    },
    // 点击添加节点确认按钮
    addSubmit() {
      if (this.addForm.name != "") {
        this.dialogFormVisible = false;
        this.addNode();
      } else {
        this.$message({
          message: "内容不能为空",
          type: "warning",
        });
        return;
      }
    },
    // 新增的节点保存
    addNode() {
      axios({
        url: `${jypath}/marketing/add`,
        method: "post",
        data: {
          pid: this.pid,
          name: this.addForm.name,
          houseId: this.houseId,
        },
      }).then((res) => {
        if (res.data.res === 1) {
          if (this.level == 0) {
            // 大类
            let newNode = {
              id: res.data.obj,
              sign: 1,
              pid: 0,
              sort: this.treeList.length + 1,
              label: this.addForm.name,
              fraction: "",
              children: [],
            };
            this.treeList.push(newNode);
            this.allTreeListId.push(res.data.obj);
          } else if (this.level == 1) {
            // 二级
            let newChild = {
              id: res.data.obj,
              pid: this.pid,
              sign: 1,
              fraction: "",
              sort: null,
              label: this.addForm.name,
              children: [],
            };

            this.treeList.forEach((item, index) => {
              if (item.id == this.pid) {
                item.children.push(newChild);
              }
            });

            /* 	if (!this.treeData.children) {
							this.$set(this.treeData, 'children', []);
						}
						this.treeData.children.push(newChild); */
            this.allTreeListId.push(res.data.obj);
          } else {
            let newChilds = {
              id: res.data.obj,
              sign: 1,
              fraction: "",
              pid: this.pid,
              sort: null,
              label: this.addForm.name,
            };
            this.treeList.forEach((item, index) => {
              item.children.forEach((ite, inde) => {
                if (ite.id == this.pid) {
                  ite.children.push(newChilds);
                }
              });
            });
            /* if (!this.treeData.children) {
							this.$set(this.treeData, 'children', []);
						}
						this.treeData.children.push(newChilds); */
            this.allTreeListId.push(res.data.obj);
          }
        }
      });
    },
    // 删除自定义的节点
    checkDelete() {
      axios({
        url: `${jypath}/marketing/del`,
        method: "post",
        params: {
          id: this.deleteId,
        },
      }).then((res) => {
        if (res.data.res === 1) {
          this.treeList.forEach((item, index) => {
            if (item.id == this.deleteId) {
              this.treeList.splice(index, 1);
            } else {
              item.children.forEach((ite, inde) => {
                if (ite.id == this.deleteId) {
                  this.treeList[index].children.splice(inde, 1);
                } else {
                  ite.children.forEach((it, ine) => {
                    if (it.id == this.deleteId) {
                      this.treeList[index].children[inde].children.splice(
                        ine,
                        1
                      );
                    }
                  });
                }
              });
            }
          });
          this.deleteChildren.splice(this.deleteIndex, 1);
          this.dialogVisible = false;
          //this.getCheckedNode();
          let index = this.allTreeListId.findIndex((d) => d === this.deleteId);
          if (index > -1) {
            this.allTreeListId.splice(index, 1);
          }
        }
      });
    },
    remove(node, data) {
      if (node.level == 3) {
        this.deleteType = "node";
        this.deleteId = data.id;
        const parent = node.parent;
        this.deleteChildren = parent.data.children || parent.data;
        this.deleteIndex = this.deleteChildren.findIndex(
          (d) => d.id === data.id
        );
        this.dialogVisible = true;
      } else {
        if (data.children.length == 0) {
          this.deleteType = "node";
          this.deleteId = data.id;
          const parent = node.parent;
          this.deleteChildren = parent.data.children || parent.data;
          this.deleteIndex = this.deleteChildren.findIndex(
            (d) => d.id === data.id
          );
          this.dialogVisible = true;
        } else {
          this.$message({
            message: "请先清空子级",
            type: "warning",
          });
        }
      }
    },
  },
};
</script>

<style scoped="scoped" lang="scss" >
.box-center {
  width: 100%;
  padding: 15px;
  padding-top: 0px;
  min-width: 1200px;
  padding-bottom: 100px;
}
#app {
  width: 100%;
  height: 100%;
  padding: 15px 15px;
  list-style: none;
  display: flex;
  background: #ffffff;
  min-height: 80vh;
  border-radius: 4px;
}
.content-left {
  width: 360px;
  padding-right: 30px;
  border-right: 1px solid #ccc;
}
.content-left .select {
  margin: 20px 0;
}
.custom-tree-node {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;
  padding-right: 8px;
}
.update-btn {
  margin-top: 30px;
  text-align: center;
}

.content-right {
  padding: 20px 30px;
  flex: 1;
}
.form-box {
  width: 100%;
  border: 2px dashed #ddd;
  margin-bottom: 20px;
  padding: 10px;
  border-radius: 8px;
}

.list-group-item {
  cursor: move;
  list-style: none;
  border: 0;
}
</style>
