<!-- IP设置-网络方式 -->
<template>
  <div>
    <!-- 无线网关 -->
    <el-dialog title="IP设置" :visible.sync="dialogFormVisible" v-loading="loading" :append-to-body="true" width="894px">
      <div class="form_content">
        <div>
          <p class="netWay">网络方式</p>
          <el-radio v-model="ippz_lwfs" label="1" style="color:white;">有线</el-radio>
          <el-radio v-model="ippz_lwfs" label="2" style="color:white;">WIFI</el-radio>
          <el-divider></el-divider>
        </div>
        <!-- v-if="ippz_lwfs == 1 ? true : false" -->
        <div>
          <h2>有线</h2>
          <el-form :label-position="labelPosition" label-width="80px" :rules="rules_ippz_wirle" ref="reform">
            <div class="wired">
              <el-row>
                <el-col :span="12">
                  <el-form-item label="网口:" :label-width="formLabelWidth" prop="ippz_wangkou">
                    <el-select v-model="ippz_wangkou" placeholder="ETHO" style="width: 200px; height: 30px">
                      <el-option label="ETH0" value="ETH0"></el-option>
                      <el-option label="ETH1" value="ETH1"></el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="默认网关:" :label-width="formLabelWidth" prop="ippz_mrwg">
                    <el-row>
                      <el-col :span="5">
                        <el-input v-model="ippz_mrwg1" maxlength="3"></el-input>
                      </el-col>
                      <el-col :span="1">. </el-col>
                      <el-col :span="5">
                        <el-input v-model="ippz_mrwg2" maxlength="3"></el-input>
                      </el-col>
                      <el-col :span="1">.</el-col>
                      <el-col :span="5">
                        <el-input v-model="ippz_mrwg3" maxlength="3"></el-input>
                      </el-col>
                      <el-col :span="1">.</el-col>
                      <el-col :span="5">
                        <el-input v-model="ippz_mrwg4" maxlength="3"></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" prop="ippz_zwym">
                    <el-row>
                      <el-col :span="5">
                        <el-input v-model="ippz_zwym1" maxlength="3"></el-input>
                      </el-col>
                      <el-col :span="1">.</el-col>
                      <el-col :span="5">
                        <el-input v-model="ippz_zwym2" maxlength="3"></el-input>
                      </el-col>
                      <el-col :span="1">.</el-col>
                      <el-col :span="5">
                        <el-input v-model="ippz_zwym3" maxlength="3"></el-input>
                      </el-col>
                      <el-col :span="1">.</el-col>
                      <el-col :span="5">
                        <el-input v-model="ippz_zwym4" maxlength="3"></el-input>
                      </el-col>
                    </el-row>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="IP地址:" :label-width="formLabelWidth" prop="ippz_ip">
                    <el-row>
                      <el-col :span="5">
                        <el-input v-model="ippz_ip1" maxlength="3"></el-input>
                      </el-col>
                      <el-col :span="1">. </el-col>
                      <el-col :span="5">
                        <el-input v-model="ippz_ip2" maxlength="3"></el-input>
                      </el-col>
                      <el-col :span="1">.</el-col>
                      <el-col :span="5">
                        <el-input v-model="ippz_ip3" maxlength="3"></el-input>
                      </el-col>
                      <el-col :span="1">.</el-col>
                      <el-col :span="5">
                        <el-input v-model="ippz_ip4" maxlength="3"></el-input>
                      </el-col>
                    </el-row>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="12">
                  <el-form-item label="首选DNS:" :label-width="formLabelWidth" prop="ippz_dns_sx">
                    <el-row>
                      <el-col :span="5">
                        <el-input v-model="ippz_dns_sx1" maxlength="3"></el-input>
                      </el-col>
                      <el-col :span="1">.</el-col>
                      <el-col :span="5">
                        <el-input v-model="ippz_dns_sx2" maxlength="3"></el-input>
                      </el-col>
                      <el-col :span="1">.</el-col>
                      <el-col :span="5">
                        <el-input v-model="ippz_dns_sx3" maxlength="3"></el-input>
                      </el-col>
                      <el-col :span="1">.</el-col>
                      <el-col :span="5">
                        <el-input v-model="ippz_dns_sx4" maxlength="3"></el-input>
                      </el-col>
                    </el-row>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="备选DNS:" :label-width="formLabelWidth" prop="ippz_dns_by">
                    <el-row>
                      <el-col :span="5">
                        <el-input v-model="ippz_dns_by1" maxlength="3"></el-input>
                      </el-col>
                      <el-col :span="1">. </el-col>
                      <el-col :span="5">
                        <el-input v-model="ippz_dns_by2" maxlength="3"></el-input>
                      </el-col>
                      <el-col :span="1">.</el-col>
                      <el-col :span="5">
                        <el-input v-model="ippz_dns_by3" maxlength="3"></el-input>
                      </el-col>
                      <el-col :span="1">.</el-col>
                      <el-col :span="5">
                        <el-input v-model="ippz_dns_by4" maxlength="3"></el-input>
                      </el-col>
                    </el-row>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="24">
                  <el-form-item label="MAC地址:" :label-width="formLabelWidth" prop="ippz_mac">
                    <el-row>
                      <el-col :span="24" class="mac">
                        <el-input style="width: 200px; height: 30px" v-model="ippz_mac"></el-input><el-button
                          v-copy="ippz_mac" @click="CopyText">复制</el-button>
                      </el-col>
                    </el-row>
                  </el-form-item>
                </el-col>
              </el-row>
            </div>
          </el-form>
        </div>
        <!-- v-if="ippz_lwfs == 2 ? true : false" -->
        <div>
          <h2>WIFI</h2>
          <el-form :label-position="labelPosition" label-width="80px" :rules="rules_ippz_wifi" ref="reform">
            <div class="wifi">
              <el-row>
                <el-col :span="12">
                  <el-form-item label="SSID:" :label-width="formLabelWidth" prop="ippz_wifi_ssid">
                    <el-input v-model="ippz_wifi_ssid" placeholder="请输入SSID"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="密码:" :label-width="formLabelWidth" prop="ippz_wifi_mm">
                    <el-input v-model="ippz_wifi_mm" 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="IP地址:" :label-width="formLabelWidth">
                    <el-row>
                      <el-col :span="5">
                        <el-input v-model.number="ippz_ip1" disabled></el-input>
                      </el-col>
                      <el-col :span="1">. </el-col>
                      <el-col :span="5">
                        <el-input v-model.number="ippz_ip2" disabled></el-input>
                      </el-col>
                      <el-col :span="1">.</el-col>
                      <el-col :span="5">
                        <el-input v-model.number="ippz_ip3" disabled></el-input>
                      </el-col>
                      <el-col :span="1">.</el-col>
                      <el-col :span="5">
                        <el-input v-model.number="ippz_ip4" disabled></el-input>
                      </el-col>
                    </el-row>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="子网掩码:" :label-width="formLabelWidth">
                    <el-row>
                      <el-col :span="5">
                        <el-input v-model="ippz_zwym1" disabled></el-input>
                      </el-col>
                      <el-col :span="1">.</el-col>
                      <el-col :span="5">
                        <el-input v-model="ippz_zwym2" disabled></el-input>
                      </el-col>
                      <el-col :span="1">.</el-col>
                      <el-col :span="5">
                        <el-input v-model="ippz_zwym3" disabled></el-input>
                      </el-col>
                      <el-col :span="1">.</el-col>
                      <el-col :span="5">
                        <el-input v-model="ippz_zwym4" disabled></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-row>
                      <el-col :span="5">
                        <el-input v-model="ippz_mrwg1" disabled></el-input>
                      </el-col>
                      <el-col :span="1">. </el-col>
                      <el-col :span="5">
                        <el-input v-model="ippz_mrwg2" disabled></el-input>
                      </el-col>
                      <el-col :span="1">.</el-col>
                      <el-col :span="5">
                        <el-input v-model="ippz_mrwg3" disabled></el-input>
                      </el-col>
                      <el-col :span="1">.</el-col>
                      <el-col :span="5">
                        <el-input v-model="ippz_mrwg4" disabled></el-input>
                      </el-col>
                    </el-row>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="首选DNS:" :label-width="formLabelWidth" prop="ippz_dns_sx">
                    <el-row>
                      <el-col :span="5">
                        <el-input v-model="ippz_dns_sx1" maxlength="3"></el-input>
                      </el-col>
                      <el-col :span="1">.</el-col>
                      <el-col :span="5">
                        <el-input v-model="ippz_dns_sx2" maxlength="3"></el-input>
                      </el-col>
                      <el-col :span="1">.</el-col>
                      <el-col :span="5">
                        <el-input v-model="ippz_dns_sx3" maxlength="3"></el-input>
                      </el-col>
                      <el-col :span="1">.</el-col>
                      <el-col :span="5">
                        <el-input v-model="ippz_dns_sx4" maxlength="3"></el-input>
                      </el-col>
                    </el-row>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="12">
                  <el-form-item label="备选DNS:" :label-width="formLabelWidth" prop="ippz_dns_by">
                    <el-row>
                      <el-col :span="5">
                        <el-input v-model="ippz_dns_by1" maxlength="3"></el-input>
                      </el-col>
                      <el-col :span="1">.</el-col>
                      <el-col :span="5">
                        <el-input v-model="ippz_dns_by2" maxlength="3"></el-input>
                      </el-col>
                      <el-col :span="1">.</el-col>
                      <el-col :span="5">
                        <el-input v-model="ippz_dns_by3" maxlength="3"></el-input>
                      </el-col>
                      <el-col :span="1">.</el-col>
                      <el-col :span="5">
                        <el-input v-model="ippz_dns_by4" maxlength="3"></el-input>
                      </el-col>
                    </el-row>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="MAC地址:" :label-width="formLabelWidth" prop="ippz_mac">
                    <el-row>
                      <el-col :span="24" class="mac">
                        <el-input style="width: 200px" v-model="ippz_mac" /><el-button
                          v-copy="formdata.ippz_mac">复制</el-button>
                      </el-col>
                    </el-row>
                  </el-form-item>
                </el-col>
              </el-row>
            </div>
          </el-form>
        </div>
        <div slot="footer" class="dialog-footer footer">
          <el-button type="primary" @click="saveAll">保 存</el-button>
          <el-button @click="dialogFormVisible = false">取 消</el-button>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { GetUserAnswer, SaveUserAnswer } from "../api/user";
