<template>
  <div>
    <div v-show="dialogVisible" class="tree_menu">
      <el-tree
        :data="datas"
        :props="defaultProps"
        :default-expanded-keys="['keyOfNodeToExpand']"
        :default-checked-keys="['keyOfNodeToSelect']"
      ></el-tree>
      <!-- 树形控件右键组件 -->
    </div>

    <div class="port_content">
      <el-dialog title="端口配置" :visible.sync="dialogVisible" width="30%">
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
          label-width="150px"
          class="demo-ruleForm"
          :label-position="labelPosition"
        >
          <el-form-item label="波特率(bps)" prop="dkpz_btl">
            <el-col :span="15">
              <el-select v-model="ruleForm.dkpz_btl">
                <el-option label="9600" value="9600"></el-option>
                <el-option label="19200" value="19200"></el-option>
                <el-option label="28800" value="28800"></el-option>
                <el-option label="38400" value="38400"></el-option>
                <el-option label="48000" value="48000"></el-option>
                <el-option label="57600" value="57600"></el-option>
                <el-option label="67200" value="67200"></el-option>
                <el-option label="76800" value="76800"></el-option>
                <el-option label="86400" value="86400"></el-option>
                <el-option label="96000" value="96000"></el-option>
                <el-option label="105600" value="105600"></el-option>
                <el-option label="115200" value="115200"></el-option>
              </el-select>
            </el-col>
          </el-form-item>
          <el-form-item label="数据位(bit)" prop="dkpz_sjw">
            <el-col :span="15">
              <el-select v-model="ruleForm.dkpz_sjw">
                <el-option label="8" value="8"></el-option>
                <el-option label="7" value="7"></el-option>
              </el-select>
            </el-col>
          </el-form-item>
          <el-form-item label="检验位" prop="dkpz_jyw">
            <el-col :span="15">
              <el-select v-model="ruleForm.dkpz_jyw">
                <el-option
                  label="无校验(None)"
                  value="无校验(None)"
                ></el-option>
                <el-option label="奇校验" value="奇校验"></el-option>
                <el-option label="偶校验" value="偶校验"></el-option>
              </el-select>
            </el-col>
          </el-form-item>
          <el-form-item label="停止位" prop="dkpz_tzw">
            <el-col :span="15">
              <el-select v-model="ruleForm.dkpz_tzw">
                <el-option label="1" value="1"></el-option>
                <el-option label="2" value="2"></el-option>
              </el-select>
            </el-col>
          </el-form-item>
          <el-form-item label="流控模式" prop="dkpz_lkms">
            <el-col :span="15">
              <el-select v-model="ruleForm.dkpz_lkms">
                <el-option label="None" value="None"></el-option>
                <el-option label="XON/XOF" value="XON/XOF"></el-option>
              </el-select>
            </el-col>
          </el-form-item>
          <el-form-item label="打包长度(bytes)" prop="dkpz_dbcd">
            <el-col :span="15">
              <el-input
                v-model="ruleForm.dkpz_dbcd"
                placeholder="0-1460"
              ></el-input>
            </el-col>
          </el-form-item>
          <el-form-item label="打包时间(ms)" prop="dkpz_dbsj">
            <el-col :span="15">
              <el-input
                v-model="ruleForm.dkpz_dbsj"
                placeholder="0-255"
              ></el-input>
            </el-col>
          </el-form-item>
          <el-form-item label="同步波特率(RF2217)" prop="dkpz_tbbtl">
            <el-col :span="15">
              <el-select v-model="ruleForm.dkpz_tbbtl">
                <el-option label="None" value="None"></el-option>
                <el-option label="启用" value="启用"></el-option>
              </el-select>
            </el-col>
          </el-form-item>
          <el-checkbox
            label="使能串口心跳包"
            name="type"
            v-model="ruleForm.dkpz_snckxtb"
          ></el-checkbox>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">保 存</el-button>
          <el-button @click="dialogVisible = false">取 消</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { SaveUserAnswer, GetUserAnswer } from "@/api/user";
