<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="$t('ckportconfig.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="$t('ckportconfig.baudRate')" prop="dkpz_btl">
            <el-col :span="15">
              <el-select v-model="ruleForm.dkpz_btl" :placeholder="$t('select')">
                <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="$t('ckportconfig.dataBits')" prop="dkpz_sjw">
            <el-col :span="15">
              <el-select v-model="ruleForm.dkpz_sjw" :placeholder="$t('select')">
                <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="$t('ckportconfig.checkBit')" prop="dkpz_jyw">
            <el-col :span="15">
              <el-select v-model="ruleForm.dkpz_jyw" :placeholder="$t('select')">
                <el-option  :value="item.value" v-for="(item,index) in checkBitList" :key="index"></el-option>
                <!-- <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="$t('ckportconfig.stopBit')" prop="dkpz_tzw">
            <el-col :span="15">
              <el-select v-model="ruleForm.dkpz_tzw" :placeholder="$t('select')">
                <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="$t('ckportconfig.controlMode')" prop="dkpz_lkms">
            <el-col :span="15">
              <el-select v-model="ruleForm.dkpz_lkms" :placeholder="$t('select')">
                <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="$t('ckportconfig.packagingLength')" 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="$t('ckportconfig.packagingTime')" 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="$t('ckportconfig.synBaudRate')" prop="dkpz_tbbtl">
            <el-col :span="15">
              <el-select v-model="ruleForm.dkpz_tbbtl" :placeholder="$t('select')">
                <el-option label="None" value="None"></el-option>
                <el-option :label="$t('ckportconfig.enable')" :value="$t('ckportconfig.enable')"></el-option>
              </el-select>
            </el-col>
          </el-form-item>
          <el-checkbox
              :label="$t('ckportconfig.checked')"
            name="type"
            v-model="ruleForm.dkpz_snckxtb"
          ></el-checkbox>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">{{ $t('save') }}</el-button>
          <el-button @click="dialogVisible = false">{{ $t('cancel') }}</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import {SaveUserAnswer, GetUserAnswer, getQueryAnswer} from "@/api/user";
export default {
  data() {
    return {
      checkBitList:this.$t('ckportconfig.checkBitList'),
      user_id: "",
      datas: this.$t('ckportconfig.datas'),
      defaultProps: {
        children: "children",
        label: "name",
      },
      dialogVisible: true,
      labelPosition: "left",
      ruleForm: {
        dkpz_btl: "",
        dkpz_sjw: "",
        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: this.$t('ckportconfig.rules.dkpz_btl'), trigger: "change" },
        ],
        dkpz_sjw: [
          { required: true, message: this.$t('ckportconfig.rules.dkpz_sjw'), trigger: "change" },
        ],
        dkpz_tzw: [
          { required: true, message: this.$t('ckportconfig.rules.dkpz_tzw'), trigger: "change" },
        ],
        dkpz_jyw: [
          { required: true, message: this.$t('ckportconfig.rules.dkpz_jyw'), trigger: "change" },
        ],
        dkpz_lkms: [
          { required: true, message: this.$t('ckportconfig.rules.dkpz_lkms'), trigger: "blur" },
        ],
        dkpz_dbcd: [
          { required: true, message: this.$t('ckportconfig.rules.dkpz_dbcd'), trigger: "blur" },
          // 自定义校验规则，这里限制最大长度为1000
          // { type: "number", message: "打包长度必须为数字值", trigger: "blur" },
        ],
        dkpz_dbsj: [
          { required: true, message: this.$t('ckportconfig.rules.dkpz_dbsj'), trigger: "blur" },
          // { type: "number", message: "打包时间必须为数字值", trigger: "blur" },
          // 自定义校验规则，这里限制最大长度为1000
        ],
        dkpz_tbbtl: [
          { required: true, message: this.$t('ckportconfig.rules.dkpz_tbbtl'), 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 && data.state) {
        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;
      }else{
        getQueryAnswer({
          mark: "CKFWQ_yx",
          user_id: this.user_id
        }).then(res => {
          // let obj = JSON.parse(res.data.data.r3);
          let obj = null;
          if (this.$i18n.locale === "zh") {
            obj = JSON.parse(res.data.data.r3_en_default);
          } else {
            obj = JSON.parse(res.data.data.r3);
          }
          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: Number(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>
