<template>
  <div>
    <Modal v-model="showEditModal" :mask-closable="false" v-drag>
      <p slot="header">{{modalTitle}}</p>
      <div>
        <Form ref="formObj" :id="formId" :model="formObj" :rules="ruleValidate" :label-width="100">
          <Form-item label="科室：" prop="groupName" class="width-48">
            <el-select @change="_selGroup" v-model="formObj.groupName" placeholder="请选择" name="groupName" style="width: 100%">
              <el-option
                v-for="(item,index) in groupoptions"
                :key="index"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </Form-item>
          <Form-item label="预警项目" prop="warningType">
            <Input v-model="formObj.warningType" name="warningType" placeholder="请输入预警项目"></Input>
          </Form-item>
          <Form-item label="预警上限" prop="upperLimit">
            <Input v-model="formObj.upperLimit" name="upperLimit" placeholder="请输入预警上限"></Input>
          </Form-item>
          <Form-item label="预警下限" prop="lowerLimit">
            <Input v-model="formObj.lowerLimit" name="lowerLimit" placeholder="请输入预警下限"></Input>
          </Form-item>
          <Form-item label="手机号" prop="tel">
            <Input v-model="formObj.tel" name="tel" placeholder="请输入手机号"></Input>
          </Form-item>
          <Form-item label="邮箱" prop="email">
            <Input v-model="formObj.email" name="email" placeholder="请输入邮箱"></Input>
          </Form-item>
          <Form-item label="备注" prop="remark">
            <Input v-model="formObj.remark" name="remark" placeholder="请输入备注"></Input>
          </Form-item>
        </Form>
      </div>
      <div slot="footer">
        <modal-footer ref="footerModal" @on-result-change="_footerResult" :footer="footerList"></modal-footer>
      </div>
    </Modal>
  </div>
</template>
<script>
import { soilAptitude } from '../../../api'

/**
 * 添加编辑常规环境检测
 */
export default {
  components: {},
  data() {
    return {
      id: '',
      modalTitle: '',
      formObj: {
        groupName: '',
        groupId: ''
      },
      locList: [],
      groupoptions: [],
      ruleValidate: {
        type: [
          {
            required: true,
            message: '类型不能为空',
            trigger: 'blur',
            type: 'number'
          }
        ],
        location: [
          { required: true, message: '位置不能为空', trigger: 'blur' }
        ],
        humidity: [
          { required: true, message: '相对湿度不能为空', trigger: 'blur' }
        ],
        temperature: [
          { required: true, message: '温度不能为空', trigger: 'blur' }
        ]
      },
      showEditModal: false,
      dateList: [],
      format: '',
      monitorFormat: '',
      formId: '',
      footerList: [
        { id: '', name: '取消', type: '' },
        { id: '', name: '保存', type: 'primary' }
      ]
    }
  },
  methods: {
    _getGroupList: async function() {
      const result = await soilAptitude.getGroupList()
      if (result) {
        this.groupoptions = result
      }
    },
    _selGroup(data) {
      this.formObj.groupId = data
    },
    /** *modal-footer */
    _footerResult(name) {
      switch (name) {
        case '取消':
          this._cancel()
          break
        case '保存':
          this._ok()
          break
      }
    },
    _hideLoading() {
      this.$refs.footerModal._hideLoading()
    },
    // 字典
    _ok() {
      this.$refs.formObj.validate(valid => {
        if (valid) {
          const data = this.$serialize(this.formId)
          if (this.$string(this.id).isEmpty()) {
            // 添加
            this._save(data)
          } else {
            // 编辑
            this._edit({ id: this.formObj.id, obj: data })
          }
        } else {
          this.$Message.error('表单验证失败!')
          this._hideLoading()
        }
      })
    },
    _save: async function(data) {
      const result = await soilAptitude.addEnvMonitor(data)
      if (result) {
        this.$Message.success('添加成功')
        this.showEditModal = false
        this.$emit('on-result-change')
      }
    },
    _edit: async function(data) {
      const result = await soilAptitude.editEnvMonitor(data)
      if (result) {
        this.$Message.success('编辑成功')
        this.showEditModal = false
        this.$emit('on-result-change')
      }
    },
    _cancel() {
      this.showEditModal = false
    },
    _open(formObj) {
      this.formId = 'edit-form-normal-' + this.$randomCode()
      this.showEditModal = true
      this.$refs.formObj.resetFields()
      this._hideLoading()
      this._getGroupList()
      if (this.$string(formObj).isEmpty()) {
        this.id = ''
        this.modalTitle = '添加'
      } else {
        this.id = formObj.id
        this.formObj = formObj
        this.modalTitle = '编辑'
      }
    }
  }
}
</script>
