<!-- 串口通道配置 -->
<template>
  <div>
    <!-- 设置树形控件 -->
    <!-- 串口通道的页面 -->
    <!-- <el-button type="info" plain @click="dialogVisible = true">串口通道</el-button> -->
    <div class="tree_menu" v-show="dialogVisible">
      <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">{{ $t('addChannel') }}</span>
          </el-menu-item>
          <el-menu-item index="2" style="font-size: 12px">
            <span slot="title">{{ $t('cancel') }}</span>
          </el-menu-item>
        </el-menu>
      </div>
    </div>
    <div :class="$i18n.locale==='en'?'pas_params':'zh pas_params'">
      <el-dialog
        :title="$t('channelconfig.title')"
        :visible.sync="add_passage"
        width="765px"
        v-loading="loading"
      >
        <el-form
          :model="formData"
          :label-position="labelPosition"
          :rules="rules"
          ref="formData"
        >
          <el-row>
            <el-col :span="12">
              <el-form-item
                  :label="$t('channelconfig.name')"
                :label-width="formLabelWidth"
                prop="tdpz_tdmc"
              >
                <el-select
                  v-model="formData.tdpz_tdmc"
                  placeholder=""
                  style="width: 200px; height: 30px"
                >
                  <el-option
                    v-for="item in options1"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item
                  :label="$t('channelconfig.channel')"
                :label-width="formLabelWidth"
                prop="tdpz_td"
              >
                <el-select
                  v-model="formData.tdpz_td"
                  placeholder=""
                  style="width: 200px; height: 30px"
                >
                  <el-option
                    v-for="item in options2"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item
                  :label="$t('channelconfig.driverName')"
                :label-width="formLabelWidth"
                prop="tdpz_qdmc"
              >
                <el-row>
                  <el-col :span="18">
                    <el-input
                      v-model="formData.tdpz_qdmc"
                      placeholder=""
                      style="width: 200px; height: 30px"
                      disabled
                    ></el-input>
                    <el-button v-if="$i18n.locale==='zh'"
                               style="width: 50px; height: 30px;position: absolute;right:-4px;top:4px"
                               @click="getDriveName"
                    >...</el-button
                    >
                  </el-col>
                  <el-col :span="6">
                    <el-button
                        v-if="$i18n.locale==='en'"
                      style="width: 50px; height: 30px"
                      @click="getDriveName"
                      >...</el-button
                    >
                  </el-col>
                </el-row>
              </el-form-item>
            </el-col>
          </el-row>
          <!-- <div ></div> -->
          <div v-if="formData.tdpz_td == '网口'">
            <el-row>
              <el-col :span="12">
                <el-form-item label="IP地址:" :label-width="formLabelWidth">
                  <el-row>
                    <el-col :span="5">
                      <el-input
                        v-model="tdpz_ip1"
                        style="width: 43px; height: 30px"
                      ></el-input>
                    </el-col>
                    <el-col :span="1" style="color: #fff">. </el-col>
                    <el-col :span="5">
                      <el-input
                        v-model="tdpz_ip2"
                        style="width: 43px; height: 30px"
                      ></el-input>
                    </el-col>
                    <el-col :span="1" style="color: #fff">.</el-col>
                    <el-col :span="5">
                      <el-input
                        v-model="tdpz_ip3"
                        style="width: 43px; height: 30px"
                      ></el-input>
                    </el-col>
                    <el-col :span="1" style="color: #fff">.</el-col>
                    <el-col :span="5">
                      <el-input
                        v-model="tdpz_ip4"
                        style="width: 43px; height: 30px"
                      ></el-input>
                    </el-col>
                  </el-row>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="端口号:" :label-width="formLabelWidth">
                  <el-input
                    v-model="tdpz_port"
                    placeholder=""
                    style="width: 200px; height: 30px"
                  ></el-input>
                </el-form-item>
              </el-col>
            </el-row>
          </div>
          <div v-else>
            <el-row>
              <el-col :span="12">
                <el-form-item
                  :label=" $t('channelconfig.serialPort')"
                  :label-width="formLabelWidth"
                  prop="tdpz_chuankou"
                >
                  <el-select
                    v-model="formData.tdpz_chuankou"
                    placeholder=""
                    style="width: 200px; height: 30px"
                  >
                    <el-option
                      v-for="item in options3"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item
                  :label="$t('channelconfig.baudRate')"
                  :label-width="formLabelWidth"
                  prop="tdpz_btl"
                >
                  <el-select
                    v-model="formData.tdpz_btl"
                    placeholder=""
                    style="width: 200px; height: 30px"
                  >
                    <el-option
                      v-for="item in options4"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item
                  :label="$t('channelconfig.dataBits')"
                  :label-width="formLabelWidth"
                  prop="tdpz_sjw"
                >
                  <el-select
                    v-model="formData.tdpz_sjw"
                    placeholder=""
                    style="width: 200px; height: 30px"
                  >
                    <el-option
                      v-for="item in options5"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item
                    :label="$t('channelconfig.stopBit')"
                  :label-width="formLabelWidth"
                  prop="tdpz_tzw"
                >
                  <el-select
                    v-model="formData.tdpz_tzw"
                    placeholder=""
                    style="width: 200px; height: 30px"
                  >
                    <el-option
                      v-for="item in options6"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item
                    :label="$t('channelconfig.parityCheck')"
                  :label-width="formLabelWidth"
                  prop="tdpz_jojy"
                >
                  <el-select
                    v-model="formData.tdpz_jojy"
                    placeholder=""
                    style="width: 200px; height: 30px"
                  >
                    <el-option
                      v-for="item in options7"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
          </div>
          <el-row>
            <el-col :span="12">
              <el-form-item
                :label="$t('channelconfig.overTime')"
                :label-width="formLabelWidth"
                prop="tdpz_cgsj"
              >
                <el-input
                  v-model="formData.tdpz_cgsj"
                  placeholder=""
                  style="width: 200px; height: 30px"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item
                  :label="$t('channelconfig.intervalTime')"
                :label-width="formLabelWidth"
                prop="tdpz_jgsj"
              >
                <el-input
                  v-model="formData.tdpz_jgsj"
                  placeholder=""
                  style="width: 200px; height: 30px"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item
                  :label="$t('channelconfig.pollTime')"
                  :label-width="formLabelWidth"
                prop="tdpz_lxsj"
              >
                <el-input
                  v-model="formData.tdpz_lxsj"
                  placeholder=""
                  style="width: 200px; height: 30px"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item
                  :label="$t('channelconfig.retryCount')"
                :label-width="formLabelWidth"
                prop="tdpz_cfcs"
              >
                <el-input
                  v-model="formData.tdpz_cfcs"
                  placeholder=""
                  style="width: 200px; height: 30px"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div class="super_parmams">
          <h3>{{ $t('channelconfig.title1') }}</h3>
          <div>
            <i class="el-icon-caret-bottom" v-if="is_retract"></i>
            <i class="el-icon-caret-top" v-else="is_retract"></i>
            <el-button
              type="text"
              plain
              @click="getRetract"
              style="

                border: none;
                color: rgba(16, 16, 16, 1);
                font-size: 14px;
                text-align: left;
              "
            >
              {{ $t('put') }}</el-button
            >
          </div>
        </div>
        <div class="super_con" v-show="is_retract">
          <el-form
            :label-position="labelPosition"
            :rules="rules"
            ref="ruleForm"
          >
            <el-row>
              <el-col :span="12">
                <el-form-item>
                  <el-checkbox v-model="formData.tdpz_watchtime"
                    >watch time(s)</el-checkbox
                  >
                  <el-input
                    v-model="input1"
                    placeholder=""
                    style="width: 200px; height: 30px"
                    :disabled="isdisabled"
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item>
                  <el-checkbox v-model="formData.tdpz_cjsylxdz"
                    >{{ $t('channelconfig.check') }}</el-checkbox
                  >
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item :label="$t('channelconfig.reading')" :label-width="formLabelWidth">
                  <el-row>
                    <el-col :span="10">
                      <el-input
                        v-model="tdpz_dxzkb1"
                        style="width: 91px; height: 30px"
                      ></el-input>
                    </el-col>
                    <el-col :span="4">: </el-col>
                    <el-col :span="10">
                      <el-input
                        v-model="tdpz_dxzkb2"
                        style="width: 91px; height: 30px"
                      ></el-input>
                    </el-col>
                  </el-row>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="05/0F:" :label-width="formLabelWidth">
                  <el-row>
                    <el-col :span="8"
                      ><el-radio v-model="formData.tdpz_050F" label="auto"
                        >auto</el-radio
                      ></el-col
                    >
                    <el-col :span="8"
                      ><el-radio v-model="formData.tdpz_050F" label="05"
                        >05</el-radio
                      ></el-col
                    >
                    <el-col :span="8"
                      ><el-radio v-model="formData.tdpz_050F" label="0F"
                        >0F</el-radio
                      ></el-col
                    >
                  </el-row>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="06/10:" :label-width="formLabelWidth">
                  <el-row>
                    <el-col :span="8"
                      ><el-radio v-model="formData.tdpz_0610" label="auto"
                        >auto</el-radio
                      ></el-col
                    >
                    <el-col :span="8"
                      ><el-radio v-model="formData.tdpz_0610" label="06"
                        >06</el-radio
                      ></el-col
                    >
                    <el-col :span="8"
                      ><el-radio v-model="formData.tdpz_0610" label="10"
                        >10</el-radio
                      ></el-col
                    >
                  </el-row>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </div>
        <span slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">{{ $t('save') }}</el-button>
          <el-button @click="add_passage = false">{{ $t('cancel') }}</el-button>
        </span>
      </el-dialog>
    </div>
    <div class="drvieName_All" v-show="diag_drive">
      <div class="title">
        <p class="title_l">{{ $t('channelconfig.agreementName') }}</p>
        <i class="el-icon-close title_r" @click="diag_drive = false"></i>
      </div>
      <el-divider></el-divider>
      <el-tree
        :data="datas2"
        :props="defaultProps"
        default-expand-all
        @node-click="handleDetailClick"
      ></el-tree>
    </div>

    <!-- 网关 -->
    <div class="gateway">
      <el-dialog :title="$t('dbcollectiongate.title')" :visible.sync="outerVisible" width="382px">
        <el-form
          :model="formData"
          :rules="rulesGate"
          ref="gateWay"
          :label-width="$i18n.locale === 'zh'?'130px':'85px'"
          class="demo-ruleForm"
        >
          <el-form-item :label="$t('dbcollectiongate.name')" prop="wgpz_wgmc">
            <el-select
              v-model="formData.wgpz_wgmc"
              :placeholder="$t('select')"
              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="$t('dbcollectiongate.ID')" prop="wgpz_wgid">
            <el-select
              v-model="formData.wgpz_wgid"
              :placeholder="$t('select')"
              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="$t('dbcollectiongate.type')" prop="wgpz_wglx">
            <el-select
              v-model="formData.wgpz_wglx"
              :placeholder="$t('select')"
              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">{{$t('save')}}</el-button>
          <el-button @click="outerVisible = false">{{$t('cancel')}}</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import _ from "lodash";