export default {
  data() {
    return {
      user_id: "",
      datas: [
        {
          name: "端口",
          children: [
            {
              name: "端口1",
            },
            {
              name: "端口2",
            },
            {
              name: "端口3",
            },
            {
              name: "端口4",
            },
            {
              name: "网页转串口",
            },
          ],
        },
      ],
      defaultProps: {
        children: "children",
        label: "name",
      },
      dialogVisible: true,
      labelPosition: "left",
      ruleForm: {
        dkpz_btl: "",
        dkpz_sjw: "8",
        dkpz_jyw: "",
        dkpz_tzw: "",
        dkpz_lkms: "",
        dkpz_dbcd: "",
        dkpz_dbsj: "",
        dkpz_tbbtl: "",
        dkpz_snckxtb: false,
      },
      ippz_ip_hqfs: "",
      ippz_dns_hqfs: "",
      ippz_ip: "",
      ippz_zwym: "",
      ippz_mrwg: "",
      ippz_dns_zy: "",
      ippz_dns_by: "",
      rules: {
        dkpz_btl: [
          { required: true, message: "请选择波特率", trigger: "change" },
        ],
        dkpz_sjw: [
          { required: true, message: "请选择数据位", trigger: "change" },
        ],
        dkpz_tzw: [
          { required: true, message: "请选择停止位", trigger: "change" },
        ],
        dkpz_jyw: [
          { required: true, message: "请选择检验位", trigger: "change" },
        ],
        dkpz_lkms: [
          { required: true, message: "请选择流控模式", trigger: "blur" },
        ],
        dkpz_dbcd: [
          { required: true, message: "请输入打包长度", trigger: "blur" },
          // 自定义校验规则，这里限制最大长度为1000
          // { type: "number", message: "打包长度必须为数字值", trigger: "blur" },
        ],
        dkpz_tbbtl: [
          { required: true, message: "请输入打包时间", trigger: "blur" },
          // { type: "number", message: "打包时间必须为数字值", trigger: "blur" },
          // 自定义校验规则，这里限制最大长度为1000
        ],
        dkpz_tbbtl: [
          { required: true, message: "请选择同步波特率", trigger: "change" },
        ],
      },
    };
  },
  mounted() {
    this.user_id = window.location.href.split("?")[1];
    this.GetUserAnswer();
  },
  methods: {
    async GetUserAnswer() {
      let { data } = await GetUserAnswer({
        mark: "CKFWQ_yx",
        user_id: this.user_id,
        // user_id: "USER202311140204340206",
      });
      if (data.data) {
        let obj = JSON.parse(data.data.mark_value);
        this.ruleForm.dkpz_btl = obj.dkpz_btl;
        this.ruleForm.dkpz_sjw = obj.dkpz_sjw;
        this.ruleForm.dkpz_jyw = obj.dkpz_jyw;
        this.ruleForm.dkpz_tzw = obj.dkpz_tzw;
        this.ruleForm.dkpz_lkms = obj.dkpz_lkms;
        this.ruleForm.dkpz_dbcd = obj.dkpz_dbcd;
        this.ruleForm.dkpz_dbsj = obj.dkpz_dbsj;
        this.ruleForm.dkpz_tbbtl = obj.dkpz_tbbtl;
        this.ruleForm.dkpz_snckxtb = Boolean(obj.dkpz_snckxtb);
        this.ippz_ip_hqfs = obj.ippz_ip_hqfs;
        this.ippz_dns_hqfs = obj.ippz_dns_hqfs;
        this.ippz_ip = obj.ippz_ip;
        this.ippz_zwym = obj.ippz_zwym;
        this.ippz_mrwg = obj.ippz_mrwg;
        this.ippz_dns_zy = obj.ippz_dns_zy;
        this.ippz_dns_by = obj.ippz_dns_by;
      }
      console.log(data.data);
      // alert(JSON.stringify(data.data.mark_value));
    },
    async savePort() {
      // this.ruleForm.dkpz_snckxtb = Number(this.ruleForm.dkpz_snckxtb);
      let obj = {
        ippz_ip_hqfs: this.ippz_ip_hqfs,
        ippz_dns_hqfs: this.ippz_dns_hqfs,
        ippz_ip: this.ippz_ip,
        ippz_zwym: this.ippz_zwym,
        ippz_mrwg: this.ippz_mrwg,
        ippz_dns_zy: this.ippz_dns_zy,
        ippz_dns_by: this.ippz_dns_by,
        dkpz_btl: this.ruleForm.dkpz_btl,
        dkpz_sjw: this.ruleForm.dkpz_sjw,
        dkpz_jyw: this.ruleForm.dkpz_jyw,
        dkpz_tzw: this.ruleForm.dkpz_tzw,
        dkpz_lkms: this.ruleForm.dkpz_lkms,
        dkpz_dbcd: this.ruleForm.dkpz_dbcd,
        dkpz_dbsj: this.ruleForm.dkpz_dbsj,
        dkpz_tbbtl: this.ruleForm.dkpz_tbbtl,
        dkpz_snckxtb: this.ruleForm.dkpz_snckxtb,
      };
      console.log(obj);
      alert(JSON.stringify(obj));
      let { data } = await SaveUserAnswer({
        mark: "CKFWQ_yx",
        // user_id: "USER202311140204340206",
        user_id: this.user_id,
        mark_value: JSON.stringify(obj),
      });
      if (data.state) {
        alert(JSON.stringify(data.data.mark_value));
        this.$message({
          message: data.message,
          type: "success",
        });
      } else {
        this.$message.error(data.message);
      }
    },
    submitForm(formName) {
      this.$refs["ruleForm"].validate((valid) => {
        if (valid) {
          this.savePort();
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    validatePackagingLength(rule, value, callback) {
      if (value && parseInt(value, 10) > 1460) {
        callback(new Error("打包长度不能超过1000"));
      } else {
        callback();
      }
    },
    validatTime(rule, value, callback) {
      if (value && parseInt(value, 10) > 255) {
        callback(new Error("打包时间不能超过255"));
      } else {
        callback();
      }
    },
  },
};
</script>
<style scoped lang="less">
.tree_menu {
  position: absolute;
  width: 286px;
  height: 753px;
  z-index: 99999999999999;
  left: 64px;
  top: 104px;
  width: 286px;
  height: 753px;
  line-height: 20px;
  border-radius: 3px;
  background-color: rgba(255, 255, 255, 1);
  text-align: center;
  border: 1px solid rgba(220, 220, 220, 1);
}
/deep/ .port_content {
  .el-input {
    width: 200px;
    height: 30px;
  }
}
</style>
