<!-- 有线网关设置 -->
<template>
  <div>
    <!-- <div> <el-button type="info" plain @click="dialogFormVisible = true">IP设置</el-button></div> -->
    <el-dialog title="IP设置" :visible.sync="dialogFormVisible" :append-to-body="true" width="894px" v-loading="loading">
      <div class="form_content">
        <span class="title_center">注意：本页面涉及排错，请检查修改默认值</span>
        <el-form :model="formData" :label-position="labelPosition" label-width="80px" :rules="rules_ippz_wirle"
          ref="formData">
          <div class="wired">
            <el-row>
              <el-col :span="12">
                <el-form-item label="网口:" :label-width="formLabelWidth" prop="ippz_wangkou">
                  <el-select v-model="formData.ippz_wangkou" 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="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="子网掩码:" :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="默认网关:" :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="首选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="formData.ippz_mac"></el-input><el-button
                        v-copy="formData.ippz_mac" @click="CopyText">复制</el-button>
                    </el-col>
                  </el-row>
                </el-form-item>
              </el-col>
            </el-row>
          </div>
        </el-form>

        <div slot="footer" class="dialog-footer footer">
          <el-button type="primary" @click="submitForm">保 存</el-button>
          <el-button @click="dialogFormVisible = false">取 消</el-button>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { SaveUserAnswer, GetUserAnswer } from "../api/user";
