<template>
  <div
    data-key="IFRAME_CONTENT"
    class="test"
    :style="styleClass"
  >
    <!-- <component :is="html" /> -->
    <iframe
      id="iframe"
      name="top"
      :src="html || url"
      frameborder="0"
    />
  </div>
</template>
<script>
  import Vue from 'vue';
  import axios from 'axios';
  import customizedPageConfig from '../config/customized.page.config.js';
  import customizedModalConfig from '../config/customized.modal.config.js';

  export default {
    props: {
      objTabActionDialogConfig: {
      },
      url: {
        type: String,
        default: '',
      },
      externalOptions: {
        type: Object,
        default: ()=>{}
      },
    },
    data() {
      return {
        html: '',
        styleClass: {
          width: '100%',
          height: '100%'
        }
      };
    },
    methods: {
      // // 取session
      // _sessionGet(key) {
      //   let data = sessionStorage[key];
      //   if (!data || data === 'null') {
      //     return null;
      //   }
      //   return JSON.parse(data).value;
      // },
      // // 存储session
      // _sessionSet(key, value) {
      //   let data = {
      //     value
      //   };
      //   sessionStorage[key] = JSON.stringify(data);
      // },
      // // 删除session
      // _sessionRemove(key) {
      //   sessionStorage.removeItem(key);
      // },
      // // 清除全部
      // _sessionClear() {
      //   sessionStorage.clear();
      // },
      getR3Data(type) {
        let keepAliveModuleName = window.R3.store.state.global.activeTab.keepAliveModuleName;
        if (keepAliveModuleName.startsWith('S.')) {
          let currentTab = this.$_live_getChildComponent(window.vm, keepAliveModuleName);
          let rows = currentTab.ag.datas.row.filter(item => currentTab.idArray.includes(item.ID.val));
          let arr = [];
          for (let i = 0; i < rows.length; i++) {
            let model = rows[i];
            let newObj = new Object();
            for (let property in model) {
              let val = model[property].refobjid ? model[property].refobjid : model[property].val;
              // 小红书面单版本是新版还是旧版
              if (property === 'VERSION') {
                newObj[property] = model[property].refobjval || val || null;
              } else {
                newObj[property] = val !== undefined ? val : null;
              }
              if(property === 'ISDIRECTCONNECT' && type === 'taoda'){
                newObj[property] = model[property].refobjval
              }
            }
            arr.push(newObj);
          }
          return {
            ids: currentTab.idArray,
            rows: arr,
            rowData:currentTab.ag.datas.row,
            userInfo: currentTab.userInfo,
          };
        }
      },
      closeIframe() {
        // if(this.objTabActionDialogConfig && this.objTabActionDialogConfig.isrefrsh){
        // this.$emit('closeActionDialog', true);
        // }
        this.$emit('closeActionDialog', true);
        if (window.parent.location.href.indexOf('T_V_OMSPRINTORDER') > -1 || window.parent.location.href.indexOf('T_V_OMSPRINTJIUCUOORDER') > -1) {
          document.getElementsByClassName('ark-btn-posdefault')[0].click();
        }
      },
      getRows() {
        let obj = window.R3.getSessionObject('customizeMessage');
        if (!obj) {
          return {};
        }
        let data = obj[this.$route.params.customizedModuleName];
        if (!data) {
          return {};
        }
        let json = {};
        if (data) {
          let row = data.standardTableurlCustomized;

          Object.keys(row).map(item => {
            json[item] = row[item].refobjid ? row[item].refobjid : row[item].val;
            return item;
          });
        }

        return json;
      },
      closePage() {
        const { fullPath } = this.$route;
        let index = fullPath.lastIndexOf('/');
        let tableId = fullPath.slice(index + 1, fullPath.length);
        const {
          keepAliveModuleName, tableName
        } = window.R3.store.state.global.activeTab;
        window.R3.store.commit('global/tabCloseAppoint', {
          tableName,
          routeFullPath: fullPath,
          keepAliveModuleName,
        });
        // 跳转
        window.R3.store.commit('global/tabOpen', {
          type: 'S',
          tableName,
          tableId,
          back: true
        });
      },
    },
    mounted() {
      // this.$nextTick(() => {
      //     let intervalTag =  setInterval(() => {
      //         if(document.getElementById("iframe").contentWindow && document.getElementById("iframe").contentWindow.setR3Data){
      //             clearInterval(intervalTag)
      //             let keepAliveModuleName = window.R3.store.state.global.activeTab.keepAliveModuleName
      //             if(keepAliveModuleName.startsWith('S.')){
      //                 let currentTab = this.$_live_getChildComponent(window.vm,keepAliveModuleName)
      //                 let rows = currentTab.ag.datas.row.filter(item => currentTab.idArray.includes(item.ID.val))
      //                 let arr = [];
      //                 for (let i = 0; i < rows.length; i++) {
      //                     let model = rows[i];
      //                     let newObj = new Object();
      //                     for (let property in model) {
      //                         let val = model[property].val;
      //                         newObj[property] = val !== undefined ? val : null;
      //                     }

      //                     arr.push(newObj);
      //                 }
      //                 // this.$_live_getChildComponent(window.vm,)
      //                 document.getElementById("iframe").contentWindow.setR3Data({
      //                     ids: currentTab.idArray,
      //                     rows:arr,
      //                     userInfo: currentTab.userInfo,
      //                 })
      //             }
      //         }
      //     },300)
      // })
      let src = document.getElementById('iframe').src;
      if (src.indexOf('PrintExpressList') > -1) {
        document.getElementsByClassName('ark-modal-close')[0].addEventListener('click', () => {
          this.closePage();
        });
      }
    },
    created() {
      window.getR3Data = this.getR3Data;
      window.closeIframe = this.closeIframe;
      window.getRows = this.getRows;
      window.sessionGet = this._sessionGet;
      window.sessionRemove = this._sessionRemove;
      window.sessionSet = this._sessionSet;
      window.sessionClear = this._sessionClear;
      let componentTag = this.$options._componentTag;
      let json = componentTag.includes('.') ? customizedPageConfig : customizedModalConfig;
      if (componentTag.includes('.')) {
        let data = this.getR3Data;
        if (data && data.ids && data.ids.length > 0) {
          componentTag = `${componentTag.split('.')[1]}?id=${data.ids.join(',')}`;
        } else {
          componentTag = componentTag.split('.')[1];
        }
      }

      Object.keys(json).map(item => {
        if (item.toUpperCase() === componentTag.toUpperCase()) {
          this.html = json[item].url;
        }
      });
      // if(this.objTabActionDialogConfig && this.objTabActionDialogConfig.cuscomponent){
      //     this.html = this.objTabActionDialogConfig.cuscomponent
      // }
      if (!this.$options._componentTag.includes('.')) {
        this.$nextTick(() => {
          console.log(document.getElementById('iframe').src);
          document.getElementsByClassName('test')[0].parentNode.style.overflow = 'auto';
          this.styleClass.height = `${window.innerHeight - 70}px`;
          if (document.getElementById('iframe').src.indexOf('PrintWarehouseList') != -1) { // 打印仓库拣货单样式
            this.styleClass.width = '600px';
          } else {
            this.styleClass.width = `${window.innerWidth - 240}px`;
          }
        });
        // this.styleClass.height = `${window.innerHeight - 70}px`;
        // this.styleClass.width = `${window.innerWidth - 142}px`;
      }
    }
  };
</script>
<style lang="less" >
.test{
    background: #fff;
    padding: 10px;
    overflow: auto;
    iframe {
        width: 100%;
        height: 100%;
        overflow: auto;
    }
}
</style>
