<template>
  <div>
    <Modal v-model="showEditModal" v-drag :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="detail " style="width: 100%">
            <el-input v-model="formObj.detail " name="detail "></el-input>
          </Form-item>
        </Form>
      </div>
      <div slot="footer">
        <ModalFooter ref="footerModal" @on-result-change="_footerResult" :footer="footerList"></ModalFooter>
      </div>
    </Modal>
  </div>
</template>
<script>
/**
 * 添加编辑
 */
import ModalFooter from '../../../components/base/modalFooter'
import { soilAptitude, soilEntrust, soilSample } from '../../../api'

export default {
  components: {
    ModalFooter
  },
  data() {
    return {
      formId: '',
      modalTitle: '土质描述详情填写',
      formObj: {
        detail: ''
      },
      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
      }
    },
    _hideLoading() {
      this.$refs.footerModal._hideLoading()
    },
    _resultChange(msg) {
      this.showEditModal = false
      this.$Message.success(msg)
      this.$emit('on-result-change')
      this._hideLoading()
    },
    _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.weiteSoilDetail(data)
      if (result) {
        this._resultChange('填写成功!')
      }
    },
    _save: async function(data) {
      const result = await soilAptitude.standardSave(data)
      if (result) {
        this._resultChange('添加成功!')
      }
    },
    _edit: async function(data) {
      const result = await soilAptitude.standardEdit(data)
      if (result) {
        this._resultChange('编辑成功!')
      }
    },
    _cancel() {
      this._hideLoading()
      this.showEditModal = false
    },
    _open(ids) {
      this.formObj.detail = ''
      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)
    },
    _registerAdd() {
      this.formId = this.$randomCode()
      this._hideLoading()
      this.$refs.formObj.resetFields()
      this.modalTitle = '添加'
      this.formObj.id = ''
      this.formObj.type = 3
      this.showEditModal = true
    }
  }
}
</script>
