<!-- 采集点收集 -->
<template>
  <div>
    <!-- 设置树形控件 -->
    <!-- <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>
    <div class="pas_params" v-show="add_passage" v-loading="loading">
      <div>
        <el-button type="info" plain @click="handleSelect">保存</el-button>
        <!-- <el-menu :default-active="activeIndex" class="el-menu-demo" mode="horizontal" @select="handleSelect">
          <el-menu-item index="2">保存</el-menu-item>
        </el-menu> -->
      </div>
      <el-table ref="singleTable" :data="tableData" style="width: 100%">
        <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_jcqdz" label="地址">
          <template slot-scope="scope">
            <el-select v-model="scope.row.cjdpz_dz" 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-select v-model="scope.row.cjdpz_jcqdz" placeholder="1">
              <el-option v-for="(option,index) in options4" :key="index" :label="option.label" :value="option.value"></el-option>
            </el-select> -->
            <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_dxsx" label="读写属性">
          <template slot-scope="scope">
            <el-select v-model="scope.row.cjdpz_dxsx" 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_scms" label="上传模式">
          <template slot-scope="scope">
            <el-select v-model="scope.row.cjdpz_scms" 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_dw" label="单位">
          <template slot-scope="scope">
            <el-input v-model="scope.row.cjdpz_dw" placeholder=""></el-input>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 网关信息 -->
    <div class="gateway">
      <el-dialog title="新建网关" :visible.sync="outerVisible" width="382px">
        <el-form :model="gateWay" :rules="rulesGate" ref="gateWay" label-width="85px" class="demo-ruleForm">
          <el-form-item label="网关名称:" prop="wgpz_wgmc">
            <el-select v-model="gateWay.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="gateWay.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>
        <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: "ChiCollectionwifi",

  data() {
    return {
      user_id: "",
      loading: false,
      flag: false,
      gateway: "",
      trees: [],
      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)"
      }],
      options4: [
        {
          value: "2000H",
          label: "2000H",
        },
        {
          value: "2002H",
          label: "2002H",
        },
        {
          value: "2004H",
          label: "2004H",
        },
        {
          value: "2005H",
          label: "2005H",
        },
        {
          value: "2006H",
          label: "2006H",
        },
        {
          value: "2007H",
          label: "2007H",
        },
        {
          value: "2008H",
          label: "2008H",
        },
        {
          value: "2009H",
          label: "2009H",
        },
        {
          value: "2010H",
          label: "2010H",
        },
        {
          value: "2011H",
          label: "2011H",
        },
        {
          value: "2012H",
          label: "2012H",
        },
        {
          value: "2013H",
          label: "2013H",
        },
        {
          value: "2014H",
          label: "2014H",
        },
        {
          value: "2015H",
          label: "2015H",
        },
        {
          value: "2016H",
          label: "2016H",
        },
        {
          value: "2017H",
          label: "2017H",
        },
        {
          value: "2018H",
          label: "2018H",
        },
        {
          value: "2019H",
          label: "2019H",
        },
        {
          value: "2020H",
          label: "2020H",
        }, {
          value: "2021H",
          label: "2021H",
        },
        {
          value: "2022H",
          label: "2022H",
        },
        {
          value: "2023H",
          label: "2023H",
        },
        {
          value: "2024H",
          label: "2024H",
        },
        {
          value: "2027H",
          label: "2027H",
        },
        {
          value: "2028H",
          label: "2028H",
        },
        {
          value: "2029H",
          label: "2029H",
        },
        {
          value: "2030H",
          label: "2030H",
        }, {
          value: "2031H",
          label: "2031H",
        }
      ],
      options5: [
        {
          value: " BIT（位0/1）",
          label: " BIT（位0/1）"
        },
        {
          value: "WORD（16位无符号整数）",
          label: "WORD（16位无符号整数）"
        }, {
          value: "INT（16位有符号整数）",
          label: "INT（16位有符号整数）"
        },
        {
          value: "DWORD（32位无符号整数）",
          label: "DWORD（32位无符号整数）"
        },
        {
          value: "DINT（32位有符号整数）",
          label: "DINT（32位有符号整数）"
        }, {
          value: "FLOAT（单精度浮点数）",
          label: "FLOAT（单精度浮点数）"
        }, {
          value: "DOUBLE（64位浮点数）",
          label: "DOUBLE（64位浮点数）"
        }, {
          value: "STRING（8位字符串）",
          label: "STRING（8位字符串）"
        }, {
          value: "BCD16（16位BCD码）",
          label: "BCD16（16位BCD码）"
        }, {
          value: "ULONG（64位无符号整数）",
          label: "ULONG（64位无符号整数）"
        }, {
          value: "LONG（64位有符号整数）",
          label: "LONG（64位有符号整数）"
        },
      ],
      options6: [{
        value: "读写",
        label: "读写"
      },
      {
        value: "只读",
        label: "只读"
      }, {
        value: "只写",
        label: "只写"
      }],
      options7: [{
        value: "Periodic",
        label: "Periodic"
      },
      {
        value: "Onchange",
        label: "Onchange"
      }, {
        value: "Never",
        label: "Never"
      }],
      activeIndex: "1",
      checked: "",
      labelPosition: "left",
      formLabelWidth: "116px",
      input1: 192,
      add_passage: false,
      rightMenu: "",
      dialogVisible: true,
      menuShow: false,
      datas: [{
        label: 'Gateway',
        children: [{
          label: '数据采集',
          children: [{
            label: 'I/O点'
          },
          {
            label: '用户点',
          },
          {
            label: '计算点',
          },
          {
            label: '系统点',
          },
          ]
        },
        {
          label: "上云服务",
          children: [
            {
              label: "标准 MQTT"
            },
            {
              label: "AWS loT"
            }]
        }]
      }],
      defaultProps: {
        children: "children",
        label: "label",
      },
      tableData: [
        {
          cjdpz_tagid: "MachinePamHID909",
          cjdpz_mc: "急停状态",
          cjdpz_dz: "0X (Coil Status)",
          cjdpz_jcqdz: "",
          cjdpz_cjsjlx: "BIT（位0/1）",
          cjdpz_dxsx: "读写",
          cjdpz_scms: "Periodic",
          cjdpz_dw: "",
        },
      ],
      // 网关配置的出现
      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" },
        ],
      },
      opts1: [
        {
          value: "有线区网关1",
          label: "有线区网关1",
        },
        {
          value: "无线区网关1",
          label: "无线区网关1",
        },
      ],
      opts2: [
        {
          value: "LAN-BOX1",
          label: "LAN-BOX1",
        },
        {
          value: "WLAN-BOX1",
          label: "WLAN-BOX1",
        },
      ],
    };
  },
  methods: {
    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;
        let res2 = await GetUserAnswer({
          mark: "CHGYWG_wx",
          user_id: this.user_id,
          // user_id: "USER202307301114011710",
        });
        if (res2.data.data && res2.data.state) {
          console.log(res2.data.data.mark_value);
          let res3 = JSON.parse(res2.data.data.mark_value);
          this.gateWay.wgpz_wgid = res3.wgpz_wgid;
          // console.log(this.formData.wgpz_wgid);
          this.gateWay.wgpz_wgmc = res3.wgpz_wgmc;
        }
      }
    },
    treeNodeClick() {
      this.add_passage = true;
    },
    getDriveName() {
      this.diag_drive = true;
    },
    async handleSelect() {
      let datas = sessionStorage.getItem("CHGYWG_wx");
      // console.log(JSON.parse(datas));
      datas = { ...JSON.parse(datas), ...this.tableData[0] };
      let res = await SaveUserAnswer({
        mark: "CHGYWG_wx",
        user_id: this.user_id,
        mark_value: JSON.stringify(datas),
      });
      if (res.data.state) {
        this.$message({
          message: "保存成功",
          type: "success",
        });
        console.log(res);
        sessionStorage.setItem("CHGYWG_wx", JSON.stringify(datas));
      } else {
        this.$message({
          message: `保存失败`,
          type: "error",
        });
      }

    },
    async GetUserAnswer() {
      this.loading = true;
      let { data } = await GetUserAnswer({
        mark: "CHGYWG_wx",
        user_id: this.user_id,
      });
      this.loading = false;
      if (data.data && data.state) {
        // console.log(JSON.parse(data.data.mark_value));
        let obj = JSON.parse(data.data.mark_value);
        if (obj.cjdpz_tagid) {
          this.tableData[0].cjdpz_tagid = obj.cjdpz_tagid;
          this.tableData[0].cjdpz_mc = obj.cjdpz_mc;
          this.tableData[0].cjdpz_dz = obj.cjdpz_dz;
          this.tableData[0].cjdpz_jcqdz = obj.cjdpz_jcqdz;
          this.tableData[0].cjdpz_cjsjlx = obj.cjdpz_cjsjlx;
          this.tableData[0].cjdpz_dxsx = obj.cjdpz_dxsx;
          this.tableData[0].cjdpz_scms = obj.cjdpz_scms;
          this.tableData[0].cjdpz_dw = obj.cjdpz_dw;
        }
      }
    },
    async sendTree() {
      let result = await SendTree({
        user_id: this.user_id,
        data: JSON.stringify(this.trees),
        type: "4",
      })
      console.log(result);
    },

    //网关配置的信息
    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("CHGYWG_wx");
      // console.log(JSON.parse(datas));
      datas = { ...JSON.parse(datas), ...this.gateWay };
      console.log(datas);
      let res = await SaveUserAnswer({
        mark: "CHGYWG_wx",
        user_id: this.user_id,
        // user_id: "USER202307301114011710",
        mark_value: JSON.stringify(datas),
      });
      if (res.data.state) {
        this.$message({
          message: "修改成功",
          type: "success",
        });
        this.trees[0].label = this.gateWay.wgpz_wgid;
        this.sendTree();
        sessionStorage.setItem("CHGYWG_wx", JSON.stringify(this.gateWay));
      } else {
        this.$message({
          message: `保存失败,原因是${res.data.message}`,
          type: "error",
        });
      }
      console.log(res);
    }
  },
  created() {
    this.user_id = this.$route.query.user_id;
    GetTree({
      user_id: this.user_id,
      type: "4",
    }).then((res) => {
      console.log(res);
      console.log(JSON.parse(res.data.data.data));
      this.trees = JSON.parse(res.data.data.data);
    })
    this.GetUserAnswer();
  }
};
</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);
}

/deep/ .pas_params {
  position: absolute;
  left: 362px;
  top: 95px;
  width: 1066px;
  height: 752px;
  line-height: 20px;
  border-radius: 3px;
  color: white;
  background-image: url(../assets/image/bg.png);
  background-size: 100% 100%;
  // 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;
        }
      }
    }
  }
}
</style>
