<!-- 采集点配置-有线 -->
<template>
  <div>
    <!-- <div v-click-outside="closeMenu"> -->
    <!-- 设置树形控件 -->
    <!-- <el-button type="info" plain @click="dialogVisible = true">采集点设置</el-button> -->
    <div v-show="dialogVisible" class="tree_menu">
      <el-tree :data="trees" node-key="id" :props="defaultProps" default-expand-all @node-contextmenu="rightClick"
        @node-click="treeNodeClick" ref="tree">
        <span class="custom-tree-node" slot-scope="{ node, data }">
          <template v-if="data.editing == 1 && flag">
            <el-input style="width: 260px" v-model="gateway" @change="changeInput"></el-input>
          </template>
          <template v-else>{{ data.label }}</template>
        </span>
      </el-tree>
      <!-- 树形控件右键组件 -->
      <div v-show="menuShow" class="tree_rightmenu" :style="{ ...rightMenu }">
        <el-menu class="el-menu-vertical-demo" @select="selectMenuNode" default-active="#0078FE">
          <el-menu-item id="menuitem" index="1" style="font-size: 12px">
            <span slot="title">编辑</span>
          </el-menu-item>
          <el-menu-item index="2" style="font-size: 12px">
            <span slot="title">删 除</span>
          </el-menu-item>
          <el-menu-item index="3" style="font-size: 12px">
            <span slot="title">取 消</span>
          </el-menu-item>
        </el-menu>
      </div>
    </div>
    <!-- </div> -->
    <div class="pas_params" v-show="add_passage">
      <div>
        <el-button type="info" plain @click="handleSelect">保存</el-button>
        <!-- <el-menu class="el-menu-demo" mode="horizontal" @select="handleSelect"> -->
        <!-- 添加时只支持一条，不能多次添加 -->
        <!-- <el-menu-item index="1">保存</el-menu-item> -->
        <!-- </el-menu> -->
      </div>
      <el-table ref="singleTable" :data="tableData" style="width: 100%" v-loading="loading">
        <el-table-column type="index"> </el-table-column>
        <el-table-column property="cjdpz_tagid" label="TagID">
          <template slot-scope="scope">
            <el-select v-model="scope.row.cjdpz_tagid" placeholder="">
              <el-option v-for="(option, index) in options1" :key="index" :label="option.label"
                :value="option.value"></el-option>
            </el-select>
          </template>
        </el-table-column>
        <el-table-column property="cjdpz_mc" label="名称">
          <template slot-scope="scope">
            <el-select v-model="scope.row.cjdpz_mc" placeholder="">
              <el-option v-for="(option, index) in options2" :key="index" :label="option.label"
                :value="option.value"></el-option>
            </el-select>
          </template>
        </el-table-column>
        <el-table-column property="cjdpz_sjq" label="数据区">
          <template slot-scope="scope">
            <el-select v-model="scope.row.cjdpz_sjq" placeholder="">
              <el-option v-for="(option, index) in options3" :key="index" :label="option.label"
                :value="option.value"></el-option>
            </el-select>
          </template>
        </el-table-column>
        <el-table-column property="cjdpz_jcqdz" label="寄存地址(十进制)" width="150px">
          <template slot-scope="scope">
            <el-input v-model="scope.row.cjdpz_jcqdz" placeholder="1-9999"></el-input>
          </template>
        </el-table-column>
        <el-table-column property="cjdpz_cjsjlx" label="采集数据类型">
          <template slot-scope="scope">
            <el-select v-model="scope.row.cjdpz_cjsjlx" placeholder="">
              <el-option v-for="(option, index) in options5" :key="index" :label="option.label"
                :value="option.value"></el-option>
            </el-select>
          </template>
        </el-table-column>
        <el-table-column property="cjdpz_zjx" label="自节序">
          <template slot-scope="scope">
            <el-select v-model="scope.row.cjdpz_zjx" placeholder="">
              <el-option v-for="(option, index) in options6" :key="index" :label="option.label"
                :value="option.value"></el-option>
            </el-select>
          </template>
        </el-table-column>
        <el-table-column property="cjdpz_dxsx" label="读写属性">
          <template slot-scope="scope">
            <el-select v-model="scope.row.cjdpz_dxsx" placeholder="">
              <el-option v-for="(option, index) in options7" :key="index" :label="option.label"
                :value="option.value"></el-option>
            </el-select>
          </template>
        </el-table-column>
        <el-table-column property="cjdpz_beilv" label="倍率">
          <template slot-scope="scope">
            <el-select v-model="scope.row.cjdpz_beilv" placeholder="">
              <el-option v-for="(option, index) in options8" :key="index" :label="option.label"
                :value="option.value"></el-option>
            </el-select>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="dev_attribute">
      <el-dialog title="设备属性" :visible.sync="edit_device" width="382px" v-loading="loading">
        <el-form :model="tableData[0]" :label-position="labelPosition" :rules="rules" ref="formdata">
          <el-row>
            <el-col :span="24">
              <el-form-item label="设备ID:" :label-width="formLabelWidth" prop="sbpz_sbid">
                <el-select v-model="tableData[0].sbpz_sbid" placeholder="" style="width: 200px; height: 30px">
                  <el-option v-for="(item, index) in options9" :key="index" :label="item.label" :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="设备名称:" :label-width="formLabelWidth" prop="sbpz_sbmc">
                <el-select v-model="tableData[0].sbpz_sbmc" placeholder="" style="width: 200px; height: 30px">
                  <el-option v-for="(item, index) in options10" :key="index" :label="item.label" :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="MAC地址:" :label-width="formLabelWidth" prop="sbpz_mac">
                <el-input v-model="tableData[0].sbpz_mac" placeholder="" style="width: 200px; height: 30px"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item>
                <!-- 需要修改的地方 -->
                <el-checkbox v-model="tableData[0].sbpz_jysb">禁用地址</el-checkbox>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="基地址选择:">
                <span class="one">1</span>
                <el-switch v-model="tableData[0].sbpz_jdzxz"> </el-switch>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">保 存</el-button>
          <el-button @click="edit_device = false">取 消</el-button>
        </span>
      </el-dialog>
    </div>

    <div class="gateway">
      <el-dialog title="新建网关" :visible.sync="outerVisible" width="382px">
        <el-form :model="tableData[0]" :rules="rulesGate" ref="gateWay" label-width="85px" class="demo-ruleForm">
          <el-form-item label="网关名称:" prop="wgpz_wgmc">
            <el-select v-model="tableData[0].wgpz_wgmc" placeholder="请选择" style="width: 200px">
              <el-option v-for="item in opts1" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="网关ID:" prop="wgpz_wgid">
            <el-select v-model="tableData[0].wgpz_wgid" placeholder="请选择" style="width: 200px">
              <el-option v-for="item in opts2" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="网关类型:" prop="wgpz_wglx">
            <el-select v-model="tableData[0].wgpz_wglx" placeholder="请选择" style="width: 200px">
              <el-option v-for="item in opts3" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer-outer">
          <el-button type="primary" @click="submitGate">保存</el-button>
          <el-button @click="outerVisible = false">取 消</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { SaveUserAnswer, GetUserAnswer, GetTree, SendTree } from "@/api/user";