export default {
  name: "DataView",
  data() {
    return {
      formdata: {
        ippz_wangkou: "ETH1",
        ippz_zwym: "2",
        ippz_mrwg: "3",
        ippz_dns_sx: "4",
        ippz_dns_by: "5",
        ippz_ip: "6",
        ippz_mac: "3C:2D:50:20:30:3D",
        ippz_wifi_ssid: "1+X",
        ippz_wifi_mm: "",
      },

      labelPosition: "left",
      ippz_wangkou: "ETH1",
      ippz_zwym: "",
      ippz_zwym1: "255",
      ippz_zwym2: "255",
      ippz_zwym3: "255",
      ippz_zwym4: "0",
      ippz_ip: "",
      ippz_ip1: "",
      ippz_ip2: "",
      ippz_ip3: "",
      ippz_ip4: "",
      ippz_mrwg: "",
      ippz_mrwg1: "192",
      ippz_mrwg2: "168",
      ippz_mrwg3: "1",
      ippz_mrwg4: "1",
      ippz_dns_sx: "",
      ippz_dns_sx1: "8",
      ippz_dns_sx2: "8",
      ippz_dns_sx3: "8",
      ippz_dns_sx4: "8",
      ippz_dns_by: "",
      ippz_dns_by1: "8",
      ippz_dns_by2: "8",
      ippz_dns_by3: "8",
      ippz_dns_by4: "8",
      ippz_mac: "3C:2D:50:20:30:3D",
      loading: false,
      // 无线的数据
      ippz_wifi_ssid: "",
      ippz_wifi_mm: "",
      ippz_lwfs: "2",
      dialogFormVisible: true,
      formLabelWidth: "100px",
      // 测试数据的变量
      answer_datas: "",
      // 有线的规则校验
      rules_ippz_wirle: {
        ippz_wangkou: [
          { required: true, message: "请输入网口", trigger: "change" },
        ],
        ippz_zwym: [
          { required: true, message: "请输入子网掩码", trigger: "blur" },
        ],
        ippz_ip: [{ required: true, message: "请输入IP地址", trigger: "blur" }],
        ippz_mrwg: [
          { required: true, message: "请输入默认网关", trigger: "blur" },
        ],
        ippz_dns_sx: [
          { required: true, message: "请输入首选DNS", trigger: "blur" },
        ],
        ippz_dns_by: [
          { required: true, message: "请输入备选DNS", trigger: "blur" },
        ],
        ippz_mac: [
          { required: true, message: "请输入备选DNS", trigger: "blur" },
        ],
      },
      rules_ippz_wifi: {
        ippz_wifi_ssid: [
          { required: true, message: "请输入SSID", trigger: "blur" },
        ],
        ippz_wifi_mm: [
          { required: true, message: "请输入密码", trigger: "blur" },
        ],
        ippz_dns_sx: [
          { required: true, message: "请输入首选DNS", trigger: "blur" },
        ],
        ippz_dns_by: [
          { required: true, message: "请输入备选DNS", trigger: "blur" },
        ],
        ippz_mac: [
          { required: true, message: "请输入mac地址", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    // 封装合并输入框的数据
    Merge(value, value1, value2, value3, value4) {
      value = value1 + "." + value2 + "." + value3 + "." + value4;
      return value;
    },
    SliceData(value) {
      let res = value.split(".");
      // console.log(res);
      return res;
    },
    // 保存数据,发送请求
    async saveAll() {
      this.dialogFormVisible = false;
      this.formdata.ippz_wangkou = this.ippz_wangkou;
      this.formdata.ippz_ip = this.Merge(
        this.ippz_ip,
        this.ippz_ip1,
        this.ippz_ip2,
        this.ippz_ip3,
        this.ippz_ip4
      );
      this.formdata.ippz_zwym = this.Merge(
        this.ippz_zwym,
        this.ippz_zwym1,
        this.ippz_zwym2,
        this.ippz_zwym3,
        this.ippz_zwym4
      );
      this.formdata.ippz_mrwg = this.Merge(
        this.ippz_mrwg,
        this.ippz_mrwg1,
        this.ippz_mrwg2,
        this.ippz_mrwg3,
        this.ippz_mrwg4
      );
      this.formdata.ippz_dns_sx = this.Merge(
        this.ippz_dns_sx,
        this.ippz_dns_sx1,
        this.ippz_dns_sx2,
        this.ippz_dns_sx3,
        this.ippz_dns_sx4
      );
      this.formdata.ippz_dns_by = this.Merge(
        this.ippz_dns_by,
        this.ippz_dns_by1,
        this.ippz_dns_by2,
        this.ippz_dns_by3,
        this.ippz_dns_by4
      );
      this.formdata.ippz_mac = this.ippz_mac;
      this.formdata.ippz_wifi_ssid = this.ippz_wifi_ssid;
      this.formdata.ippz_wifi_mm = this.ippz_wifi_mm;
      let res = await SaveUserAnswer({
        mark: "DB-IotLite_wx",
        user_id: "USER202307301114011710",
        mark_value: JSON.stringify(this.formdata),
      });
      if (res.status == 200) {
        this.$message({
          message: '保存成功',
          type: 'success'
        });
      }
      console.log(res);
      sessionStorage.setItem("DB-IotLite_wx", JSON.stringify(this.formdata));
    },
    CopyText() {
      this.$message({
        message: "复制成功",
        type: "success",
      });
    },
    async GetUserAnswer() {
      this.loading = true;
      let res2 = await GetUserAnswer({
        mark: "DB-IotLite_wx",
        user_id: "USER202307301114011710",
      });
      this.loading = false;
      console.log(res2);
      if (res2.data.data && res2.data.state) {
        let res3 = JSON.parse(res2.data.data.mark_value);
        console.log(res3);
        this.ippz_wangkou = res3.ippz_wangkou;
        this.ippz_mrwg = res3.ippz_mrwg;
        this.ippz_ip = res3.ippz_ip;
        this.ippz_zwym = res3.ippz_zwym;
        this.ippz_dns_sx = res3.ippz_dns_sx;
        this.ippz_dns_by = res3.ippz_dns_by;
        this.ippz_mac = res3.ippz_mac;
        this.ippz_wifi_ssid = res3.ippz_wifi_ssid;
        this.ippz_wifi_mm = res3.ippz_wifi_mm;
        if (res3.ippz_mrwg) {
          this.ippz_mrwg1 = this.SliceData(this.ippz_mrwg)[0];
          this.ippz_mrwg2 = this.SliceData(this.ippz_mrwg)[1];
          this.ippz_mrwg3 = this.SliceData(this.ippz_mrwg)[2];
          this.ippz_mrwg4 = this.SliceData(this.ippz_mrwg)[3];
          this.ippz_ip1 = this.SliceData(this.ippz_ip)[0];
          this.ippz_ip2 = this.SliceData(this.ippz_ip)[1];
          this.ippz_ip3 = this.SliceData(this.ippz_ip)[2];
          this.ippz_ip4 = this.SliceData(this.ippz_ip)[3];
          this.ippz_zwym1 = this.SliceData(this.ippz_zwym)[0];
          this.ippz_zwym2 = this.SliceData(this.ippz_zwym)[1];
          this.ippz_zwym3 = this.SliceData(this.ippz_zwym)[2];
          this.ippz_zwym4 = this.SliceData(this.ippz_zwym)[3];
          this.ippz_dns_sx1 = this.SliceData(this.ippz_dns_sx)[0];
          this.ippz_dns_sx2 = this.SliceData(this.ippz_dns_sx)[1];
          this.ippz_dns_sx3 = this.SliceData(this.ippz_dns_sx)[2];
          this.ippz_dns_sx4 = this.SliceData(this.ippz_dns_sx)[3];
          this.ippz_dns_by1 = this.SliceData(this.ippz_dns_by)[0];
          this.ippz_dns_by2 = this.SliceData(this.ippz_dns_by)[1];
          this.ippz_dns_by3 = this.SliceData(this.ippz_dns_by)[2];
          this.ippz_dns_by4 = this.SliceData(this.ippz_dns_by)[3];
        }
      }
    }
  },
  created() {
    this.GetUserAnswer();
  },
  mounted() { },
  directives: {
    copy: {
      bind(el, { value }) {
        // 给元素赋值一个$value值,即指令绑定的值
        el.$value = value;
        el.handler = () => {
          // 如果可复制的值为空的时候，给出提示；
          if (!el.$value) {
            console.log("无复制内容");
            return;
          }
          // 动态创建 textarea 标签
          const textarea = document.createElement("textarea");
          // 将该 textarea 设为 readonly 防止 IOS 下自动唤起键盘，同时将 textarea 移除可视区域
          textarea.readOnly = "readonly";
          textarea.style.position = "absolute";
          textarea.style.left = "-9999px";
          // 将要copy的值赋值给textarea 标签的value属性
          textarea.value = el.$value;
          // 将textarea 插入到body中
          document.body.appendChild(textarea);
          // 选中值并复制
          textarea.select();
          const result = document.execCommand("Copy");
          if (result) {
            console.log("复制成功");
          }
          document.body.removeChild(textarea);
        };
        // 绑定点击事件，点击的时候copy值
        el.addEventListener("click", el.handler);
      },
      // 当传递进来的值更新的时候触发
      componentUpdated(el, { value }) {
        el.$value = value;
      },
      // 指令与元素解绑的时候，移除事件绑定
      unbind(el) {
        el.removeEventListener("click", el.handler);
      },
    },
  },
};
</script>

<style lang="less" scoped>
/deep/ .wired,
/deep/ .wifi {
  .el-row {
    .el-col-12:last-child {
      padding-right: 120px;
    }

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

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

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

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

      .mac {
        // display: flex;
        align-items: center;

        .el-button {
          position: absolute;
          left: 200px;
          bottom: 5px;
          margin-left: 10px;
          margin-top: 10px;
          width: 80px;
          height: 30px;
          line-height: 0.5;
        }
      }
    }
  }
}

.netWay {
  margin-bottom: 20px;
  width: 80px;
  color: rgba(16, 16, 16, 1);
  font-size: 20px;
  text-align: left;
}

.footer {
  position: absolute;
  left: 360px;
  bottom: 2px;
}
</style>
