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

    <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="btl">
          <el-col :span="15">
            <el-select v-model="ruleForm.btl">
              <el-option label="9600" value="shanghai"></el-option>
              <el-option label="区域二" value="beijing"></el-option>
            </el-select>
          </el-col>
        </el-form-item>
        <el-form-item label="数据位(bit)" prop="data">
          <el-col :span="15">
            <el-select v-model="ruleForm.data">
              <el-option label="8" value="shanghai"></el-option>
              <el-option label="区域二" value="beijing"></el-option>
            </el-select>
          </el-col>
        </el-form-item>
        <el-form-item label="检验位" prop="check">
          <el-col :span="15">
            <el-select v-model="ruleForm.check">
              <el-option label="无校验(None)" value="shanghai"></el-option>
              <el-option label="区域二" value="beijing"></el-option>
            </el-select>
          </el-col>
        </el-form-item>
        <el-form-item label="停止位" prop="stop">
          <el-col :span="15">
            <el-select v-model="ruleForm.stop">
              <el-option label="1" value="shanghai"></el-option>
              <el-option label="区域二" value="beijing"></el-option>
            </el-select>
          </el-col>
        </el-form-item>
        <el-form-item label="流控模式" prop="waterContorl">
          <el-col :span="15">
            <el-select v-model="ruleForm.waterContorl">
              <el-option label="None" value="shanghai"></el-option>
              <el-option label="区域二" value="beijing"></el-option>
            </el-select>
          </el-col>
        </el-form-item>
        <el-form-item label="打包长度(bytes)" prop="bagLength">
          <el-col :span="15">
            <el-input
              v-model="ruleForm.bagLength"
              placeholder="0-1460"
            ></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="打包时间(ms)" prop="time">
          <el-col :span="15">
            <el-input v-model="ruleForm.time" placeholder="0-255"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="同步波特率(RF2217)" prop="RFbtl">
          <el-col :span="15">
            <el-select v-model="ruleForm.RFbtl">
              <el-option label="None" value="shanghai"></el-option>
              <el-option label="区域二" value="beijing"></el-option>
            </el-select>
          </el-col>
        </el-form-item>
        <el-checkbox
          label="使能串口心跳包"
          name="type"
          v-model="checked"
        ></el-checkbox>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button
          type="primary"
          @click="submitForm('ruleForm'), (dialogVisible = false)"
          >保存</el-button
        >
        <el-button @click="dialogVisible = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { isValidPrimaryDNS } from "../validate";
export default {
  data() {
    return {
      datas: [
        {
          name: "端口",
          children: [
            {
              name: "端口1",
            },
            {
              name: "端口2",
            },
            {
              name: "端口3",
            },
            {
              name: "端口4",
            },
            {
              name: "网页转串口",
            },
          ],
        },
      ],
      checked: false,
      defaultProps: {
        children: "children",
        label: "name",
      },
      dialogVisible: false,
      labelPosition: "left",
      ruleForm: {
        btl: "",
        data: "",
        check: "",
        stop: "",
        waterContorl: "",
        bagLength: "",
        time: "",
        RFbtl: "",
      },
      rules: {
        btl: [{ required: true, message: "请选择波特率", trigger: "change" }],
        data: [{ required: true, message: "请选择数据位", trigger: "change" }],
        stop: [{ required: true, message: "请选择停止位", trigger: "change" }],
        check: [{ required: true, message: "请选择检验位", trigger: "change" }],
        waterContorl: [
          { required: true, message: "请选择流控模式", trigger: "blur" },
        ],
        bagLength: [
          { required: true, message: "请输入打包长度", trigger: "blur" },
          // 自定义校验规则，这里限制最大长度为1000
          { validator: this.validatePackagingLength, trigger: "blur" },
          { type: "number", message: "打包长度必须为数字值", trigger: "blur" },
        ],
        time: [
          { required: true, message: "请输入打包时间", trigger: "blur" },
          { type: "number", message: "打包时间必须为数字值", trigger: "blur" },
          // 自定义校验规则，这里限制最大长度为1000
          { validator: this.validatTime, trigger: "blur" },
        ],
        RFbtl: [
          { required: true, message: "请选择同步波特率", trigger: "change" },
        ],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          alert("submit!");
        } 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>
.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);
}
</style>
