<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    width="600px">
    <el-form 
      :model="dataForm" 
      :rules="dataRule" 
      ref="dataForm" 
      @keyup.enter.native="dataFormSubmit()" 
      label-width="80px" 
      size="mini"
      style="margin-left: 20px;width: 500px">
      <el-form-item label="所属范围" prop="belongTo">
        <!-- <el-input v-model.number="dataForm.thresholdType" placeholder="请输入数字"></el-input> -->
        <el-select v-model="dataForm.belongTo" style="width: 100%">
          <el-option
            v-for="item in belongTo"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="参数名称" prop="thresholdType">
        <el-input v-model="dataForm.thresholdType"></el-input>
      </el-form-item>
      <el-form-item label="参数阈值" prop="thresholdValue">
        <el-input v-model="dataForm.thresholdValue"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="mini" @click="visible = false">关闭</el-button>
      <el-button size="mini" type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    props: ["belongTo"],
    data () {
      return {
        visible: false,
        dataForm: {
          id: '',
          belongTo: '',
          thresholdType: '',
          thresholdValue: '',
          // farmId: ''
        },
        // belongTo: [
        //   {
        //     label: '室内',
        //     value: 1
        //   },
        //   {
        //     label: '室外',
        //     value: 2
        //   },
        //   {
        //     label: '耳标数据',
        //     value: 3
        //   },
        //   {
        //     label: '饮用水',
        //     value: 4
        //   }
        // ],
        dataRule: {
          belongTo: [
            { required: true, message: '参数范围不能为空', trigger: 'blur' }
          ],
          thresholdType: [
            { required: true, message: '参数名称不能为空', trigger: 'blur' }
          ],
          thresholdValue: [
            { required: true, message: '参数阈值不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      // 在ealarm.vue调用该方法
      init (id) {
        // this.farmId = this.$cookie.get('farmId') === 'null'? undefined: parseInt(this.$cookie.get('farmId'))
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          // this.dataForm.farmId = (this.dataForm.farmId || this.farmId) || undefined
        })
        this.dataForm.id = id || undefined
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/management/thresholdManagement/info/${this.dataForm.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0) {
              // console.log(data)
              this.dataForm.belongTo = data.thresholdManagement.belongTo
              this.dataForm.thresholdType = data.thresholdManagement.thresholdType
              this.dataForm.thresholdValue = data.thresholdManagement.thresholdValue
              // this.dataForm.farmId = data.thresholdManagement.farmId
            }
          })
        }
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            // let belong = {
            //   '1': 1,
            //   '2': 1,
            //   '3': 1,
            //   '4': 4,
            //   '5': 2,
            //   '6': 2,
            //   '7': 2,
            //   '8': 3,
            //   '9': 3,
            //   '10': 3
            // }
            this.$http({
              url: this.$http.adornUrl(`/management/thresholdManagement/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'thresholdType': this.dataForm.thresholdType,
                'thresholdValue': this.dataForm.thresholdValue,
                'farmId': parseInt(this.$cookie.get('formFarmId')),
                'belongTo': this.dataForm.belongTo
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.visible = false
                this.$emit('refreshDataList')
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
