<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="location">
            <Input v-model="formObj.location" name="location" placeholder="请输入位置"></Input>
          </Form-item>
          <Form-item label="相对湿度(%)" prop="humidity">
            <Input v-model="formObj.humidity" name="humidity" placeholder="请输入相对湿度"></Input>
          </Form-item>
          <Form-item label="温度(℃)" prop="temperature">
            <Input v-model="formObj.temperature" name="temperature" placeholder="请输入温度"></Input>
          </Form-item>
          <Form-item label="设备编号" prop="equipNum">
            <Input v-model="formObj.equipNum" name="equipNum" placeholder="请输入设备编号"></Input>
          </Form-item>
          <Form-item label="监测日期" prop="monitorDate">
            <Date-picker :format="format" v-model="formObj.monitorDate" name="monitorDate" type="datetime"
                         placeholder="监测日期" style="width: 100%;"></Date-picker>
          </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: {
        location: '',
        humidity: '',
        temperature: '',
        monitorDate: '',
        recorder: '',
        companyId: '',
        equipNum: ''
      },
      locList: [],
      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: {
    /** *modal-footer */
    _footerResult(name) {
      switch (name) {
        case '取消':
          this._cancel()
          break
        case '保存':
          this._ok()
          break
      }
    },
    _hideLoading() {
      this.$refs.footerModal._hideLoading()
    },
    _resultChange(msg) {
      this.showEditModal = false
      this.$Message.success(msg)
      this.$emit('on-result-change')
    },
    _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.envMonitorAdd(data)
      if (result) {
        this.$Message.success('添加成功')
        this.showEditModal = false
        this.$emit('on-result-change')
      }
    },
    _edit: async function(data) {
      const result = await soilAptitude.envMonitorEdit(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()
      if (this.$string(formObj).isEmpty()) {
        this.id = ''
        this.modalTitle = '添加常规环境检测'
      } else {
        this.id = formObj.id
        this.formObj = formObj
        this.modalTitle = '编辑常规环境检测'
        if (formObj.monitorDate === undefined) {
          this.formObj.monitorDate = ''
        } else {
          this.formObj.monitorDate = new Date(formObj.monitorDate)
          $('input[name="monitorDate"]').val(
            this.$dateformat(formObj.monitorDate, 'yyyy-mm-dd')
          )
        }
      }
    }
  }
}
</script>
