<template>
  <div>
    <Modal v-model="showEditModal" :mask-closable="false" :width="500" class="zIndex-1200">
      <p slot="header">{{modalTitle}}</p>
      <div>
        <Form id="edit-form" ref="formObj" :model="formObj" :rules="ruleValidate" :label-width="100" inline>
          <Form-item label="库房号" prop="warehouse" style="width: 100%">
            <Input v-model="formObj.warehouse" name="warehouse" placeholder="请选择库房号" style="width: 95%;"/>
            <span @click="_storageModel()" style="font-size: 20px;color: #00b5ec">+</span>
          </Form-item>
          <Form-item label="架位号" prop="receiveLocation" style="width: 100%">
            <Input v-model="formObj.receiveLocation" name="receiveLocation" placeholder="请选择架位号"/>
          </Form-item>
          <Form-item label="留存有效期：" prop="keepLimitTime" style="width: 100%">
            <Date-picker v-model="formObj.keepLimitTime" @on-change="_ctimeChange" type="date" split-panels
                         placeholder="请选择留存有效期" name="keepLimitTime"></Date-picker>
          </Form-item>
        </Form>
      </div>
      <div slot="footer">
        <ModalFooter ref="footerModal" @on-result-change="_footerResult" :footer="footerList"></ModalFooter>
      </div>
    </Modal>
    <sampleKeep ref="storageModal" @on-result-change="_storageData"></sampleKeep>
  </div>
</template>
<script>
/**
 * 添加编辑
 */
import ModalFooter from '../../../../components/base/modalFooter'
import { soilAptitude, soilEntrust, soilSample } from '../../../../api'
import sampleKeep from '../sampleKeep'
export default {
  components: {
    ModalFooter,
    sampleKeep
  },
  data() {
    return {
      formId: '',
      modalTitle: '留存位置选择',
      formObj: {
        receiveLocation: undefined,
        keepLimitTime: undefined,
        locationId: undefined
      },
      ruleValidate: {},
      showEditModal: false,
      ids: '',
      options: [],
      footerList: [
        { id: '', name: '取消', type: '' },
        { id: '', name: '保存', type: 'primary' }
      ]
    }
  },
  methods: {
    /** *modal-footer */
    selectLocation(data) {
      this._getLocationById(data)
    },
    _footerResult(name) {
      switch (name) {
        case '取消':
          this._cancel()
          break
        case '保存':
          this._ok()
          break
      }
    },
    _storageModel() {
      this.$refs.storageModal._open()
    },
    _storageData(data) {
      this.$forceUpdate()
      this.formObj.locationId = data.id
      this.formObj.warehouse = data.warehouse
      this.formObj.receiveLocation = data.name
    },
    _hideLoading() {
      this.$refs.footerModal._hideLoading()
    },
    _resultChange(msg) {
      this.showEditModal = false
      this.$Message.success(msg)
      this.$emit('on-result-change')
      this._hideLoading()
    },
    _ctimeChange(data) {
      this.formObj.keepLimitTime = data
    },
    _ok() {
      this.$refs.formObj.validate(valid => {
        if (valid) {
          this.formObj.ids = this.ids
          this._receive(this.formObj)
        } else {
          this.$Message.error('表单验证失败!')
          this._hideLoading()
        }
      })
    },
    _receive: async function(data) {
      const result = await soilEntrust.sampleRetain(data)
      if (result) {
        this._resultChange('留存成功!')
      }
      this.$refs.footerModal._hideLoading()
    },
    _save: async function(data) {
      const result = await soilAptitude.standardSave(data)
      if (result) {
        this._resultChange('添加成功!')
      }
    },
    _cancel() {
      this._hideLoading()
      this.showEditModal = false
    },
    _open(ids) {
      this.formObj.receiveLocation = ''
      this.formObj.warehouse = ''
      this.formObj.keepLimitTime = undefined
      this.formObj.locationId = ''
      this._getLocation()
      this.ids = ids
      this.showEditModal = true
    },
    _getLocation: async function() {
      const result = await soilSample.locationList()
      if (result) {
        console.log(result)
        this.options = result
        console.log(this.options)
      }
    },
    _getLocationById: async function(id) {
      console.log(id)
      const result = await soilSample.locationGetById(id)
      if (result) {
        this.formObj.receiveLocation = result.name
      }
      console.log(this.formObj.receiveLocation)
    }
  }
}
</script>