import {SaveUserAnswer, GetUserAnswer, GetTree, SendTree, getQueryAnswer} from "@/api/user";
export default {
  name: "TestTreeView",
  data() {
    return {
      tdpz_port: "",
      tdpz_ip1: "",
      tdpz_ip2: "",
      tdpz_ip3: "",
      tdpz_ip4: "",
      user_id: "",
      loading: false,
      flag: false,
      gateway: "",
      nodekey: "",
      trees: [],
      formData: {
        ippz_wangkou: "",
        ippz_lwfs: "",
        ippz_zwym: "",
        ippz_mrwg: "",
        ippz_dns_sx: "",
        ippz_dns_by: "",
        ippz_ip: "",
        ippz_mac: "",
        ippz_wifi_ssid: "",
        ippz_wifi_mm: "",
        wgpz_wgmc: "",
        wgpz_wgid: "",
        wgpz_wglx: "",
        tdpz_tdmc: "",
        tdpz_td: "",
        tdpz_qdmc: "",
        tdpz_chuankou: "",
        tdpz_btl: "",
        tdpz_sjw: "8",
        tdpz_tzw: "",
        tdpz_jojy: "",
        tdpz_cgsj: "",
        tdpz_jgsj: "",
        tdpz_lxsj: "",
        tdpz_cfcs: "",
        tdpz_watchtime: false,
        tdpz_cjsylxdz: true,
        tdpz_dxzkb: "",
        tdpz_050F: "05",
        tdpz_0610: "auto",
        sbpz_sbid: "",
        sbpz_sbmc: "",
        sbpz_sbdz: "",
        sbpz_jysb: "",
        sbpz_jdzxz: "",
        cjdpz_tagid: "",
        cjdpz_mc: "",
        cjdpz_sjq: "",
        cjdpz_jcqdz: "",
        cjdpz_cjsjlx: "",
        cjdpz_zjx: "",
        cjdpz_dxsx: "",
        cjdpz_beilv: "",
        mqtt_qiyong: "",
        mqtt_ip: "",
        mqtt_port: "",
        mqtt_wgsn: "",
        mqtt_sssjfbzt: "",
        mqtt_qos: "",
        mqtt_scms: "",
        mqtt_sczq: "",
        mqtt_sjgl: "",
        mqtt_cjd_sbmc: "",
        mqtt_cjd_tagid: "",
        mqtt_retained: "",
      },
      tdpz_dxzkb1: "1",
      tdpz_dxzkb2: "1",
      diag_drive: false,
      options1: this.$t('channelconfig.options1'),
      options2:this.$t('channelconfig.options'),
      options3: [
        {
          value: "com1",
          label: "com1",
        },
        {
          value: "com2",
          label: "com2",
        },
        {
          value: "com3",
          label: "com3",
        },
        {
          value: "com4",
          label: "com4",
        },
        {
          value: "com5",
          label: "com5",
        },
        {
          value: "com6",
          label: "com6",
        },
        {
          value: "com7",
          label: "com7",
        },
        {
          value: "com8",
          label: "com8",
        },
        {
          value: "com9",
          label: "com9",
        },
        {
          value: "com10",
          label: "com10",
        },
        {
          value: "com11",
          label: "com11",
        },
        {
          value: "com12",
          label: "com12",
        },
        {
          value: "com13",
          label: "com13",
        },
        {
          value: "com14",
          label: "com14",
        },
        {
          value: "com15",
          label: "com15",
        },
        {
          value: "com16",
          label: "com16",
        },
        {
          value: "com17",
          label: "com17",
        },
        {
          value: "com18",
          label: "com18",
        },
        {
          value: "com19",
          label: "com19",
        },
        {
          value: "com20",
          label: "com20",
        },
        {
          value: "com21",
          label: "com21",
        },
        {
          value: "com22",
          label: "com22",
        },
        {
          value: "com23",
          label: "com23",
        },
        {
          value: "com24",
          label: "com24",
        },
        {
          value: "com25",
          label: "com25",
        },
        {
          value: "com26",
          label: "com26",
        },
        {
          value: "com27",
          label: "com27",
        },
        {
          value: "com28",
          label: "com28",
        },
        {
          value: "com29",
          label: "com29",
        },
        {
          value: "com30",
          label: "com30",
        },
        {
          value: "com31",
          label: "com31",
        },
        {
          value: "com32",
          label: "com32",
        },
        {
          value: "com33",
          label: "com33",
        },
        {
          value: "com34",
          label: "com34",
        },
        {
          value: "com35",
          label: "com35",
        },
        {
          value: "com36",
          label: "com36",
        },
        {
          value: "com37",
          label: "com37",
        },
        {
          value: "com38",
          label: "com38",
        },
        {
          value: "com39",
          label: "com39",
        },
        {
          value: "com40",
          label: "com40",
        },
        {
          value: "com41",
          label: "com41",
        },
        {
          value: "com42",
          label: "com42",
        },
        {
          value: "com43",
          label: "com43",
        },
        {
          value: "com44",
          label: "com44",
        },
        {
          value: "com45",
          label: "com45",
        },
        {
          value: "com46",
          label: "com46",
        },
        {
          value: "com47",
          label: "com47",
        },
        {
          value: "com48",
          label: "com48",
        },
        {
          value: "com49",
          label: "com49",
        },
        {
          value: "com50",
          label: "com50",
        },
      ],
      options4: [
        {
          value: "1200",
          label: "1200",
        },
        {
          value: "2400",
          label: "2400",
        },
        {
          value: "4800",
          label: "4800",
        },
        {
          value: "9600",
          label: "9600",
        },
        {
          value: "115200",
          label: "115200",
        },
      ],
      options5: [
        {
          value: "8",
          label: "8",
        },
        {
          value: "7",
          label: "7",
        },
      ],
      options6: [
        {
          value: "1",
          label: "1",
        },
        {
          value: "2",
          label: "2",
        },
      ],
      options7: [
        {
          value: "奇校验",
          label: "奇校验",
        },
        {
          value: "偶校验",
          label: "偶校验",
        },
        {
          value: "无校验（none）",
          label: "无校验（none）",
        },
      ],
      isdisabled: true,
      is_retract: true,
      labelPosition: "left",
      formLabelWidth: this.$i18n.locale==='zh'?"155px":"116px",
      input1: 10,
      add_passage: false,
      rightMenu: "",
      dialogVisible: true,
      menuShow: false,
      datas2: this.$t('channelconfig.agreement'),
      defaultProps: {
        children: "children",
        label: "label",
      },
      rules: {
        tdpz_tdmc: [
          { required: true, message: this.$t('channelconfig.rules.tdpz_tdmc'), trigger: "change" },
        ],
        tdpz_td: [{ required: true, message: this.$t('channelconfig.rules.tdpz_td'), trigger: "change" }],
        tdpz_qdmc: [
          { required: true, message: this.$t('channelconfig.rules.tdpz_qdmc'), trigger: "change" },
        ],
        tdpz_chuankou: [
          { required: true, message: this.$t('channelconfig.rules.tdpz_chuankou'), trigger: "change" },
        ],
        tdpz_btl: [
          { required: true, message: this.$t('channelconfig.rules.tdpz_btl'), trigger: "change" },
        ],
        tdpz_tzw: [
          { required: true, message: this.$t('channelconfig.rules.tdpz_tzw'), trigger: "change" },
        ],
        tdpz_sjw: [
          { required: true, message: this.$t('channelconfig.rules.tdpz_sjw'), trigger: "change" },
        ],
        tdpz_jojy: [
          { required: true, message: this.$t('channelconfig.rules.tdpz_jojy'), trigger: "change" },
        ],
        tdpz_cgsj: [
          { required: true, message: this.$t('channelconfig.rules.tdpz_cssj'), trigger: "blur" },
        ],
        tdpz_jgsj: [
          { required: true, message: this.$t('channelconfig.rules.tdpz_jgsj'), trigger: "blur" },
        ],
        tdpz_lxsj: [
          { required: true, message: this.$t('channelconfig.rules.tdpz_lxsj'), trigger: "blur" },
        ],
        tdpz_cfcs: [
          { required: true, message: this.$t('channelconfig.rules.tdpz_cfcs'), trigger: "blur" },
        ],
        tdpz_ip: [{ required: true, message: "请输入IP地址", trigger: "blur" }],
        tdpz_port: [
          { required: true, message: "请输入端口号", trigger: "blur" },
        ],
      },

      // 网关配置的出现
      outerVisible: false,
      rulesGate: {
        wgpz_wgmc: [
          { required: true, message: "请选择网关名称", trigger: "blur" },
        ],
        wgpz_wgid: [
          { required: true, message: "请选择网关id", trigger: "blur" },
        ],
        wgpz_wglx: [
          { required: true, message: "请选择网关类型", trigger: "blur" },
        ],
      },
      opts1: this.$t('dbcollectiongate.options1'),
      opts2: [
        {
          value: "LAN-BOX1",
          label: "LAN-BOX1",
        },
        {
          value: "WLAN-BOX1",
          label: "WLAN-BOX1",
        },
      ],
      opts3: this.$t('dbcollectiongate.options3'),
    };
  },
  methods: {
    // 提交时校验
    submitForm: _.debounce(function (e) {
      this.$refs["formData"].validate((valid) => {
        if (valid) {
          this.saveGate();
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    }, 1000),
    async rightClick(e, data, node) {
      if (data.editing) {
        this.outerVisible = true;
        this.GetUserAnswer();
        // let res2 = await GetUserAnswer({
        //     mark: "DB-IotLite_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;
        //     this.gateWay.wgpz_wglx = res3.wgpz_wglx;
        // }
      }
      if (node.level == 3 && data.label == "I/O点" ) {
        this.nodekey = data.id;
        this.menuShow = false;
        this.menuShow = true;
        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();
          }
        });
      }
    },
    // 点击时候的判断,判断是否为新增网关
    selectMenuNode(index, indexPath) {
      console.log(index);
      if (index == 1) {
        this.add_passage = true;
        this.formData.tdpz_050F = "auto";
        this.formData.tdpz_0610 = "auto";
      } else {
        this.menuShow = false;
      }
    },
    foo() {
      // 取消鼠标监听事件 菜单栏
      this.menuShow = false;
      document.removeEventListener("click", this.foo); // 关掉监听，
    },
    treeNodeClick() {
      this.foo();
    },
    // 收起点击事件的判断
    getRetract() {
      if (this.is_retract == true) {
        this.is_retract = false;
      } else {
        this.is_retract = true;
      }
    },
    getDriveName() {
      this.diag_drive = true;
    },
    closeMenu(ev) {
      console.log({ ev });
      this.dialogVisible = false;
    },
    handleDetailClick(data, node, value) {
      console.log(data.label);
      if (node.level == 2) {
        this.formData.tdpz_qdmc = data.label;
        this.diag_drive = false;
      }
    },
    // 保存
    async saveGate() {
      var obj = {
        tdpz_tdmc: this.formData.tdpz_tdmc,
        tdpz_td: this.formData.tdpz_td,
        tdpz_qdmc: this.formData.tdpz_qdmc,
        tdpz_port: this.formData.tdpz_port,
        tdpz_chuankou: this.formData.tdpz_chuankou,
        tdpz_btl: this.formData.tdpz_btl,
        tdpz_sjw: this.formData.tdpz_sjw,
        tdpz_tzw: this.formData.tdpz_tzw,
        tdpz_jojy: this.formData.tdpz_jojy,
        tdpz_cgsj: this.formData.tdpz_cgsj,
        tdpz_jgsj: this.formData.tdpz_jgsj,
        tdpz_lxsj: this.formData.tdpz_lxsj,
        tdpz_cfcs: this.formData.tdpz_cfcs,
        tdpz_watchtime: String(Number(this.formData.tdpz_watchtime)),
        tdpz_cjsylxdz: String(Number(this.formData.tdpz_cjsylxdz)),
        tdpz_dxzkb: this.tdpz_dxzkb1 + ":" + this.tdpz_dxzkb2,
        tdpz_050F: this.formData.tdpz_050F,
        tdpz_0610: this.formData.tdpz_0610,
      };
      // console.log(obj);
      // alert(obj.tdpz_dxzkb);
      this.add_passage = false;
      // console.log(this.formData);
      let datas = { ...this.formData, ...obj };
      let res = await SaveUserAnswer({
        mark: "DB-IotLite_wx",
        user_id: this.user_id,
        // user_id: "USER202307301114011710",
        mark_value: JSON.stringify(datas),
        step: "2",
      });
      console.log(res);
      if (res.data.state) {
        this.$message({
          message: this.$t('success'),
          type: "success",
        });
        sessionStorage.setItem("DB-IotLite_wx", JSON.stringify(datas));
        // console.log(res);
        // console.log(res.data.data.mark_value);
        this.$refs["tree"].append(
          {
            id: this.nodekey + 1000,
            label: this.formData.tdpz_tdmc,
          },
          this.nodekey
        );
        // console.log(this.trees);
        this.sendTree();
        // this.$router.push(`/dbserialdevice?user_id=${this.user_id}`);
        this.$router.push(`/dbserialdevice?user_id=${this.user_id}`);
      } else {
        this.$message({
          message: this.$t('fail'),
          type: "error",
        });
      }
    },
    async GetUserAnswer() {
      this.loading = true;
      let answer = await GetUserAnswer({
        mark: "DB-IotLite_wx",
        // user_id: "USER202307301114011710",
        user_id: this.user_id,
      });
      this.loading = false;
      console.log(answer);
      if(!answer.data.state){
        getQueryAnswer({
          mark: "DB-IotLite_wx",
          user_id: this.user_id
        }).then(res => {
          let obj = JSON.parse(res.data.data.r3);
          // console.log(obj);
          if (obj.tdpz_dxzkb) {
            this.tdpz_dxzkb1 = obj.tdpz_dxzkb.split(":")[0];
            this.tdpz_dxzkb2 = obj.tdpz_dxzkb.split(":")[1];
            this.formData.tdpz_watchtime = Boolean(Number(obj.tdpz_watchtime));
            this.formData.tdpz_cjsylxdz = Boolean(Number(obj.tdpz_cjsylxdz));
          }
          if (obj.tdpz_050F) {
            this.formData.tdpz_050F = obj.tdpz_050F;
            this.formData.tdpz_0610 = obj.tdpz_0610;
          }
          this.formData.tdpz_tdmc = obj.tdpz_tdmc;
          this.formData.tdpz_td = obj.tdpz_td;
          this.formData.tdpz_qdmc = obj.tdpz_qdmc;
          this.formData.tdpz_chuankou = obj.tdpz_chuankou;
          this.formData.tdpz_btl = obj.tdpz_btl;
          this.formData.tdpz_sjw = obj.tdpz_sjw;
          this.formData.tdpz_tzw = obj.tdpz_tzw;
          this.formData.tdpz_jojy = obj.tdpz_jojy;
          this.formData.tdpz_port = obj.tdpz_port;
          this.formData.tdpz_cgsj = obj.tdpz_cgsj;
          this.formData.tdpz_jgsj = obj.tdpz_jgsj;
          this.formData.tdpz_lxsj = obj.tdpz_lxsj;
          this.formData.tdpz_cfcs = obj.tdpz_cfcs;
          this.formData.tdpz_dxzkb = obj.tdpz_dxzkb;
          this.formData.wgpz_wgid = obj.wgpz_wgid;
          console.log(this.formData.wgpz_wgid);
          this.formData.wgpz_wgmc = obj.wgpz_wgmc;
          this.formData.wgpz_wglx = obj.wgpz_wglx;

          this.formData.ippz_wangkou = obj.ippz_wangkou;
          this.formData.ippz_lwfs = obj.ippz_lwfs;
          this.formData.ippz_ip = obj.ippz_ip;
          this.formData.ippz_zwym = obj.ippz_zwym;
          this.formData.ippz_mrwg = obj.ippz_mrwg;
          this.formData.ippz_dns_sx = obj.ippz_dns_sx;
          this.formData.ippz_dns_by = obj.ippz_dns_by;
          this.formData.ippz_mac = obj.ippz_mac;
          this.formData.ippz_wifi_ssid = obj.ippz_wifi_ssid;
          this.formData.ippz_wifi_mm = obj.ippz_wifi_mm;
          this.formData.sbpz_sbid = obj.sbpz_sbid;
          this.formData.sbpz_sbmc = obj.sbpz_sbmc;
          this.formData.sbpz_sbdz = obj.sbpz_sbdz;
          this.formData.sbpz_jysb = obj.sbpz_jysb;
          this.formData.sbpz_jdzxz = obj.sbpz_jdzxz;

          this.formData.cjdpz_tagid = obj.cjdpz_tagid;
          this.formData.cjdpz_mc = obj.cjdpz_mc;
          this.formData.cjdpz_sjq = obj.cjdpz_sjq;
          this.formData.cjdpz_jcqdz = obj.cjdpz_jcqdz;
          this.formData.cjdpz_cjsjlx = obj.cjdpz_cjsjlx;
          this.formData.cjdpz_zjx = obj.cjdpz_zjx;
          this.formData.cjdpz_dxsx = obj.cjdpz_dxsx;
          this.formData.cjdpz_beilv = obj.cjdpz_beilv;

          this.formData.mqtt_qiyong = obj.mqtt_qiyong;
          this.formData.mqtt_ip = obj.mqtt_ip;
          this.formData.mqtt_port = obj.mqtt_port;
          this.formData.mqtt_wgsn = obj.mqtt_wgsn;
          this.formData.mqtt_sssjfbzt = obj.mqtt_sssjfbzt;
          this.formData.mqtt_qos = obj.mqtt_qos;
          this.formData.mqtt_scms = obj.mqtt_scms;
          this.formData.mqtt_sczq = obj.mqtt_sczq;
          this.formData.mqtt_sjgl = obj.mqtt_sjgl;
          this.formData.mqtt_cjd_sbmc = obj.mqtt_cjd_sbmc;
          this.formData.mqtt_cjd_tagid = obj.mqtt_cjd_tagid;
        })
      }
      // console.log(answer.data);
      if (answer.data.data && answer.data.state) {
        // console.log(answer);
        let obj = JSON.parse(answer.data.data.mark_value);
        // console.log(obj);
        if (obj.tdpz_dxzkb) {
          this.tdpz_dxzkb1 = obj.tdpz_dxzkb.split(":")[0];
          this.tdpz_dxzkb2 = obj.tdpz_dxzkb.split(":")[1];
          this.formData.tdpz_watchtime = Boolean(Number(obj.tdpz_watchtime));
          this.formData.tdpz_cjsylxdz = Boolean(Number(obj.tdpz_cjsylxdz));
        }
        if (obj.tdpz_050F) {
          this.formData.tdpz_050F = obj.tdpz_050F;
          this.formData.tdpz_0610 = obj.tdpz_0610;
        }
        this.formData.tdpz_tdmc = obj.tdpz_tdmc;
        this.formData.tdpz_td = obj.tdpz_td;
        this.formData.tdpz_qdmc = obj.tdpz_qdmc;
        this.formData.tdpz_chuankou = obj.tdpz_chuankou;
        this.formData.tdpz_btl = obj.tdpz_btl;
        this.formData.tdpz_sjw = obj.tdpz_sjw;
        this.formData.tdpz_tzw = obj.tdpz_tzw;
        this.formData.tdpz_jojy = obj.tdpz_jojy;
        this.formData.tdpz_port = obj.tdpz_port;
        this.formData.tdpz_cgsj = obj.tdpz_cgsj;
        this.formData.tdpz_jgsj = obj.tdpz_jgsj;
        this.formData.tdpz_lxsj = obj.tdpz_lxsj;
        this.formData.tdpz_cfcs = obj.tdpz_cfcs;
        this.formData.tdpz_dxzkb = obj.tdpz_dxzkb;
        this.formData.wgpz_wgid = obj.wgpz_wgid;
        console.log(this.formData.wgpz_wgid);
        this.formData.wgpz_wgmc = obj.wgpz_wgmc;
        this.formData.wgpz_wglx = obj.wgpz_wglx;

        this.formData.ippz_wangkou = obj.ippz_wangkou;
        this.formData.ippz_lwfs = obj.ippz_lwfs;
        this.formData.ippz_ip = obj.ippz_ip;
        this.formData.ippz_zwym = obj.ippz_zwym;
        this.formData.ippz_mrwg = obj.ippz_mrwg;
        this.formData.ippz_dns_sx = obj.ippz_dns_sx;
        this.formData.ippz_dns_by = obj.ippz_dns_by;
        this.formData.ippz_mac = obj.ippz_mac;
        this.formData.ippz_wifi_ssid = obj.ippz_wifi_ssid;
        this.formData.ippz_wifi_mm = obj.ippz_wifi_mm;
        this.formData.sbpz_sbid = obj.sbpz_sbid;
        this.formData.sbpz_sbmc = obj.sbpz_sbmc;
        this.formData.sbpz_sbdz = obj.sbpz_sbdz;
        this.formData.sbpz_jysb = obj.sbpz_jysb;
        this.formData.sbpz_jdzxz = obj.sbpz_jdzxz;

        this.formData.cjdpz_tagid = obj.cjdpz_tagid;
        this.formData.cjdpz_mc = obj.cjdpz_mc;
        this.formData.cjdpz_sjq = obj.cjdpz_sjq;
        this.formData.cjdpz_jcqdz = obj.cjdpz_jcqdz;
        this.formData.cjdpz_cjsjlx = obj.cjdpz_cjsjlx;
        this.formData.cjdpz_zjx = obj.cjdpz_zjx;
        this.formData.cjdpz_dxsx = obj.cjdpz_dxsx;
        this.formData.cjdpz_beilv = obj.cjdpz_beilv;

        this.formData.mqtt_qiyong = obj.mqtt_qiyong;
        this.formData.mqtt_ip = obj.mqtt_ip;
        this.formData.mqtt_port = obj.mqtt_port;
        this.formData.mqtt_wgsn = obj.mqtt_wgsn;
        this.formData.mqtt_sssjfbzt = obj.mqtt_sssjfbzt;
        this.formData.mqtt_qos = obj.mqtt_qos;
        this.formData.mqtt_scms = obj.mqtt_scms;
        this.formData.mqtt_sczq = obj.mqtt_sczq;
        this.formData.mqtt_sjgl = obj.mqtt_sjgl;
        this.formData.mqtt_cjd_sbmc = obj.mqtt_cjd_sbmc;
        this.formData.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: "2",
      });
      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;
      let res = await SaveUserAnswer({
        mark: "DB-IotLite_wx",
        user_id: this.user_id,
        // user_id: "USER202307301114011710",
        mark_value: JSON.stringify(this.formData),
        step: "1",
      });
      if (res.data.state) {
        this.$message({
          message: "修改成功",
          type: "success",
        });
        this.trees[0].label = this.formData.wgpz_wgid;
        this.sendTree();
      } else {
        this.$message({
          message: `保存失败,原因是${res.data.message}`,
          type: "error",
        });
      }
      console.log(res);
    },
  },
  mounted() {
    this.user_id = this.$route.query.user_id;
    if (this.user_id != "undefined") {
      GetTree({
        user_id: this.user_id,
        type: "2",
      }).then((res) => {
        console.log(res);
        console.log(JSON.parse(res.data.data.r2));
        if(this.$i18n.locale==='zh'){
        this.trees = JSON.parse(res.data.data.r2);
      }else{
        this.trees = JSON.parse(res.data.data.data);
      }
      });
    }
    this.GetUserAnswer();
  },
};
</script>