export default {
  name: "GateWay",
  data() {
    return {
      // disabled: true,
      user_id: "",
      // 判断是否保存过
      loading: false,
      formData: {
        ippz_wangkou: "ETH0",
        ippz_zwym: "",
        ippz_mrwg: "",
        ippz_dns_sx: "",
        ippz_dns_by: "",
        ippz_ip: "",
        ippz_mac: "3C:2D:50:20:30:3D",
      },
      labelPosition: "left",
      ippz_zwym1: "255",
      ippz_zwym2: "255",
      ippz_zwym3: "255",
      ippz_zwym4: "0",
      ippz_ip1: "",
      ippz_ip2: "",
      ippz_ip3: "",
      ippz_ip4: "",
      ippz_mrwg1: "192",
      ippz_mrwg2: "168",
      ippz_mrwg3: "1",
      ippz_mrwg4: "1",
      ippz_dns_sx1: "8",
      ippz_dns_sx2: "8",
      ippz_dns_sx3: "8",
      ippz_dns_sx4: "8",

      ippz_dns_by1: "8",
      ippz_dns_by2: "8",
      ippz_dns_by3: "8",
      ippz_dns_by4: "8",
      dialogFormVisible: true,
      formLabelWidth: "100px",
      // 有线的规则校验
      rules_ippz_wirle: {
        ippz_wangkou: [
          {
            required: true,
            message: "请输入网口",
            trigger: "change"
          },
        ],
        ippz_zwym: [
          {
            required: true,
            message: "",
            validator: this.validateZwym,
            trigger: "blur"
          },
        ],
        ippz_ip: [
          {
            required: true,
            message: "",
            validator: this.validateIp,
            trigger: "blur"
          },
        ],
        ippz_mrwg: [
          {
            required: true,
            message: "",
            validator: this.validateMrwg,
            trigger: "blur"
          },
        ],
        ippz_dns_sx: [
          {
            required: true, message: "",
            validator: this.validateDnsSx,
            trigger: "blur"
          },
        ],
        ippz_dns_by: [
          {
            required: true,
            message: "",
            validator: this.validateDnsBy,
            trigger: "blur"
          },
        ],
        ippz_mac: [
          { required: true, message: "请输入MAC地址", trigger: "blur" },
        ],
      },
    };
  },
  computed: {
    // isSaveButtonDisabled() {
    //   return (
    //     !this.$refs.rules_ippz_wirle ||
    //     this.$refs.rules_ippz_wirle.validateSync().length > 0
    //   );
    // },
  },
  methods: {
    getParameter(data) {
      console.log(data);
      this.user_id = data;
    },
    // 提交时的校验
    submitForm() {
      this.$refs["formData"].validate((valid) => {
        if (valid) {
          this.saveAll();
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 封装合并输入框的数据
    Merge(value, value1, value2, value3, value4) {
      value = value1 + "." + value2 + "." + value3 + "." + value4;
      return value;
    },
    //子网掩码验证
    validateZwym(rule, value, callback) {
      if (
        this.ippz_zwym1 +
        "." +
        this.ippz_zwym2 +
        "." +
        this.ippz_zwym3 +
        "." +
        this.ippz_zwym4 ===
        "..."
      ) {
        callback(new Error("请输入子网掩码"));
      } else if (
        !/^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/.test(
          this.ippz_zwym1 +
          "." +
          this.ippz_zwym2 +
          "." +
          this.ippz_zwym3 +
          "." +
          this.ippz_zwym4
        )
      ) {
        callback(new Error("请输入有效的子网掩码"));
      } else {
        callback();
      }
    },
    //ip地址验证
    validateIp(rule, value, callback) {
      if (
        this.ippz_ip1 +
        "." +
        this.ippz_ip2 +
        "." +
        this.ippz_ip3 +
        "." +
        this.ippz_ip4 ===
        "..."
      ) {
        callback(new Error("请输入IP地址"));
      } else if (
        !/^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/.test(
          this.ippz_ip1 +
          "." +
          this.ippz_ip2 +
          "." +
          this.ippz_ip3 +
          "." +
          this.ippz_ip4
        )
      ) {
        callback(new Error("请输入有效的IP地址"));
      } else {
        callback();
      }
    },
    //默认网关验证
    validateMrwg(rule, value, callback) {
      if (
        this.ippz_mrwg1 +
        "." +
        this.ippz_mrwg2 +
        "." +
        this.ippz_mrwg3 +
        "." +
        this.ippz_mrwg4 ===
        "..."
      ) {
        callback(new Error("请输入网关地址"));
      } else if (
        !/^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/.test(
          this.ippz_mrwg1 +
          "." +
          this.ippz_mrwg2 +
          "." +
          this.ippz_mrwg3 +
          "." +
          this.ippz_mrwg4
        )
      ) {
        callback(new Error("请输入有效的网关地址"));
      } else {
        callback();
      }
    },
    //首选DNS服务验证
    validateDnsSx(rule, value, callback) {
      if (
        this.ippz_dns_sx1 +
        "." +
        this.ippz_dns_sx2 +
        "." +
        this.ippz_dns_sx3 +
        "." +
        this.ippz_dns_sx4 ===
        "..."
      ) {
        callback(new Error("请输入主DNS服务器地址"));
      } else if (!/^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/.test
        (
          this.ippz_dns_sx1 +
          "." +
          this.ippz_dns_sx2 +
          "." +
          this.ippz_dns_sx3 +
          "." +
          this.ippz_dns_sx4
        )
      ) {
        callback(new Error("请输入有效的主DNS服务器地址"));
      } else {
        callback();
      }
    },
    //备选dns服务验证
    validateDnsBy(rule, value, callback) {
      if (
        this.ippz_dns_by1 +
        "." +
        this.ippz_dns_by2 +
        "." +
        this.ippz_dns_by3 +
        "." +
        this.ippz_dns_by4 ===
        "..."
      ) {
        callback(new Error("请输入备用DNS服务器地址"));
      } else if (!/^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/.test
        (
          this.ippz_dns_by1 +
          "." +
          this.ippz_dns_by2 +
          "." +
          this.ippz_dns_by3 +
          "." +
          this.ippz_dns_by4
        )
      ) {
        callback(new Error("请输入有效的备用DNS服务器地址"));
      } else {
        callback();
      }
    },

    // 保存数据, 发送请求
    async saveAll() {
      this.dialogFormVisible = false;
      this.isSaveAnswer = true;
      this.formData.ippz_ip = this.Merge(
        this.formData.ippz_ip,
        this.ippz_ip1,
        this.ippz_ip2,
        this.ippz_ip3,
        this.ippz_ip4
      );
      this.formData.ippz_zwym = this.Merge(
        this.formData.ippz_zwym,
        this.ippz_zwym1,
        this.ippz_zwym2,
        this.ippz_zwym3,
        this.ippz_zwym4
      );
      this.formData.ippz_mrwg = this.Merge(
        this.formData.ippz_mrwg,
        this.ippz_mrwg1,
        this.ippz_mrwg2,
        this.ippz_mrwg3,
        this.ippz_mrwg4
      );
      this.formData.ippz_dns_sx = this.Merge(
        this.formData.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.formData.ippz_dns_by,
        this.ippz_dns_by1,
        this.ippz_dns_by2,
        this.ippz_dns_by3,
        this.ippz_dns_by4
      );
      console.log(this.formData);
      sessionStorage.setItem("DB-IotLite_yx", JSON.stringify(this.formData));
      let data = {
        mark: "DB-IotLite_yx",
        user_id: this.user_id,
        mark_value: JSON.stringify(this.formData),
      };
      let res = await SaveUserAnswer(data);
      if (res.data.state) {
        this.$message({
          message: "保存成功",
          type: "success",
        });
      }else{
        this.$message({
          message: `保存失败,原因是${res.data.message}`,
          type: "error",
        });
      }
      console.log(res);
    },
    CopyText() {
      this.$message({
        message: "复制成功",
        type: "success",
      });
    },
    async getUserAnswer() {
      this.loading = true;
      let res2 = await GetUserAnswer({
        mark: "DB-IotLite_yx",
        user_id: this.user_id,
      });
      this.loading = false;
      console.log(res2);
      if (res2.data.data && res2.data.state) {
        console.log("391");
        let res3 = JSON.parse(res2.data.data.mark_value);
        console.log(res3);
        if (res3.ippz_wangkou) {
          this.formData.ippz_wangkou = res3.ippz_wangkou;
        }
        if (res3.ippz_mac) {
          this.formData.ippz_mac = res3.ippz_mac;
        }
        if (res3.ippz_mrwg) {
          this.ippz_mrwg1 = res3.ippz_mrwg.split(".")[0];
          this.ippz_mrwg2 = res3.ippz_mrwg.split(".")[1];
          this.ippz_mrwg3 = res3.ippz_mrwg.split(".")[2];
          this.ippz_mrwg4 = res3.ippz_mrwg.split(".")[3];
          this.ippz_ip1 = res3.ippz_ip.split(".")[0];
          this.ippz_ip2 = res3.ippz_ip.split(".")[1];
          this.ippz_ip3 = res3.ippz_ip.split(".")[2];
          this.ippz_ip4 = res3.ippz_ip.split(".")[3];
          this.ippz_zwym1 = res3.ippz_zwym.split(".")[0];
          this.ippz_zwym2 = res3.ippz_zwym.split(".")[1];
          this.ippz_zwym3 = res3.ippz_zwym.split(".")[2];
          this.ippz_zwym4 = res3.ippz_zwym.split(".")[3];
          this.ippz_dns_sx1 = res3.ippz_dns_sx.split(".")[0];
          this.ippz_dns_sx2 = res3.ippz_dns_sx.split(".")[1];
          this.ippz_dns_sx3 = res3.ippz_dns_sx.split(".")[2];
          this.ippz_dns_sx4 = res3.ippz_dns_sx.split(".")[3];
          this.ippz_dns_by1 = res3.ippz_dns_by.split(".")[0];
          this.ippz_dns_by2 = res3.ippz_dns_by.split(".")[1];
          this.ippz_dns_by3 = res3.ippz_dns_by.split(".")[2];
          this.ippz_dns_by4 = res3.ippz_dns_by.split(".")[3];
        }
      }
    },
  },
  created() {
    let that = this;
    window.getParameter = that.getParameter;
    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>
.title_center{
  font-size: 18px;
  color: red;
  position: absolute;
  left: 100px;
  top: 20px;
}
/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>
