<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="lowValue">
            <el-input
              v-model="deviceForm.lowValue"
              placeholder="请输入报警下限"
              clearable
              :disabled="dialogStatus === 'detail' ? true : false"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="报警上限" prop="highValue">
            <el-input
              v-model="deviceForm.highValue"
              placeholder="请输入报警上限"
              clearable
              :disabled="dialogStatus === 'detail' ? true : false"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="操作人姓名" prop="openAtor">
            <el-input
              v-model="deviceForm.openAtor"
              placeholder="请输入操作人姓名"
              clearable
              :disabled="dialogStatus === 'detail' ? true : false"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="超时时间" prop="overTime">
            <el-date-picker
              v-model="deviceForm.overTime"
              type="datetime"
              value-format="yyyy-MM-dd HH:mm:ss"
              clearable
              placeholder="请选择超时时间"
              :disabled="dialogStatus === 'detail' ? true : false"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="突然阈值" prop="saltaTion">
            <el-input
              v-model="deviceForm.saltaTion"
              clearable
              placeholder="请输入突然阈值"
              :disabled="dialogStatus === 'detail' ? true : false"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="时间戳" prop="ts">
            <el-date-picker
              v-model="deviceForm.ts"
              type="datetime"
              value-format="yyyy-MM-dd HH:mm:ss"
              clearable
              placeholder="请选择时间戳"
              :disabled="dialogStatus === 'detail' ? true : false"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="报警等级" prop="grade">
            <el-select
              v-model="deviceForm.grade"
              placeholder="请选择报警等级"
              clearable
              :disabled="dialogStatus === 'detail' ? true : false"
            >
              <el-option
                v-for="item in gradeList"
                :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="sensorCode">
            <el-input
              v-model="deviceForm.sensorCode"
              clearable
              placeholder="请输入传感器编号"
              :disabled="dialogStatus === 'create' ? false : true"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row v-show="dialogStatus === 'detail' ? true : false" :gutter="20">
        <el-col :span="12">
          <el-form-item label="设备编号" prop="devCode">
            <el-input
              v-model="deviceForm.devCode"
              clearable
              disabled
              placeholder="请输入设备编号"
            />
          </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 { updateWarningRule, addWarningRule } from '@/api/warning/warningRule'
export default {
  name: 'DialogListWarning',
  data() {
    return {
      dialogFormVisible: false, // 对话框显示隐藏
      dialogStatus: '', // 对话框类型：create,update,detail
      // 对话框标题
      textMap: {
        update: '编辑',
        create: '新增',
        detail: '详情'
      },
      // 报警等级
      gradeList: [
        {
          name: '一级报警',
          value: '1'
        },
        {
          name: '二级报警',
          value: '2'
        }
      ],
      // 表单内容
      deviceForm: {
        highValue: '',
        lowValue: '',
        overTime: '',
        saltaTion: '',
        sensorCode: '',
        grade: '',
        ts: '',
        openAtor: '',
        devCode: ''
      },
      // 表单校验规则
      rules: {
        highValue: [{ required: true, message: '报警上限不能为空', trigger: ['blur', 'change'] }],
        lowValue: [{ required: true, message: '报警下限不能为空', trigger: ['blur', 'change'] }],
        overTime: [{ required: true, message: '超时时间必选', trigger: ['blur', 'change'] }],
        saltaTion: [{ required: true, message: '突然阈值不能为空', trigger: ['blur', 'change'] }],
        sensorCode: [{ required: true, message: '传感器编号不能为空', trigger: ['blur', 'change'] }],
        grade: [{ required: true, message: '报警等级必选', trigger: ['blur', 'change'] }],
        ts: [{ required: true, message: '时间戳必选', trigger: ['blur', 'change'] }],
        openAtor: [{ required: true, message: '操作人姓名不能为空', trigger: ['blur', 'change'] }]
      }
    }
  },
  methods: {
    // 重置表单
    resetForm() {
      this.deviceForm = {
        highValue: '',
        lowValue: '',
        overTime: '',
        saltaTion: '',
        sensorCode: '',
        grade: '',
        ts: '',
        openAtor: '',
        devCode: ''
      }
    },
    // 父子组件通信
    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.deviceForm = {
          highValue: row.highValue,
          lowValue: row.lowValue,
          overTime: row.overTime,
          saltaTion: row.saltaTion,
          sensorCode: row.sensorCode,
          grade: row.grade,
          ts: row.ts,
          openAtor: row.openAtor,
          devCode: row.devCode
        }
      }
    },
    // 保存数据
    saveData() {
      if (this.dialogStatus === 'update') {
        this.updateData()
      } else if (this.dialogStatus === 'create') {
        this.createData()
      }
    },
    // 修改数据
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          updateWarningRule(this.deviceForm).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.deviceForm)
        if (valid) {
          addWarningRule(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>
  .el-select__tags {
    flex-wrap: inherit !important;
    overflow-x: auto !important;
  }
  ::-webkit-scrollbar {
    height: 4px;
  }
  /* //滚动条的滑块 */
  ::-webkit-scrollbar-thumb {
    background-color: #eaecf1;
    border-radius: 3px;
  }
</style>
