<template>
  <el-dialog append-to-body :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
    <el-form ref="dataForm" :rules="rules" :model="dataForm" label-width="140px" label-well-code="right">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item prop="id">
            <span slot="label">
              设备类型编号<el-tooltip v-if="dialogStatus === 'create'" content="编号不能超过100" placement="top"><i class="el-icon-warning-outline" /></el-tooltip>
            </span>
            <el-input
              v-model.trim="dataForm.id"
              maxlength="3"
              type="text"
              placeholder="请输入设备类型编号"
              clearable
              :disabled="dialogStatus === 'create' ? false : true"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="设备类型" prop="typeName">
            <el-input v-model="dataForm.typeName" clearable placeholder="请输入设备类型名称" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="描述" prop="typeDescription">
            <el-input
              v-model.trim="dataForm.typeDescription"
              type="text"
              placeholder="非必填"
              clearable
              :disabled="dialogStatus === 'update' || dialogStatus === 'create' ? false : true"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="监控内容" prop="watchType">
            <el-input v-model.trim="dataForm.watchType" type="text" clearable placeholder="请输入监控内容" :disabled="dialogStatus === 'update' || dialogStatus === 'create' ? false : true" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="是否启用" prop="valid">
            <el-select v-model.trim="dataForm.valid" placeholder="请选择是否启用" clearable>
              <el-option v-for="item in isValidList" :key="item.value" :label="item.label" :value="item.value" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="报警规则类型" prop="alarmRuleType">
            <el-select v-model.trim="dataForm.alarmRuleType" placeholder="请选择" clearable>
              <el-option v-for="item in alarmRuleTypeList" :key="item.value" :label="item.label" :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="alarmRuleHighvalue">
            <el-select v-model.trim="dataForm.alarmRuleHighvalue" placeholder="请选择" clearable>
              <el-option v-for="item in isValidList" :key="item.value" :label="item.label" :value="item.value" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="是否需要报警下限" prop="alarmRuleLowvalue">
            <el-select v-model.trim="dataForm.alarmRuleLowvalue" placeholder="请选择" clearable>
              <el-option v-for="item in isValidList" :key="item.value" :label="item.label" :value="item.value" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="数据展示项配置" prop="dataConfig">
            <el-input
              v-model.trim="dataForm.dataConfig"
              type="text"
              placeholder="非必填，非开发人员勿动，json串"
              clearable
              :disabled="dialogStatus === 'update' || dialogStatus === 'create' ? false : true"
            />
          </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 { updateDeviceType, addDeviceType } from '@/api/device/deviceType'
export default {
  name: 'EditDeviceType',
  data() {
    return {
      dialogFormVisible: false, // 对话框显示隐藏
      dialogStatus: '', // 对话框类型：create,update,detail
      // 对话框标题
      textMap: {
        update: '编辑',
        create: '新增',
        detail: '详情'
      },
      // 是否启用
      isValidList: [{ label: '是', value: '1' }, { label: '否', value: '0' }],
      alarmRuleTypeList: [{ label: '需要单独设置报警规则', value: '1' }, { label: '统一的报警规则', value: '2' }, { label: '无报警规则', value: '3' }],
      // 表单内容
      dataForm: {
        id: '', // id
        typeName: '', // 设备类型名称
        typeDescription: '', // 描述
        valid: '', // 是否启用
        watchType: '', // 监控内容
        alarmRuleType: '', // 报警规则类型
        alarmRuleHighvalue: '', // 是否需要报警上限
        alarmRuleLowvalue: '', // 是否需要报警下限
        dataConfig: '' // 数据项配置
      },
      // 表单校验规则
      rules: {
        id: [{ required: true, message: '设备编号不能为空', trigger: ['blur', 'change'] }],
        typeName: [{ required: true, message: '设备类型必选', trigger: ['blur', 'change'] }],
        valid: [{ required: true, message: '是否启用必选', trigger: ['blur', 'change'] }],
        watchType: [{ required: true, message: '监控内容不能为空', trigger: ['blur', 'change'] }],
        alarmRuleType: [{ required: true, message: '报警规则类型不能为空', trigger: ['blur', 'change'] }],
        alarmRuleHighvalue: [{ required: true, message: '是否需要报警上限必选', trigger: ['blur', 'change'] }],
        alarmRuleLowvalue: [{ required: true, message: '是否需要报警下限必选', trigger: ['blur', 'change'] }]
      }
    }
  },
  methods: {
    // 重置表单
    resetForm() {
      this.dataForm = {
        id: '', // id
        typeName: '', // 设备类型名称
        typeDescription: '', // 描述
        valid: '', // 是否启用
        watchType: '', // 监控内容
        alarmRuleType: '', // 报警规则类型
        alarmRuleHighvalue: '', // 是否需要报警上限
        alarmRuleLowvalue: '', // 是否需要报警下限
        dataConfig: '' // 数据项配置
      }
    },
    // 初始化对话框
    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.dialogStatus === 'detail') {
        this.dataForm = {
          id: row.id,
          typeName: row.typeName,
          typeDescription: row.typeDescription,
          valid: row.valid,
          watchType: row.watchType,
          alarmRuleType: row.alarmRuleType, // 报警规则类型
          alarmRuleHighvalue: row.alarmRuleHighvalue, // 是否需要报警上限
          alarmRuleLowvalue: row.alarmRuleLowvalue, // 是否需要报警下限
          dataConfig: row.dataConfig // 数据项配置
        }
      }
    },
    // 保存数据
    saveData() {
      if (this.dialogStatus === 'update') {
        this.updateData()
      } else if (this.dialogStatus === 'create') {
        this.createData()
      }
    },
    // 修改数据
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          updateDeviceType(this.dataForm).then(response => {
            if (response.code === 200) {
              this.$message.success('修改成功')
              this.$emit('watchChild')
              this.dialogFormVisible = false
            }
          })
        }
      })
    },
    // 新增数据
    createData: function() {
      this.$refs['dataForm'].validate((valid) => {
        console.log(this.dataForm)
        if (valid) {
          addDeviceType(this.dataForm).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-dialog{
    width:700px
  }
  .el-select{
    width: 100%;
  }
</style>

