<!-- 斥候数据采集——有线网关配置 -->
<template>
  <div>
    <div class="gateway">
      <el-dialog title="新建网关" :visible.sync="outerVisible" width="382px">
        <el-form
          :model="formData"
          :rules="rules"
          ref="formData"
          label-width="85px"
          class="demo-ruleForm"
        >
          <el-form-item label="网关名称:" prop="wgpz_wgmc">
            <el-select
              v-model="formData.wgpz_wgmc"
              placeholder="请选择"
              style="width: 200px"
            >
              <el-option
                v-for="item in options1"
                :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="formData.wgpz_wgid"
              placeholder="请选择"
              style="width: 200px"
            >
              <el-option
                v-for="item in options2"
                :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="submitForm">保存</el-button>
          <el-button @click="outerVisible = false">取 消</el-button>
        </div>
      </el-dialog>
    </div>
    <div>
      <el-dialog
        title="系统提示"
        :visible.sync="innerVisible"
        append-to-body
        width="382px"
      >
        <p class="inner_content">
          请确认选择的网关类型与网关实体一致:否则该工程无法下载至实体网关
        </p>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="saveGate">确定</el-button>
          <el-button @click="innerVisible = false">取 消</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { SaveUserAnswer, GetUserAnswer, GetTree, SendTree } from "@/api/user";
