<template>
  <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" append-to-body width="700px">
    <el-form ref="dataForm" :rules="rules" :model="thresholdForm" label-position="right" label-width="180px">
      <el-row :gutter="20">
        <el-col :span="16" offset="4">
          <el-form-item label="开灯光照阈值" prop="turnOn">
            <el-input v-model.trim="thresholdForm.turnOn" :placeholder="dialogStatus=='detail'?'':'必填'" type="text" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="16" offset="4">
          <el-form-item label="关灯光照阈值" prop="turnOff">
            <el-input v-model.trim="thresholdForm.turnOff" :disabled="dialogStatus=='detail'" :placeholder="dialogStatus=='detail'?'':'必填'" type="text"/>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" style="width: 100px;font-size: 16px;" @click="saveData">保 存</el-button>
      <el-button style="width: 100px;font-size: 16px" @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { setThreshold } from '@/api/system/strategy'
import { getThreshold } from '@/api/system/strategy'
export default {
  name: 'EditThreshold',
  data() {
    return {
      deptShow: true,
      dialogFormVisible: false, // 对话框是否显示
      dialogStatus: '', // 对话框类型：create,update
      thresholdForm: {
        turnOn: '',
        turnOff: ''
      }, // 表单
      textMap: {
        update: '阈值设置'
      }, // 表头显示标题
      rules: {
        turnOn: [{ required: true, message: '开灯光照阈值不能为空', trigger: ['blur', 'change'] },
          { pattern: /^(([1-9]{1}\d*)|(0{1}))(\.\d{1,2})?$/, required: true, message: '请输入合法数字，最多两位小数', trigger: ['blur', 'change'] }],
        turnOff: [{ required: true, message: '关灯光照阈值不能为空', trigger: ['blur', 'change'] },
          { pattern: /^(([1-9]{1}\d*)|(0{1}))(\.\d{1,2})?$/, required: true, message: '请输入合法数字，最多两位小数', trigger: ['blur', 'change'] }]
      } // 前端校验规则
    }
  },
  methods: {
    // 初始化对话框
    initDialog: function(dialogStatus, dialogFormVisible, row = null) {
      this.dialogStatus = dialogStatus
      this.dialogFormVisible = dialogFormVisible
      if (dialogStatus === 'create') { // 如果是新增，清除验证
        this.resetForm()
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      } else if (dialogStatus === 'update' || dialogStatus === 'detail') { // 如果是修改，将row中数据填写到输入框中
        getThreshold().then(response => {
          if (response.code === 200) {
            this.thresholdForm.turnOff = response.data.turnOff
            this.thresholdForm.turnOn = response.data.turnOn
          }
        })
      }
    },
    // 重置表单
    resetForm() {
      this.thresholdForm = {
        turnOff: '',
        turnOn: ''
      }
    },
    // 保存数据
    saveData() {
      if (this.dialogStatus === 'update') {
        this.updateData()
      }
    },
    // 修改数据
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          console.log(this.thresholdForm)
          setThreshold(this.thresholdForm).then(response => {
            if (response.code === 200) {
              this.$message.success('修改成功')
              this.$emit('watchChild')
              this.dialogFormVisible = false
            }
          })
        }
      })
    },
    cancel() {
      this.dialogFormVisible = false
      this.$emit('watchChild')
    },
    changeiot(val) {
      this.thresholdForm.iot = val.iot
      this.thresholdForm.deviceId = val.id
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  $tableTitleHeight: 40px;
  .el-form-item {
    margin-bottom: 22px;
  }
  .el-select{
    width: 100%;
  }
  .table-title {
    background-color: rgba(243, 243, 243, 1);
    height: $tableTitleHeight;
    .title-header {
      line-height: $tableTitleHeight;
      color: #606266;
      font-size: 14px;
      font-weight: bold;
      i {
        margin-left: 5px;
        margin-right: 5px;
      }
    }
  }
</style>
