<template>
  <div>
    <!--<Modal v-model="showModal" title="原始记录" :mask-closable="false" width="1300" class="modal-footer-none">-->
    <!--<div style="text-align:center;align-content:center;width: 100%;height: 790px" v-html="htmlContent">-->
    <!--</div>-->
    <!--</Modal>-->
  </div>
</template>
<script>
import Global from '../../../api/config'
import { soilTest } from '../../../api'

/**
 * 编辑原始记录详情
 */
export default {
  data() {
    return {
      // showModal: false,
      again: false,
      formId: '',
      htmlContent: '',
      formIdTemp: ''
    }
  },
  created() {
    // 监听原始记录消息
    // eslint-disable-next-line nuxt/no-globals-in-created
    window.addEventListener('message', this._saveOriginal)
  },
  // 销毁监听事件
  beforeDestroy() {
    this.again = false
    window.removeEventListener('message', this._saveOriginal)
  },
  methods: {
    _open(formId) {
      this.formId = formId
      this.formIdTemp = formId + this.$randomCode()
      this.again = true
      // this.showModal = true;

      // 编辑的时候传此bindUri 是为了 绑定spreadJs自定义的公式
      const bindUri =
        Global.baseURL + '/env/v1/env_item/original_record_data_bind?source='
      let url = ''
      if (process.env.NODE_ENV === 'production') {
        url =
          'http://record.patzn.com/print/v1/eln/form_soil_' +
          formId +
          '?bindUri=' +
          encodeURIComponent(bindUri)
      } else {
        url =
          Global.recordURL +
          '/print/v1/eln/form_soil_' +
          formId +
          '?bindUri=' +
          encodeURIComponent(bindUri)
      }
      // this.htmlContent = '<iframe style="padding: 0px;width:100%;height:100%" frameborder="0" src=' + encodeURI(url) + '></iframe>';
      this.$layx(this.formIdTemp, '编辑原始记录', url)
    },
    _openWithType(formId, fromType) {
      this.formId = formId
      this.formIdTemp = formId + this.$randomCode()
      this.again = true
      // this.showModal = true;

      // 编辑的时候传此bindUri 是为了 绑定spreadJs自定义的公式
      const bindUri =
        Global.baseURL + '/env/v1/env_item/original_record_data_bind?source='
      let url = ''
      if (process.env.NODE_ENV === 'production') {
        url =
          'http://record.patzn.com/print/v1/eln/form_soil_' +
          formId +
          '?bindUri=' +
          encodeURIComponent(bindUri) +
          '&type=' +
          fromType
      } else {
        url =
          Global.recordURL +
          '/print/v1/eln/form_soil_' +
          formId +
          '?bindUri=' +
          encodeURIComponent(bindUri) +
          '&type=' +
          fromType
      }
      // this.htmlContent = '<iframe style="padding: 0px;width:100%;height:100%" frameborder="0" src=' + encodeURI(url) + '></iframe>';
      this.$layx(this.formIdTemp, '编辑原始记录', url)
    },
    _saveOriginal(data) {
      console.log('修改的数据', data)
      if (this.again) {
        if (data.data.msg === true) {
          this._editSaveRecord(data)
        } else {
          this._cancel()
        }
        this.again = false
      }
    },
    _cancel() {
      // this.showModal = false;
      // 关闭所有layx弹框
      // eslint-disable-next-line no-undef
      layx.destroyAll(this.formIdTemp)
    },
    _updateRecord: async function(param) {
      const result = await soilTest.updateItem(param)
      console.log(result)
      if (result) {
        this.$Message.success('保存成功')
        this._cancel()
        this.$emit('on-result-change')
      }
    },
    // 保存后解析项目的检测值
    _editSaveRecord(data) {
      const param = {
        formId: this.formId
      }
      console.log('data', data)
      const copyMapTemp = {}
      if (data.data.copySheet && data.data.copyedSheet) {
        param.copyMap = {}
        const copyedKey = String(data.data.copyedSheet)
        copyMapTemp[copyedKey] = ''
        copyMapTemp[copyedKey] = String(data.data.copySheet)
        // 存在复制sheet的情况
        param.copyMap = JSON.stringify(copyMapTemp)
      } else if (typeof data.data.testValueArry !== 'undefined') {
        const testValue = data.data.testValueArry
        param.copyMap = {}
        testValue.forEach(item => {
          const copyedKey = String(item.copyedSheet)
          copyMapTemp[copyedKey] = ''
          copyMapTemp[copyedKey] = String(item.copySheet)
          param.copyMap = JSON.stringify(copyMapTemp)
        })
      }
      this._updateRecord(param)
      // this.$store.dispatch('EnvItem/updateFormForItem', param).then(() => {
      //   if (this.$store.state.EnvItem.success) {
      //     this.$Message.success('保存成功')
      //     // this.showModal = false;
      //     this.$emit('on-result-change')
      //     this._cancel()
      //   }
      // })
    }
  }
}
</script>