export default {
  name: "TestChiGate",
  data() {
    return {
      // isShow: true,
      loading: false,
      trees: [],
      user_id: "",
      options1: [
        {
          value: "有线区网关2",
          label: "有线区网关2",
        },
        {
          value: "无线区网关2",
          label: "无线区网关2",
        },
      ],
      options2: [
        {
          value: "LAN-BOX2",
          label: "LAN-BOX2",
        },
        {
          value: "WLAN-BOX2",
          label: "WLAN-BOX2",
        },
      ],
      formData: {
        ippz_wllx: "",
        ippz_ip: "",
        ippz_zwym: "",
        ippz_wg: "",
        ippz_dns: "",
        ippz_dns_by: "",

        wgpz_wgmc: "",
        wgpz_wgid: "",

        sbpz_sbmc: "",
        sbpz_kzqxy: "",
        sbpz_ip: "",
        sbpz_port: "",
        sbpz_czdz: "",
        sbpz_lxzq: "",
        sbpz_qyhmcj: "",
        sbpz_qyplcj: "",
        sbpz_zdplcjs: "",
        sbpz_cjcgsj: "",
        sbpz_zqsbonchange: "",
        sbpz_txsjjg: "",
        sbpz_xqjcqxr: "",
        sbpz_bcjcqxr: "",

        cjdpz_tagid: "",
        cjdpz_mc: "",
        cjdpz_sjq: "",
        cjdpz_jcqdz: "",
        cjdpz_cjsjlx: "",
        cjdpz_dxsx: "",
        cjdpz_scms: "",

        mqtt_ip: "",
        mqtt_wgsn: "",
        mqtt_port: "",
        mqtt_keepalive: "",
        mqtt_tls: "",
        mqtt_clearsession: "",
        mqtt_sssjfbzt: "",
        mqtt_qos: "",

        mqtt_cjd_sbmc: "",
        mqtt_cjd_tagid: "",
      },
      innerVisible: false,
      outerVisible: true,
      labelPosition: "left",
      formLabelWidth: "96px",
      count: 0,
      // isShow: true,
      rules: {
        wgpz_wgid: [
          { required: true, message: "请输入网关ID", trigger: "blur" },
        ],
        wgpz_wgmc: [
          { required: true, message: "请输入网关名称", trigger: "blur" },
        ],
      },
    };
  },
  mounted() {
    this.user_id = window.location.href.split("?")[1];
    this.GetUserAnswer();
    this.getTree();
    // this.isShow = true;
  },
  methods: {
    async sendTree() {
      let result = await SendTree({
        user_id: this.user_id,
        // user_id: "USER202307301114011710",
        data: JSON.stringify(this.trees),
        type: "3",
      });
      console.log(result);
    },
    async getTree() {
      let res = await GetTree({
        user_id: this.user_id,
        type: "3",
      });
      console.log(res);
      console.log(JSON.parse(res.data.data.data));
      this.trees = JSON.parse(res.data.data.data);
    },
    // 登陆时进行校验
    submitForm() {
      this.$refs["formData"].validate((valid) => {
        if (valid) {
          this.innerVisible = true;
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    async saveGate() {
      console.log(this.formData);
      let res = await SaveUserAnswer({
        mark: "CHGYWG_yx",
        user_id: this.user_id,
        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();
        this.$router.push(`/configuration?user_id=${this.user_id}`);
      } else {
        this.$message({
          message: `保存失败,原因是${res.data.message}`,
          type: "error",
        });
      }
      // console.log(res);
    },
    async GetUserAnswer() {
      this.loading = true;
      let { data } = await GetUserAnswer({
        mark: "CHGYWG_yx",
        user_id: this.user_id,
      });
      console.log(data);
      this.loading = false;
      if (data.data && data.state) {
        if (data.data.step == 1) {
          this.$router.push(`/configuration?user_id=${this.user_id}`);
        } else if (data.data.step == 2) {
          this.$router.push(`/collectionpoint?user_id=${this.user_id}`);
        } else {
          // this.isShow = true;
          console.log(JSON.parse(data.data.mark_value));
          let obj = JSON.parse(data.data.mark_value);
          console.log(obj);
          this.formData.wgpz_wgid = obj.wgpz_wgid;
          this.formData.wgpz_wgmc = obj.wgpz_wgmc;
          this.formData.ippz_wllx = obj.ippz_wllx;
          this.formData.ippz_ip = obj.ippz_ip;
          this.formData.ippz_zwym = obj.ippz_zwym;
          this.formData.ippz_wg = obj.ippz_wg;
          this.formData.ippz_dns = obj.ippz_dns;
          this.formData.ippz_dns_by = obj.ippz_dns_by;

          this.formData.sbpz_sbmc = obj.sbpz_sbmc;
          this.formData.sbpz_kzqxy = obj.sbpz_kzqxy;
          this.formData.sbpz_ip = obj.sbpz_ip;
          this.formData.sbpz_port = obj.sbpz_port;
          this.formData.sbpz_czdz = obj.sbpz_czdz;
          this.formData.sbpz_lxzq = obj.sbpz_lxzq;
          this.formData.sbpz_qyhmcj = obj.sbpz_qyhmcj;
          this.formData.sbpz_qyplcj = obj.sbpz_qyplcj;
          this.formData.sbpz_zdplcjs = obj.sbpz_zdplcjs;
          this.formData.sbpz_cjcgsj = obj.sbpz_cjcgsj;
          this.formData.sbpz_zqsbonchange = obj.sbpz_zqsbonchange;
          this.formData.sbpz_txsjjg = obj.sbpz_txsjjg;
          this.formData.sbpz_xqjcqxr = obj.sbpz_xqjcqxr;
          this.formData.sbpz_bcjcqxr = obj.sbpz_bcjcqxr;

          this.formData.cjdpz_tagid = obj.mqtt_cjd_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_dxsx = obj.cjdpz_dxsx;
          this.formData.cjdpz_scms = obj.cjdpz_scms;

          this.formData.mqtt_ip = obj.mqtt_ip;
          this.formData.mqtt_wgsn = obj.mqtt_wgsn;
          this.formData.mqtt_port = obj.mqtt_port;
          this.formData.mqtt_keepalive = obj.mqtt_keepalive;
          this.formData.mqtt_tls = obj.mqtt_tls;
          this.formData.mqtt_clearsession = obj.mqtt_clearsession;
          this.formData.mqtt_sssjfbzt = obj.mqtt_sssjfbzt;
          this.formData.mqtt_qos = obj.mqtt_qos;

          this.formData.mqtt_cjd_sbmc = obj.mqtt_cjd_sbmc;
          this.formData.mqtt_cjd_tagid = obj.mqtt_cjd_tagid;
        }
      }
    },
  },
};
</script>

<style lang="less" scoped>
/deep/ .gateway {
  // position: absolute;
  // left: 694px;
  // top: 188px;
  // width: 382px;
  // height: 309px;
  // line-height: 20px;
  // // background-color: rgba(255, 255, 255, 1);
  // background: url(../assets/image/bg.png);
  // background-size: 100% 100%;
  // text-align: center;
  // border: 1px solid rgba(187, 187, 187, 1);
  .el-divider--horizontal {
    margin: 8px 0;
  }

  .title {
    margin: 13px 11px 0px 21px;
    display: flex;
    justify-content: space-between;

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

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

  .content {
    margin-left: 50px;
    margin-right: 50px;

    .el-input__inner {
      width: 200px;
      height: 30px;
    }
  }

  /deep/ .footer {
    .el-button {
      width: 80px;
      height: 30px;
      line-height: 0.5;
    }
  }
}
</style>
