<template>
  <el-dialog
    append-to-body
    :title="textMap[dialogStatus]"
    :visible.sync="dialogFormVisible"
  >
    <el-form
      ref="dataForm"
      :rules="rules"
      :model="deviceForm"
      label-width="100px"
      label-well-code="right"
    >
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="设备类型" prop="deviceType">
            <el-select
              v-model="deviceForm.deviceType"
              placeholder="请选择设备类型"
              clearable
            >
              <el-option
                v-for="item in deviceTypeList"
                :key="item.value"
                :label="item.name"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="监测指标" prop="name">
            <el-input
              v-model="deviceForm.name"
              clearable
              placeholder="请输入监测指标名称"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="开启报警" prop="openAlarm">
            <el-select
              v-model="deviceForm.openAlarm"
              placeholder="请选择是否开启报警"
              clearable
            >
              <el-option
                v-for="item in openAlarmList"
                :key="item.value"
                :label="item.name"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="开启工单" prop="openJob">
            <el-select
              v-model="deviceForm.openJob"
              placeholder="请选择是否开启工单"
              clearable
            >
              <el-option
                v-for="item in openJobList"
                :key="item.value"
                :label="item.name"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="告警上限" prop="highValue">
            <el-input
              v-model="deviceForm.highValue"
              placeholder="请输入告警上限"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="告警下限" prop="lowValue">
            <el-input
              v-model="deviceForm.lowValue"
              placeholder="请输入告警下限"
              clearable
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="报警等级" prop="level">
            <el-select
              v-model="deviceForm.level"
              placeholder="请选择报警等级"
              clearable
            >
              <el-option
                v-for="item in levelList"
                :key="item.value"
                :label="item.name"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div
      v-show="dialogStatus === 'detail' ? false : true"
      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 { getDevice, addAlarmLevel } from '@/api/systemConfig/alarmLevel'
export default {
  name: 'EditAlarmLevel',
  data() {
    return {
      dialogFormVisible: false, // 对话框显示隐藏
      deviceTypeList: '', // 设备类型
      dialogStatus: '', // 对话框类型：create,update,detail
      // 对话框标题
      textMap: {
        update: '编辑',
        create: '新增',
        detail: '详情'
      },
      // 是否开启报警
      openAlarmList: [
        {
          name: '是',
          value: '1'
        },
        {
          name: '否',
          value: '0'
        }
      ],
      // 是否开启工单
      openJobList: [
        {
          name: '是',
          value: '1'
        },
        {
          name: '否',
          value: '0'
        }
      ],
      // 报警等级
      levelList: [
        {
          name: '一级报警',
          value: '1'
        },
        {
          name: '二级报警',
          value: '2'
        },
        {
          name: '三级报警',
          value: '3'
        }
      ],
      // 表单内容
      deviceForm: {
        deviceType: '',
        name: '',
        openAlarm: '',
        openJob: '',
        level: '',
        lowValue: '',
        highValue: ''
      },
      // 表单校验规则
      rules: {
        deviceType: [{ required: true, message: '支持设备类型必选', trigger: ['blur', 'change'] }],
        name: [{ required: true, message: '监测指标名称不能为空', trigger: ['blur', 'change'] }],
        openAlarm: [{ required: true, message: '是否开启报警必选', trigger: ['blur', 'change'] }],
        openJob: [{ required: true, message: '是否开启工单必选', trigger: ['blur', 'change'] }],
        level: [{ required: true, message: '报警等级必选', trigger: ['blur', 'change'] }],
        lowValue: [{ required: true, message: '告警下限不能为空', trigger: ['blur', 'change'] }],
        highValue: [{ required: true, message: '告警上限不能为空', trigger: ['blur', 'change'] }]
      }
    }
  },
  created() {
    this.fetchDeviceType()
  },
  methods: {
    // 获取设备类型
    fetchDeviceType() {
      getDevice().then(response => {
        this.deviceTypeList = response.data
      })
    },
    // 重置表单
    resetForm() {
      this.deviceForm = {
        deptid: '',
        wellType: '',
        deviceType: '',
        lng: '',
        lat: ''
      }
    },
    // 父子组件通信
    initDialog(dialogStatus, row = null) {
      this.dialogFormVisible = true
      this.dialogStatus = dialogStatus
      if (this.dialogStatus === 'create') {
        this.resetForm()
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      } else if (this.dialogStatus === 'update') {
        this.deviceForm = {
          deptid: row.deptid,
          wellType: row.wellType,
          deviceType: row.deviceType,
          lng: row.lng,
          lat: row.lat
        }
      }
    },
    // 保存数据
    saveData() {
      if (this.dialogStatus === 'create') {
        this.createData()
      }
    },
    // 新增数据
    createData: function() {
      this.$refs['dataForm'].validate((valid) => {
        console.log(this.deviceForm)
        if (valid) {
          addAlarmLevel(this.deviceForm).then(response => {
            if (response.code === 200) {
              this.$confirm('新增成功，是否继续新增?', '提示', {
                confirmButtonText: '是',
                cancelButtonText: '否',
                type: 'info'
              }).then(() => {
                this.resetForm()
                this.$nextTick(() => {
                  this.$refs['dataForm'].clearValidate()
                })
              }).catch(() => {
                this.$emit('watchChild') // 刷新父组件数据列表
                this.dialogFormVisible = false
              })
            }
          })
        }
      })
    }
  }
}
</script>

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