<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="120px">
      <el-row v-if="dialogStatus=='update'" :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 v-if="dialogStatus=='create'" :gutter="20">
        <el-col :span="12">
          <el-form-item label="设备编号" prop="devcode">
            <el-select
              v-model="alarmRuleForm.devcode"
              :loading="loading"
              :disabled="dialogStatus=='update'?true:false"
              filterable
              placeholder="请输入设备编号"
            >
              <el-option
                v-for="item in devices"
                :key="item.id"
                :label="item.devcode"
                :value="item.devcode"/>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="设备类型">
            <el-input v-model="baseInfo.deviceTypeName" type="text" placeholder="未知" disabled/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row v-else :gutter="20">
        <el-col :span="12">
          <el-form-item label="设备编号">
            <el-input v-model="baseInfo.devcode" type="text" placeholder="未知" disabled/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="设备类型">
            <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="告警上限" 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 { addAlarmRule, updateAlarmRule } from '@/api/alarmRule'
import { getDeviceSimpleList } 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()
        }
      }
    }
    return {
      dialogFormVisible: false, // 对话框是否显示
      dialogStatus: '', // 对话框类型：create,update
      baseInfo: {
        wellCode: '', // 窨井编号
        wellName: '', // 窨井名称
        deep: '', // 井深
        deviceType: '', // 设备类别
        deviceTypeName: '', // 设备类别
        devcode: ''// 设备编号
      },
      alarmRuleForm: {
        id: '', // 告警规则id
        devcode: '', // 设备编号
        deviceType: '', // 设备类型
        highValue: '', // 告警上限
        lowValue: '0'// 告警下限
      }, // 表单
      textMap: {
        update: '编辑',
        create: '新增'
      }, // 表头显示标题
      devices: [], // 待选设备编号列表
      loading: true,
      // unit: '', // 单位
      rules: {
        devcode: [{ required: true, message: '请选择设备', trigger: ['blur', 'change'] }],
        highValue: [{ required: true, trigger: ['blur', 'change'], validator: validateHighValue }],
        lowValue: [{ required: true, trigger: ['blur', 'change'], validator: validateLowValue }]
      } // 前端校验规则
    }
  },
  computed: {
    showHighValue() {
      // if (this.baseInfo.deviceType === '2' || this.baseInfo.deviceType === '6') {
      //   return true
      // } else {
      //   return false
      // }
      return true
    },
    showLowValue() {
      // if (this.baseInfo.deviceType === '2' || this.baseInfo.deviceType === '6') {
      //   return false
      // } else {
      //   return true
      // }
      return false
    },
    unit() {
      if (this.alarmRuleForm.deviceType === '2') {
        return 'm'
      } else if (this.alarmRuleForm.deviceType === '6') {
        return 'mg'
      }
    }
  },
  watch: {
    'alarmRuleForm.devcode'(newValue, oldValue) {
      for (const device of this.devices) {
        if (device.devcode === newValue) {
          debugger
          this.baseInfo.deviceTypeName = device.deviceTypeName
          this.baseInfo.deviceType = device.deviceType
          this.alarmRuleForm.deviceType = device.deviceType
          // if (device.deviceType === '2') {
          //   this.unit = 'm'
          // } else if (device.deviceType === '6') {
          //   this.unit = 'mg'
          // }
          return
        }
      }
    },
    'baseInfo.devcode'(newValue, oldValue) {
      this.alarmRuleForm.devcode = newValue
    }
  },
  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
        }
      } else { // 如果是新增
        this.resetForm()
        this.fetchDevice()
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      }
    },
    // 获取可设置告警规则设备列表
    fetchDevice() {
      this.loading = true
      getDeviceSimpleList().then(response => {
        if (response.code === 200) {
          this.loading = false
          this.devices = response.data
        }
      })
    },
    // 重置表单
    resetForm() {
      this.baseInfo = {
        wellCode: '', // 窨井编号
        wellName: '', // 窨井名称
        deep: '', // 井深
        deviceType: '', // 设备类别
        deviceTypeName: '', // 设备类别
        devcode: ''// 设备编号
      }
      this.alarmRuleForm = {
        id: '', // 告警规则id
        devcode: '', // 设备编号
        deviceType: '', // 设备类型
        highValue: '', // 告警上限
        lowValue: '0'// 告警下限
      } // 表单
    },
    // 保存数据
    saveData: function() {
      if (this.dialogStatus === 'create') {
        this.addData()
      } else {
        this.updateData()
      }
    },
    // 新增数据
    addData: function() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          debugger
          const alarmRuleForm = {
            devcode: this.alarmRuleForm.devcode, // 设备编号
            deviceType: this.alarmRuleForm.deviceType, // 设备类型
            highValue: this.alarmRuleForm.highValue, // 告警上限
            lowValue: this.alarmRuleForm.lowValue// 告警下限
          }
          addAlarmRule(alarmRuleForm).then(response => {
            if (response.code === 200) {
              this.$message.success('新增成功')
              this.$emit('watchChild')
              this.dialogFormVisible = false
            }
          })
        }
      })
    },
    // 修改数据
    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>
