<template>
  <div>
    <Modal v-model="showEditModal" v-drag :mask-closable="false">
      <p slot="header">
        {{ modalTitle }}
      </p>
      <div>
        <Form id="edit-form" ref="formObj" :model="formObj" :rules="ruleValidate" :label-width="95">
          <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="浮游菌(cfu/m³)" prop="airborneMicrobe">
            <Input v-model="formObj.airborneMicrobe" name="airborneMicrobe" placeholder="请输入浮游菌" /></Input>
          </Form-item>
          <Form-item label="沉降菌(个/皿)" prop="settlingMicrobe">
            <Input v-model="formObj.settlingMicrobe" name="settlingMicrobe" placeholder="请输入沉降菌" /></Input>
          </Form-item>
          <Form-item label="监测日期" prop="monitorDate">
            <Date-picker
              v-model="formObj.monitorDate"
              name="monitorDate"
              type="date"
              placeholder="监测日期"
              format="yyyy-MM-dd"
              style="width: 100%;"
            />
          </Form-item>
        </Form>
      </div>
      <div slot="footer">
        <modal-footer ref="footerModal" :footer="footerList" @on-result-change="_footerResult" />
      </div>
    </Modal>
  </div>
</template>
<script>
/**
 * 添加编辑
 */
import { envMonitor } from '../../../api'

export default {
  data() {
    return {
      id: '',
      modalTitle: '添加',
      formObj: {
        type: '1',
        location: '',
        humidity: '',
        temperature: '',
        airborneMicrobe: '',
        settlingMicrobe: '',
        monitorDate: '',
        recorder: '',
        companyId: ''
      },
      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' }
        ],
        airborneMicrobe: [
          { required: true, message: '浮游菌不能为空', trigger: 'blur' }
        ],
        settlingMicrobe: [
          { required: true, message: '沉降菌不能为空', trigger: 'blur' }
        ]
      },
      showEditModal: false,
      footerList: [
        { id: '', name: '取消', type: '' },
        { id: '', name: '保存', type: 'primary' }
      ]
    }
  },
  methods: {
    // modal
    _footerResult(name) {
      switch (name) {
        case '取消':
          this._cancel()
          break
        case '保存':
          this._ok()
          break
      }
    },
    _hideLoading() {
      this.$refs.footerModal._hideLoading()
    },
    _resultChange(result, msg) {
      if (result) {
        this.showEditModal = false
        this.$Message.success(msg)
        this.$emit('on-result-change')
      }
      this._hideLoading()
    },
    _ok() {
      this.$refs.formObj.validate(valid => {
        if (valid) {
          const data = this.$serialize('edit-form')
          this._save(data)
        } else {
          this.$Message.error('表单验证失败')
          this._hideLoading()
        }
      })
    },
    // 添加、编辑保存
    _save: async function(data) {
      // 添加
      if (this.$string(this.id).isEmpty()) {
        const result = await envMonitor.add(data)
        this._resultChange(result, '添加成功')
      } else {
        // 编辑
        const result = await envMonitor.edit({
          id: this.formObj.id,
          obj: data
        })
        this._resultChange(result, '编辑成功')
      }
    },
    _cancel() {
      this.showEditModal = false
    },
    _open(formObj) {
      this.$refs.formObj.resetFields()
      this.showEditModal = true
      this._hideLoading()
      if (this.$string(formObj).isEmpty()) {
        this.id = ''
        this.modalTitle = '添加微生物环境检测'
      } else {
        this.id = formObj.id
        this.formObj = formObj
        if (this.formObj.monitorDate === undefined) {
          this.formObj.monitorDate = ''
        } else {
          this.formObj.monitorDate = new Date(this.formObj.monitorDate)
        }
        this.modalTitle = '编辑微生物环境检测'
      }
    }
  }
}
</script>
