<template>
  <el-dialog title="设备设置" :visible.sync="dialogFormVisible" width="400px" append-to-body>
    <div class="container">
      <el-form ref="dataForm" :rules="rules" :model="Info" label-position="right" label-width="130px">
        <el-form-item :label="'报警阈值('+unitMap[deviceType]+')'" prop="thresholdVol">
          <el-input v-model="Info.thresholdVol" type="text" />
        </el-form-item>
        <el-form-item label="采样间隔(分)" prop="collectInterval">
          <el-input v-model="Info.collectInterval" type="text" />
        </el-form-item>
        <el-form-item label="上传周期(分)" prop="uploadCycle">
          <el-input v-model.number="Info.uploadCycle" type="number" />
        </el-form-item>
        <el-form-item label="重传次数" prop="retryNum">
          <el-input-number v-model.number="Info.retryNum" />
        </el-form-item>
<!--        <el-form-item label="IP地址" prop="IP">-->
<!--          <el-input v-model="Info.IP" type="text" placeholder="非必填，格式：xxx.xxx.xxx.xxx" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="端口" prop="port">-->
<!--          <el-input v-model="Info.port" type="text" placeholder="非必填" />-->
<!--        </el-form-item>-->
      </el-form>
    </div>
    <template slot="footer">
      <div class="item">
        <el-button type="primary" @click="save">
          确定
        </el-button>
        <el-button @click="close">
          取消
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import { validateFloatPlus } from '@/utils/validate'
import { configWarn } from '@/api/models'
export default {
  // model: {
  //   prop: 'modelVal',//指向props的参数名
  //   event: 'change'//事件名称
  // },
  name: 'ShowConfig',
  props: ['configInfo', 'deviceType'],
  data() {
    const validateIp = (rule, value, callback) => {
      if (value !== '') {
        if ((/((25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))\.){3}(25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))/).test(value) === false) {
          callback(new Error('请填写正确的ip地址'))
        } else {
          callback()
        }
      } else {
        // callback(new Error('ip地址不能为空'))
        callback()
      }
    }
    const validatePort = (rule, value, callback) => {
      if (value !== '') {
        if ((/[1-9]$|(^[1-9][0-9]$)|(^[1-9][0-9][0-9]$)|(^[1-9][0-9][0-9][0-9]$)|(^[1-6][0-5][0-5][0-3][0-5]$)/).test(value) === false) {
          callback(new Error('请填写正确的端口号'))
        } else {
          callback()
        }
      } else {
        // callback(new Error('端口号不能为空'))
        callback()
      }
    }
    const validateCollectCycle = (rule, value, callback) => {
      if (value !== '') {
        if (validateFloatPlus(value, 1, 60) === true) {
          callback()
        } else {
          callback(new Error('请填写1到60数值'))
        }
      } else {
        callback(new Error('采样间隔不能为空'))
      }
    }
    const validateUpPeriod = (rule, value, callback) => {
      if (value !== '') {
        if (validateFloatPlus(value, 0, 10080) === true) {
          callback()
        } else {
          callback(new Error('请填写0到10080之间数值'))
        }
      } else {
        callback(new Error('上传周期不能为空'))
      }
    }
    return {
      dialogFormVisible: false,
      unitMap: {
        '23': '%LEL',
        '22': 'ppm',
        '24': 'MPa'
      },
      Info: {
        thresholdVol: '1',
        collectInterval: 10,
        uploadCycle: '7',
        retryNum: '5',
        IP: '',
        port: '',
        id: '24'
      },
      rules: {
        collectInterval: [{ required: true, trigger: ['blur', 'change'], validator: validateCollectCycle }],
        uploadCycle: [{ required: true, trigger: ['blur', 'change'], validator: validateUpPeriod }],
        id: [{ required: true, message: '设备编号不能为空', trigger: ['blur', 'change'] }],
        retryNum: [{ required: true, message: '重传次数不能为空', trigger: ['blur', 'change'] }],
        thresholdVol: [{ required: true, message: '报警阈值不能为空', trigger: ['blur', 'change'] }],
        IP: [{ trigger: ['blur', 'change'], validator: validateIp }],
        port: [{ trigger: ['blur', 'change'], validator: validatePort }]
      } // 前端校验规则
    }
  },
  mounted() {
    this.Info = { ...this.configInfo }
  },
  methods: {
    initDialog() {
      this.dialogFormVisible = true
    },
    close() {
      this.dialogFormVisible = false
      this.$emit('close')
    },
    save() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const params = {
            ...this.Info,
            deviceType: this.deviceType
          }
          configWarn(params).then(res => {
            this.$message({
              message: '保存成功',
              type: 'success'
            })
            this.close()
          })
        }
      })
    }
  }
}
</script>

<style scoped>
.container {
  height: 100%;
  width: 100%;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
}

.content {
  width: 203px;
  font-size: 12px;
  top: 50%;
  left: 50%;
  background: #fff;
  padding: 30px 10px;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.item {
  display: flex;
  padding: 5px;
  align-items: center;
  width: 100%;
}

.item > div {
  flex: 1;
  padding: 0 3px;
  text-align: right;
}

.inp {
  flex: 2 !important;
  display: flex;
  text-align: left;
}

input {
  width: 100%;
  outline: none;
  border: 1px solid;
}

.btn {
  display: block;
  height: 18px;
  width: 100%;
  background: skyblue;
  border-radius: 4px;
  color: #fff;
  text-align: center !important;
  line-height: 18px;
  margin: 0 3px;

}
</style>