<style lang="less" scoped>
.tree_menu {
  position: absolute;
  width: 286px;
  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;
}

.super_parmams {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 668px;

  i {
    width: 24px;
    height: 24px;
    display: inline-block;
  }
}
.zh{
  .el-col-12:last-child {
    padding-right: 25px !important;
  }

  .el-col-12:first-child {
    padding-right: 10px !important;
  }
}
/deep/ .pas_params {
  .el-col-12:last-child {
    padding-right: 150px;
  }

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

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

  .el-row {
    display: flex;
    align-items: flex-end;

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

    .el-col-6 {
      height: 40px;
      top: 4px;
      left: 78px;
      position: relative;
      width: 50px;
    }
  }
}

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

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

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

  .el-row {
    display: flex;
    align-items: flex-end;

    .el-col-4 {
      padding-left: 5px;
      width: 14px;
      text-align: center;
      line-height: 4px;
      height: 20px;
    }
  }
}

.el-dialog__footer {
  padding: 18px 1.25rem 50px;
}

.dialog-footer {
  position: absolute;
  left: 50%;
  bottom: 45px;
  transform: translateX(-50%);
}

/deep/ .drvieName_All {
  position: absolute;
  left: 1408px;
  top: 140px;
  width: 435px;
  max-height: 720px;
  z-index: 10000;
  border: 1px solid #f5f5f5;

  .title {
    background-color: #fff;
    padding: 5px 6px 5px 25px;
    display: flex;
    justify-content: space-between;

    .title_l {
      //width: 103px;
      height: 26px;
      color: rgba(16, 16, 16, 1);
      font-size: 18px;
      text-align: left;
    }

    .title_r {
      width: 24px;
      height: 24px;
      line-height: 24px;
      cursor: pointer;
      color: rgba(111, 103, 103, 1);
    }
  }

  // h3 {
  //   padding-bottom: 10px;
  //   background-color: #fff;
  // }

  .el-divider--horizontal {
    margin: 0px;
  }
}
</style>
