<template>
  <div>
    <Modal v-model="showDiagramModal" title="原始记录填写" width="1300" class="footer-hidden">
      <div id="iframeOne" style="text-align:center;align-content:center;width: 100%;height: 790px">
      </div>
      <div slot="footer">
      </div>
    </Modal>
  </div>
</template>
<script>
import global from '../../../../../api/config'
import { drugItem } from '../../../../../api'

/**
 * 原始记录填写
 */
export default {
  data() {
    return {
      showDiagramModal: false,
      templateId: '',
      again: false,
      itemIds: []
    }
  },
  mounted() {
    // 监听原始记录消息
    window.addEventListener('message', this._saveOriginal)
  },
  methods: {
    async _saveOriginal(data) {
      console.log('data11111', data)
      if (this.again) {
        this.again = false
        if (undefined !== data.data.msg && data.data.msg === true) {
          this._cancel()
        } else if (
          undefined === data.data.templateId &&
          data.data.msg !== undefined
        ) {
          const result = await drugItem.originalRecordsAdd({
            ids: this.itemIds,
            formId: data.data.msg
          })
          if (result) {
            this._cancel()
          }
        } else {
          // this.$store
          //   .dispatch('FoodItem/saveModelForTestBasis', {
          //     ids: this.itemIds,
          //     templateId: data.data.templateId
          //   })
          //   .then(() => {
          //     this._cancel()
          //   })
        }
      }
    },
    _cancel() {
      this.showDiagramModal = false
      this.$emit('on-result-change')
    },
    _open(itemIds, modelId, dataSource) {
      console.log(dataSource)
      const num =
        undefined === dataSource[0].sampleCode ? '' : dataSource[0].sampleCode
      const name =
        undefined === dataSource[0].sampleName ? '' : dataSource[0].sampleName
      const testBasis =
        undefined === dataSource[0].testBasis ? '' : dataSource[0].testBasis
      let itemNames = ''
      const length = dataSource.length

      let itemUrl = ''

      for (let i = 0; i < length; i++) {
        if (undefined !== dataSource[i].name && dataSource[i].name !== '') {
          itemNames += dataSource[i].name + '、'
          itemUrl += '&item' + (i + 1) + '=' + dataSource[i].name
        }
      }
      console.log('itemUrl', itemUrl)
      if (itemNames.length > 0) {
        itemNames = itemNames.substring(0, itemNames.length - 1)
      }
      this.again = true
      this.itemIds = itemIds
      console.log('this.itemIds', this.itemIds)
      this.showDiagramModal = true
      let baseUrl = global.baseURL
      baseUrl +=
        '/llgt/v1/item/original_record_data_bind?ids=' +
        this.itemIds +
        '&modelId=' +
        modelId
      const url =
        'http://record.patzn.com:7000/print/v1/eln/template_' +
        modelId +
        '?num=' +
        num +
        '&itemNames=' +
        itemNames +
        '&name=' +
        name +
        '&testBasis=' +
        encodeURIComponent(testBasis) +
        '&bindUri=' +
        encodeURIComponent(baseUrl)
      $('#iframeOne').html(
        '<iframe style="padding: 0px;width:100%;height:100%" frameborder="0" src=' +
          url +
          '></iframe>'
      )
    }
  }
}
</script>
