<template>
  <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" append-to-body>
    <el-form ref="dataForm" :rules="rules" :model="alarmRuleForm" label-position="right" label-width="100px">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="闸井编号" prop="concencode">
            <el-input v-model="baseInfo.wellCode" type="text" placeholder="未知" disabled/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="井深" prop="simid">
            <el-input v-model="baseInfo.deep" type="text" placeholder="未知" disabled/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="设备编号" prop="lng">
            <el-input v-model="baseInfo.devcode" type="text" placeholder="未知" disabled/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="设备类型" prop="lat">
            <el-input v-model="baseInfo.deviceTypeName" type="text" placeholder="未知" disabled/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col v-if="showHighValue" :span="12">
          <el-form-item label="告警上限（m）" prop="highValue">
            <el-input v-model.trim="alarmRuleForm.highValue" type="text" placeholder="必填"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item v-if="showLowValue" label="告警下限" prop="lowValue">
            <el-input v-model.trim="alarmRuleForm.lowValue" type="text" placeholder="非必填"/>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="saveData">保存</el-button>
      <el-button @click="dialogFormVisible = false">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { updateAlarmRule } from '@/api/alarmRule'

export default {
  name: 'EditRule',
  data() {
    const validateHighValue = (rule, value, callback) => {
      if (value !== '') {
        if ((/^[+]{0,1}(\d+)$|^[+]{0,1}(\d+\.\d+)$/).test(value) === false) {
          callback(new Error('请填写大于0的数字'))
        } else {
          callback()
        }
      } else {
        callback(new Error('报警上限不能为空'))
      }
    }
    const validateLowValue = (rule, value, callback) => {
      if (value !== '') {
        if ((/^[+]{0,1}(\d+)$|^[+]{0,1}(\d+\.\d+)$/).test(value) === false) {
          callback(new Error('请填写大于0的数字'))
        } else {
          callback()
        }
      } else {
        callback(new Error('报警下限不能为空'))
      }
    }
    return {
      dialogFormVisible: false, // 对话框是否显示
      dialogStatus: '', // 对话框类型：create,update
      baseInfo: {
        wellCode: '', // 闸井编号
        wellName: '', // 闸井名称
        deep: '', // 井深
        deviceType: '', // 设备类别
        deviceTypeName: '', // 设备类别
        devcode: ''// 设备编号
      },
      alarmRuleForm: {
        id: '', // 报警规则id
        highValue: '', // 告警上限
        lowValue: '0'// 告警下限
      }, // 表单
      textMap: {
        update: '编辑',
        create: '新增'
      }, // 表头显示标题
      rules: {
        highValue: [{ required: true, trigger: ['blur', 'change'], validator: validateHighValue }],
        lowValue: [{ required: true, trigger: ['blur', 'change'], validator: validateLowValue }]
      } // 前端校验规则
    }
  },
  computed: {
    showHighValue() {
      if (this.baseInfo.deviceType === '2') {
        return true
      } else {
        return false
      }
    },
    showLowValue() {
      if (this.baseInfo.deviceType === '2') {
        return false
      } else {
        return true
      }
    }
  },
  methods: {
    // 初始化对话框
    initDialog: function(dialogStatus, dialogFormVisible, row = null) {
      this.dialogStatus = dialogStatus
      this.dialogFormVisible = dialogFormVisible
      if (dialogStatus === 'update') { // 如果是修改，将row中数据填写到输入框中
        this.alarmRuleForm = {
          id: row.id,
          highValue: row.highValue,
          lowValue: row.lowValue
        }
        this.baseInfo = {
          wellCode: row.wellCode,
          wellName: row.wellName,
          deep: row.deep,
          deviceType: row.deviceType,
          deviceTypeName: row.deviceTypeName,
          devcode: row.devcode
        }
      }
    },
    // 重置表单
    resetForm() {
      this.alarmRuleForm = {
        id: '',
        concencode: '',
        simid: '',
        pid: '',
        pids: '',
        num: '',
        tips: '',
        version: ''
      }
    },
    // 保存数据
    saveData: function() {
      this.updateData()
    },
    // 修改数据
    updateData: function() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          updateAlarmRule(this.alarmRuleForm).then(response => {
            if (response.code === 200) {
              this.$message.success('修改成功')
              this.$emit('watchChild')
              this.dialogFormVisible = false
            }
          })
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .el-select{
    width: 100%;
  }
  .el-date-editor{
    width: 100%;
  }
</style>