export default {
  name: "CollectionView",

  data() {
    return {
      loading: false,
      flag: false,
      gateway: "",
      user_id: "",
      trees: [],
      nodekey: "",
      tableData: [
        {
          cjdpz_tagid: "",
          cjdpz_mc: "",
          cjdpz_sjq: "",
          cjdpz_jcqdz: "",
          cjdpz_cjsjlx: "",
          cjdpz_zjx: "",
          cjdpz_dxsx: "",
          cjdpz_beilv: "",
        },
      ],
      formData: {
        ippz_wangkou: "",
        ippz_zwym: "",
        ippz_mrwg: "",
        ippz_dns_sx: "",
        ippz_dns_by: "",
        ippz_ip: "",
        ippz_mac: "",
        wgpz_wgmc: "",
        wgpz_wgid: "",
        wgpz_wglx: "",
        tdpz_tdmc: "",
        tdpz_td: "",
        tdpz_qdmc: "",
        tdpz_ip: "",
        tdpz_port: "",
        tdpz_cssj: "",
        tdpz_jgsj: "",
        tdpz_lxsj: "",
        tdpz_cfcs: "",
        tdpz_watchtime: "",
        tdpz_cjsylxdz: "",
        tdpz_dxzkb: "",
        tdpz_050F: "",
        tdpz_0610: "",
        sbpz_sbid: "",
        sbpz_sbmc: "",
        sbpz_mac: "",
        sbpz_jysb: "",
        sbpz_jdzxz: "",
        mqtt_qiyong: "",
        mqtt_ip: "",
        mqtt_port: "",
        mqtt_wgsn: "",
        mqtt_sssjfbzt: "",
        mqtt_qos: "",
        mqtt_scms: "",
        mqtt_sczq: "",
        mqtt_sjgl: "",
        mqtt_cjd_sbmc: "",
        mqtt_cjd_tagid: "",
      },
      options1: [
        {
          value: "MachinePamHID909",
          label: "MachinePamHID909",
        },
        {
          value: "MachinePamHID910",
          label: "MachinePamHID910",
        },
        {
          value: "MachinePamHID911",
          label: "MachinePamHID911",
        },
        {
          value: "MachinePamHID912",
          label: "MachinePamHID912",
        },
        {
          value: "MachinePamHID913",
          label: "MachinePamHID913",
        },
        {
          value: "MachinePamHID914",
          label: "MachinePamHID914",
        },
        {
          value: "MachinePamHID915",
          label: "MachinePamHID915",
        },
        {
          value: "MachinePamHID916",
          label: "MachinePamHID916",
        },
        {
          value: "MachinePamHID917",
          label: "MachinePamHID917",
        },
        {
          value: "MachinePamHID918",
          label: "MachinePamHID918",
        },
        {
          value: "MachinePamHID919",
          label: "MachinePamHID919",
        },
        {
          value: "MachinePamHID920",
          label: "MachinePamHID920",
        },
        {
          value: "MachinePamHID921",
          label: "MachinePamHID921",
        },
        {
          value: "MachinePamHID922",
          label: "MachinePamHID922",
        },
        {
          value: "MachinePamHID923",
          label: "MachinePamHID923",
        },
        {
          value: "MachinePamHID924",
          label: "MachinePamHID924",
        },
        {
          value: "MachinePamHID925",
          label: "MachinePamHID925",
        },
        {
          value: "MachinePamHID926",
          label: "MachinePamHID926",
        },
        {
          value: "MachinePamHID927",
          label: "MachinePamHID927",
        },
        {
          value: "MachinePamHID928",
          label: "MachinePamHID928",
        },
        {
          value: "MachinePamHID929",
          label: "MachinePamHID929",
        },
        {
          value: "MachinePamHID930",
          label: "MachinePamHID930",
        },
        {
          value: "MachinePamHID931",
          label: "MachinePamHID931",
        },
        {
          value: "MachinePamHID932",
          label: "MachinePamHID932",
        },
        {
          value: "MachinePamHID933",
          label: "MachinePamHID933",
        },
        {
          value: "MachinePamHID934",
          label: "MachinePamHID934",
        },
        {
          value: "MachinePamHID935",
          label: "MachinePamHID935",
        },
        {
          value: "MachinePamHID936",
          label: "MachinePamHID936",
        },
        {
          value: "MachinePamHID937",
          label: "MachinePamHID937",
        },
        {
          value: "MachinePamHID938",
          label: "MachinePamHID938",
        },
      ],
      options2: [
        {
          value: "急停状态",
          label: "急停状态",
        },
        {
          value: "产量",
          label: "产量",
        },
        {
          value: "相对位置X",
          label: "相对位置X",
        },
        {
          value: "相对位置Y",
          label: "相对位置Y",
        },
        {
          value: "相对位置Z",
          label: "相对位置Z",
        },
        {
          value: "实际位置X",
          label: "实际位置X",
        },
        {
          value: "实际位置Y",
          label: "实际位置Y",
        },
        {
          value: "实际位置Z",
          label: "实际位置Z",
        },
        {
          value: "主轴转速",
          label: "主轴转速",
        },
        {
          value: "当前刀具",
          label: "当前刀具",
        },
        {
          value: "预选刀具",
          label: "预选刀具",
        },
        {
          value: "报警信息",
          label: "报警信息",
        },
        {
          value: "驱动单元电流",
          label: "驱动单元电流",
        },
        {
          value: "负载电流",
          label: "负载电流",
        },
        {
          value: "额定电流",
          label: "额定电流",
        },
        {
          value: "运行时间",
          label: "运行时间",
        },
        {
          value: "加工产品用时",
          label: "加工产品用时",
        },
        {
          value: "工作模式",
          label: "工作模式",
        },
        {
          value: "切削速度",
          label: "切削速度",
        },
        {
          value: "任务信息",
          label: "任务信息",
        },
        {
          value: "使能状态",
          label: "使能状态",
        },
        {
          value: "系统状态",
          label: "系统状态",
        },
        {
          value: "执行任务信息",
          label: "执行任务信息",
        },
        {
          value: "进给倍军",
          label: "进给倍军",
        },
        {
          value: "轴速度",
          label: "轴速度",
        },
        {
          value: "轴角度",
          label: "轴角度",
        },
        {
          value: "进给速度",
          label: "进给速度",
        },
      ],
      options3: [
        {
          value: "0X (Coil Status)",
          label: "0X (Coil Status)",
        },
        {
          value: "1X (Input Status)",
          label: "1X (Input Status)",
        },
        {
          value: "3X (Input Registers)",
          label: "3X (Input Registers)",
        },
        {
          value: "4X (Holding Register)",
          label: "4X (Holding Register)",
        },
      ],
      options5: [
        {
          value: " bool",
          label: "bool",
        },
        {
          value: "bit",
          label: "bit",
        },
        {
          value: "uint16",
          label: "uint16",
        },
        {
          value: "int16",
          label: "int16",
        },
        {
          value: "uint32",
          label: "uint32",
        },
        {
          value: "int32",
          label: "int32",
        },
        {
          value: "float",
          label: "float",
        },
        {
          value: "double",
          label: "double",
        },
      ],
      options6: [
        {
          value: " Null",
          label: "Null",
        },
        {
          value: "Big-endian（4321）",
          label: "Big-endian（4321）",
        },
        {
          value: "Little-endian（1234）",
          label: "Little-endian（1234）",
        },
        {
          value: "Big-endian byte swap（2143）",
          label: "Big-endian byte swap（2143）",
        },
        {
          value: "Little-endian byte swap（3412）",
          label: "Little-endian byte swap（3412）",
        },
      ],
      options7: [
        {
          value: "读写",
          label: "读写",
        },
        {
          value: "只读",
          label: "只读",
        },
        {
          value: "只写",
          label: "只写",
        },
      ],
      options8: [
        {
          value: " 0.1",
          label: "0.1",
        },
        {
          value: "1",
          label: "1",
        },
        {
          value: "10",
          label: "10",
        },
        {
          value: "100",
          label: "100",
        },
      ],
      options9: [
        {
          value: "有线区加工设备1",
          label: "有线区加工设备1",
        },
        {
          value: "无线区加工设备1",
          label: "无线区加工设备1",
        },
      ],
      options10: [
        {
          value: "有线区精雕1",
          label: "有线区精雕1",
        },
        {
          value: "无线区精雕1",
          label: "无线区精雕1",
        },
      ],
      labelPosition: "left",
      formLabelWidth: "116px",
      add_passage: true,
      edit_device: false,
      rightMenu: "",
      dialogVisible: true,
      menuShow: false,
      // formdata: {
      //     sbpz_sbid: "",
      //     sbpz_sbmc: "",
      //     sbpz_mac: "3C:2D:50:20:30:3D",
      //     sbpz_jysb: false,
      //     sbpz_jdzxz: true,
      // },
      selectlabel: "",
      datas: [
        {
          label: "Gateway",
          children: [
            {
              label: "数据采集",
              children: [
                {
                  label: "I/O点",
                },
                {
                  label: "用户点",
                },
                {
                  label: "计算点",
                },
                {
                  label: "系统点",
                },
              ],
            },
            {
              label: "数据服务",
              children: [
                {
                  label: "Modbus",
                },
              ],
            },
            {
              label: "IOT",
              children: [
                {
                  label: "Mqtt Client",
                },
                {
                  label: "Alink",
                },
                {
                  label: "Tlink",
                },
                {
                  label: "loTDDC",
                },
                {
                  label: "Ulink",
                },
              ],
            },
          ],
        },
      ],
      defaultProps: {
        children: "children",
        label: "label",
      },
      rules: {
        sbpz_sbid: [
          { required: true, message: "请输入设备ID", trigger: "blur" },
        ],
        sbpz_sbmc: [
          { required: true, message: "请输入设备名称", trigger: "blur" },
        ],
        sbpz_mac: [
          { required: true, message: "请输入设备地址", trigger: "blur" },
        ],
      },

      // 网关配置
      outerVisible: false,
      // gateWay: {
      //     wgpz_wgmc: "",
      //     wgpz_wgid: "",
      //     wgpz_wglx: "",
      // },
      rulesGate: {
        wgpz_wgmc: [
          { required: true, message: "请选择网关名称", trigger: "blur" },
        ],
        wgpz_wgid: [
          { required: true, message: "请选择网关id", trigger: "blur" },
        ],
        wgpz_wglx: [
          { required: true, message: "请选择网关类型", trigger: "blur" },
        ],
      },
      opts1: [
        {
          value: "有线区网关1",
          label: "有线区网关1",
        },
        {
          value: "无线区网关1",
          label: "无线区网关1",
        },
      ],
      opts2: [
        {
          value: "LAN-BOX1",
          label: "LAN-BOX1",
        },
        {
          value: "WLAN-BOX1",
          label: "WLAN-BOX1",
        },
      ],
      opts3: [
        {
          value: "loT Lite网关",
          label: "loT Lite网关",
        },
        {
          value: "MODBUS网关",
          label: "MODBUS网关",
        },
        {
          value: "BACNET网关",
          label: "BACNET网关",
        },
        {
          value: "OPC UA网关",
          label: "OPC UA网关",
        },
        {
          value: "OPC DA网关",
          label: "OPC DA网关",
        },
        {
          value: "loT网关",
          label: "loT网关",
        },
      ],
    };
  },
  methods: {
    // 提交时校验
    submitForm() {
      this.$refs["formdata"].validate((valid) => {
        if (valid) {
          this.saveDevice();
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    changeInput() {
      this.flag = false;
      this.trees[0].label = this.gateway;
      this.sendTree();
    },
    async rightClick(e, data, node) {
      console.log(node);
      if (data.editing) {
        this.outerVisible = true;
        this.add_passage = false;
        this.GetUserAnswer();
        // let res2 = await GetUserAnswer({
        //     mark: "DB-IotLite_yx",
        //     user_id: this.user_id,
        //     // user_id: "USER202307301114011710",
        // });
        // if (res2.data.data && res2.data.state) {
        //     console.log(res2.data.data.mark_value);
        //     let obj = JSON.parse(res2.data.data.mark_value);
        //     this.gateWay.wgpz_wgid = obj.wgpz_wgid;
        //     // console.log(this.tableData[0].wgpz_wgid);
        //     this.gateWay.wgpz_wgmc = obj.wgpz_wgmc;
        //     this.gateWay.wgpz_wglx = obj.wgpz_wglx;
        // }
      }
      if (node.level == 5) {
        this.menuShow = false;
        this.menuShow = true;
        this.nodekey = data.id;
        console.log(data.label);
        this.selectlabel = data.label;
        // console.log(node);
        e.preventDefault(); //关闭浏览器右键默认事件
        this.rightMenu = { top: e.pageY + "px", left: e.pageX + "px" };
        document.addEventListener("click", (ev) => {
          ev.stopImmediatePropagation();
          if (ev.target !== document.querySelector(".el-menu-item.is-active")) {
            this.foo();
          }
        });
      }
    },
    // 点击时候的判断,判断是否为编辑
    async selectMenuNode(index, indexPath) {
      console.log(index);
      if (index == 1) {
        this.edit_device = true;
        this.GetUserAnswer();
        // let answer = await GetUserAnswer({
        //     mark: "DB-IotLite_yx",
        //     // user_id: "USER202307301114011710",
        //     user_id: this.user_id,
        // })
        // this.add_passage = false;
        // console.log(answer);
        // let obj = JSON.parse(answer.data.data.mark_value);
        // console.log(obj);
        // if (obj.sbpz_sbid) {
        //     this.formdata.sbpz_sbid = obj.sbpz_sbid;
        //     this.formdata.sbpz_jdzxz = Boolean(Number(obj.sbpz_jdzxz));
        //     this.formdata.sbpz_jysb = Boolean(Number(obj.sbpz_jysb));
        //     this.formdata.sbpz_sbmc = obj.sbpz_sbmc;
        //     this.formdata.sbpz_mac = obj.sbpz_mac;
        // }
      } else if (index == 2) {
        this.$refs["tree"].remove(this.nodekey);
        this.sendTree();
        this.$message({
          message: "删除成功",
          type: "success",
        });
        this.$router.back();
      }
    },
    foo() {
      // 取消鼠标监听事件 菜单栏
      this.menuShow = false;
      document.removeEventListener("click", this.foo); // 关掉监听，
    },
    treeNodeClick(key, keyPath) {
      console.log(key);
      console.log(keyPath);
      // if (keyPath.id == 10 || key.label == "Mqtt Client") {
      //     this.$router.push("./mqttconfig")
      // }
      this.add_passage = true;
    },
    getDriveName() {
      this.diag_drive = true;
    },
    // 保存表单
    async handleSelect() {
      // console.log(this.tableData);
      // let datas = sessionStorage.getItem("DB-IotLite_yx");
      // this.tableData = { ...JSON.parse(datas), ...this.tableData[0] };
      let obj = {
        ippz_wangkou: this.formData.ippz_wangkou,
        ippz_zwym: this.formData.ippz_zwym,
        ippz_mrwg: this.formData.ippz_mrwg,
        ippz_dns_sx: this.formData.ippz_dns_sx,
        ippz_dns_by: this.formData.ippz_dns_by,
        ippz_ip: this.formData.ippz_ip,
        ippz_mac: this.formData.ippz_mac,
        wgpz_wgmc: this.formData.wgpz_wgmc,
        wgpz_wgid: this.formData.wgpz_wgid,
        wgpz_wglx: this.formData.wgpz_wglx,
        tdpz_tdmc: this.formData.tdpz_tdmc,
        tdpz_td: this.formData.tdpz_td,
        tdpz_qdmc: this.formData.tdpz_qdmc,
        tdpz_ip: this.formData.tdpz_ip,
        tdpz_port: this.formData.tdpz_port,
        tdpz_cssj: this.formData.tdpz_cssj,
        tdpz_jgsj: this.formData.tdpz_jgsj,
        tdpz_lxsj: this.formData.tdpz_lxsj,
        tdpz_cfcs: this.formData.tdpz_cfcs,
        tdpz_watchtime: this.formData.tdpz_watchtime,
        tdpz_cjsylxdz: this.formData.tdpz_cjsylxdz,
        tdpz_dxzkb: this.formData.tdpz_dxzkb,
        tdpz_050F: this.formData.tdpz_050F,
        tdpz_0610: this.formData.tdpz_0610,
        sbpz_sbid: this.formData.sbpz_sbid,
        sbpz_sbmc: this.formData.sbpz_sbmc,
        sbpz_mac: this.formData.sbpz_mac,
        sbpz_jysb: this.formData.sbpz_jysb,
        sbpz_jdzxz: this.formData.sbpz_jdzxz,
        mqtt_qiyong: this.formData.mqtt_qiyong,
        mqtt_ip: this.formData.mqtt_ip,
        mqtt_port: this.formData.mqtt_port,
        mqtt_wgsn: this.formData.mqtt_wgsn,
        mqtt_sssjfbzt: this.formData.mqtt_sssjfbzt,
        mqtt_qos: this.formData.mqtt_qos,
        mqtt_scms: this.formData.mqtt_scms,
        mqtt_sczq: this.formData.mqtt_sczq,
        mqtt_sjgl: this.formData.mqtt_sjgl,
        mqtt_cjd_sbmc: this.formData.mqtt_cjd_sbmc,
        mqtt_cjd_tagid: this.formData.mqtt_cjd_tagid,
        cjdpz_tagid: this.tableData[0].cjdpz_tagid,
        cjdpz_mc: this.tableData[0].cjdpz_mc,
        cjdpz_sjq: this.tableData[0].cjdpz_sjq,
        cjdpz_jcqdz: this.tableData[0].cjdpz_jcqdz,
        cjdpz_cjsjlx: this.tableData[0].cjdpz_cjsjlx,
        cjdpz_zjx: this.tableData[0].cjdpz_zjx,
        cjdpz_dxsx: this.tableData[0].cjdpz_dxsx,
        cjdpz_beilv: this.tableData[0].cjdpz_beilv,
      }
      let res = await SaveUserAnswer({
        mark: "DB-IotLite_yx",
        user_id: this.user_id,
        // user_id: "USER202307301114011710",
        mark_value: JSON.stringify(obj),
      });
      if (res.data.state) {
        this.$message({
          message: "保存成功",
          type: "success",
        });
        console.log(res);
        sessionStorage.setItem(
          "DB-IotLite_yx",
          JSON.stringify(obj)
        );
        // console.log(this.tableData);
      } else {
        this.$message({
          message: `保存失败`,
          type: "error",
        });
      }
      // }
    },
    closeMenu(ev) {
      console.log({ ev });
      this.dialogVisible = false;
    },
    async GetUserAnswer() {
      let answer = await GetUserAnswer({
        mark: "DB-IotLite_yx",
        user_id: this.user_id,
        // user_id: "USER202307301114011710",
      });
      if (answer.data.state && answer.data.data) {
        console.log(answer);
        let obj = JSON.parse(answer.data.data.mark_value);
        console.log(obj);
        if (obj.cjdpz_tagid) {
          this.tableData[0].cjdpz_tagid = obj.cjdpz_tagid;
          this.tableData[0].cjdpz_mc = obj.cjdpz_mc;
          this.tableData[0].cjdpz_sjq = obj.cjdpz_sjq;
          this.tableData[0].cjdpz_jcqdz = obj.cjdpz_jcqdz;
          this.tableData[0].cjdpz_cjsjlx = obj.cjdpz_cjsjlx;
          this.tableData[0].cjdpz_zjx = obj.cjdpz_zjx;
          this.tableData[0].cjdpz_dxsx = obj.cjdpz_dxsx;
          this.tableData[0].cjdpz_beilv = obj.cjdpz_beilv;
        }
        this.formData.ippz_wangkou = obj.ippz_wangkou;
        this.tableData[0].ippz_mrwg = obj.ippz_mrwg;
        this.tableData[0].ippz_ip = obj.ippz_ip;
        this.tableData[0].ippz_zwym = obj.ippz_zwym;
        this.tableData[0].ippz_dns_sx = obj.ippz_dns_sx;
        this.tableData[0].ippz_dns_by = obj.ippz_dns_by;
        this.tableData[0].wgpz_wgmc = obj.wgpz_wgmc;
        this.tableData[0].wgpz_wgid = obj.wgpz_wgid;
        this.tableData[0].wgpz_wglx = obj.wgpz_wglx;
        this.tableData[0].tdpz_tdmc = obj.tdpz_tdmc;
        this.tableData[0].tdpz_td = obj.tdpz_td;
        this.tableData[0].tdpz_qdmc = obj.tdpz_qdmc;
        this.tableData[0].tdpz_ip = obj.tdpz_ip;
        this.tableData[0].tdpz_port = obj.tdpz_port;
        this.tableData[0].tdpz_cssj = obj.tdpz_cssj;
        this.tableData[0].tdpz_jgsj = obj.tdpz_jgsj;
        this.tableData[0].tdpz_lxsj = obj.tdpz_lxsj;
        this.tableData[0].tdpz_cfcs = obj.tdpz_cfcs;
        this.tableData[0].tdpz_watchtime = obj.tdpz_watchtime;
        this.tableData[0].tdpz_cjsylxdz = obj.tdpz_cjsylxdz;
        this.tableData[0].tdpz_dxzkb = obj.tdpz_dxzkb;
        this.tableData[0].tdpz_050F = obj.tdpz_050F;
        this.tableData[0].tdpz_0610 = obj.tdpz_0610;
        this.tableData[0].sbpz_sbid = obj.sbpz_sbid;
        this.tableData[0].sbpz_sbmc = obj.sbpz_sbmc;
        this.tableData[0].sbpz_mac = obj.sbpz_mac;
        this.tableData[0].sbpz_jysb = Boolean(Number(obj.sbpz_jysb));
        this.tableData[0].sbpz_jdzxz = Boolean(Number(obj.sbpz_jdzxz));
        this.tableData[0].mqtt_qiyong = obj.mqtt_qiyong;
        this.tableData[0].mqtt_ip = obj.mqtt_ip;
        this.tableData[0].mqtt_port = obj.mqtt_port;
        this.tableData[0].mqtt_wgsn = obj.mqtt_wgsn;
        this.tableData[0].mqtt_sssjfbzt = obj.mqtt_sssjfbzt;
        this.tableData[0].mqtt_qos = obj.mqtt_qos;
        this.tableData[0].mqtt_scms = obj.mqtt_scms;
        this.tableData[0].mqtt_sczq = obj.mqtt_sczq;
        this.tableData[0].mqtt_sjgl = obj.mqtt_sjgl;
        this.tableData[0].mqtt_cjd_sbmc = obj.mqtt_cjd_sbmc;
        this.tableData[0].mqtt_cjd_tagid = obj.mqtt_cjd_tagid;
      }
    },
    async sendTree() {
      let result = await SendTree({
        user_id: this.user_id,
        // user_id: "USER202307301114011710",
        data: JSON.stringify(this.trees),
        type: "1",
      });
      console.log(result);
    },
    // 保存设备的属性
    async saveDevice() {
      let datas = sessionStorage.getItem("DB-IotLite_yx");
      console.log(datas);
      var obj = {
        sbpz_sbid: this.tableData[0].sbpz_sbid,
        sbpz_sbmc: this.tableData[0].sbpz_sbmc,
        sbpz_mac: this.tableData[0].sbpz_mac,
        sbpz_jysb: String(Number(this.tableData[0].sbpz_jysb)),
        sbpz_jdzxz: String(Number(this.tableData[0].sbpz_jdzxz)),
      };
      datas = { ...this.tableData[0], ...obj };
      this.add_passage = false;
      let res4 = await SaveUserAnswer({
        mark: "DB-IotLite_yx",
        user_id: this.user_id,
        // user_id: "USER202307301114011710",
        mark_value: JSON.stringify(datas),
      });
      if (res4.data.state) {
        // console.log(res4.data.state);
        this.$message({
          message: "更改成功",
          type: "success",
        });
        sessionStorage.setItem("DB-IotLite_yx", JSON.stringify(datas));
        this.updateLabelById(this.nodekey, this.tableData[0].sbpz_sbmc);
        this.sendTree();
      } else {
        this.$message({
          message: `更改失败,原因是${res4.data.message}`,
          type: "error",
        });
      }
    },
    // 修改树形控件的名字
    findNodeById(nodes, targetId) {
      let foundNode = null;
      const findNode = (nodes) => {
        for (const node of nodes) {
          if (node.id === targetId) {
            // 找到节点
            foundNode = node;
            return;
          }
          if (node.children) {
            findNode(node.children);
          }
        }
      };

      findNode(nodes);
      return foundNode;
    },
    updateLabelById(targetId, newLabel) {
      const targetNode = this.findNodeById(this.trees, targetId);
      // console.log(targetId);
      // console.log(targetNode);
      // console.log(newLabel);
      if (targetNode) {
        // 找到节点，更新label值
        // console.log(targetNode);
        // console.log(newLabel);
        this.$set(targetNode, "label", newLabel);
        // 强制更新视图
        this.$forceUpdate();
        // this.getTree();
      }
    },
    async getTree() {
      let res = await GetTree({
        user_id: this.user_id,
        // user_id: "USER202307301114011710",
        type: "1",
      });
      console.log(res);
      console.log(JSON.parse(res.data.data.data));
      this.trees = JSON.parse(res.data.data.data);
    },

    //网关信息配置
    async submitGate() {
      this.$refs["gateWay"].validate((valid) => {
        if (valid) {
          // console.log('submit!');
          this.saveGates();
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    async saveGates() {
      this.outerVisible = false;
      // this.innerVisible = false;
      // let datas = sessionStorage.getItem("DB-IotLite_yx");
      // console.log(JSON.parse(datas));
      // datas = { ...JSON.parse(datas), ...this.gateWay };
      // console.log(datas);
      let res = await SaveUserAnswer({
        mark: "DB-IotLite_yx",
        user_id: this.user_id,
        // user_id: "USER202307301114011710",
        mark_value: JSON.stringify(this.tableData[0]),
        // step: "3",
      });
      if (res.data.state) {
        this.$message({
          message: "修改成功",
          type: "success",
        });
        this.trees[0].label = this.tableData[0].wgpz_wgid;
        this.sendTree();
        sessionStorage.setItem(
          "DB-IotLite_yx",
          JSON.stringify(this.tableData[0])
        );
      } else {
        this.$message({
          message: `保存失败,原因是${res.data.message}`,
          type: "error",
        });
      }
      console.log(res);
    },
  },
  created() {
    console.log(this.$route.query.user_id);
    this.user_id = this.$route.query.user_id;
    // this.user_id = this.$store.state.user_id;
    this.getTree();
    this.loading = true;
    this.GetUserAnswer();
    this.loading = false;
  },
};
</script>

<style lang="less" scoped>
.tree_menu {
  position: absolute;
  width: 286px;
  height: 753px;
  left: 64px;
  top: 104px;
  width: 286px;
  height: 753px;
  line-height: 20px;
  border-radius: 3px;
  background-color: rgba(255, 255, 255, 1);
  text-align: center;
  border: 1px solid rgba(220, 220, 220, 1);
}

.tree_rightmenu {
  position: fixed;
  width: 120px;
  border: 1px solid rgba(0, 0, 0, 0.15);
  box-shadow: 0 6px 12px rgba(0, 0, 0, 0.175);
  z-index: 1000;
}

/deep/ .pas_params {
  position: absolute;
  left: 362px;
  top: 125px;
  width: 1066px;
  height: 716px;
  line-height: 20px;
  border-radius: 3px;
  background-color: rgba(255, 255, 255, 1);
  // text-align: center;
  border: 1px solid rgba(220, 220, 220, 1);

  .el-button {
    width: 80px;
    height: 30px;
    line-height: 0.5;
  }

  .el-col-12:last-child {
    padding-right: 150px;
  }

  .el-col-12:first-child {
    padding-right: 54px;
  }

  .el-row {
    height: 40px;
    display: flex;
    align-items: flex-start;

    .el-col-1 {
      text-align: center;
      line-height: 20px;
      height: 20px;
    }

    .el-col-9 {
      padding-left: 30px;
    }
  }

  .el-input__inner {
    height: 30px;
    text-align: left;
    padding: 0px 8px;
  }

  .content {
    position: absolute;
    left: 377px;
    top: 69px;
    width: 1220px;
    height: 836px;
    line-height: 20px;
    padding-left: 36px;
    background-color: rgba(255, 255, 255, 1);
    color: rgba(16, 16, 16, 1);
    font-size: 14px;
    border: 1px solid rgba(187, 187, 187, 1);
    display: flex;
    justify-content: flex-start;
    align-items: flex-start;

    .el-form-item {
      margin-bottom: 0px;
    }

    /deep/ .con_left {
      .el-con-5 {
        width: 43px;
      }
    }

    .con_left {
      width: 709px;
      position: relative;

      .con_left_footer {
        position: absolute;
        left: 499px;
        top: 777px;
      }
    }

    .con_right {
      width: 480px;

      .dialog-footer {
        .el-button {
          width: 80px;
          height: 30px;
          line-height: 0.5;
        }
      }
    }
  }
}

.one {
  z-index: 10000;
  position: absolute;
  left: 95px;
  top: 0px;
}
</style>
