<template>
  <div v-loading="loading" class="app-container">
    <iframe ref="companyIframe" marginwidth="0" marginheight="0" frameborder="0" scrolling="no" width="100%" :height="iframeHight" :src="src" />
  </div>
</template>

<script>
export default {
  name: 'PartyB',
  components: {

  },
  data() {
    return {
      loading: false, // 是否加载完成
      navHeight: 68,  // iframe以为的高度px
      iframeHight: window.innerHeight, // iframe高度
      domain: 'https://pre-plug.jiansheku.com',
      // domain: 'http://192.168.60.30:3300',
      ak: 'aec7b3ff2y2q8x6t49a7e2c463ce21912' // 需要携带的sdkId
    }
  },
  created() {
    if (this.$route.params.id) { // 获取companyId
      this.loading = true
      if(this.$route.name=='Company'){  //企业详情
        this.src = `${this.domain}/enterprise/${this.$route.params.id}?ak=${this.ak}`
      }
      if(this.$route.name=='Personnel'){ //人员详情
        this.src = `${this.domain}/personnel/${this.$route.params.id}.html?ak=${this.ak}`
      }
    }
  },
  mounted() {
    this.getInframeHight() // 实时控制iframe高度
    window.addEventListener('scroll', this.scrolling) // 监听页面滚动事件
  },
  beforeDestroy() {
    window.removeEventListener('scroll', this.scrolling) // 销毁页面滚动事件
  },
  methods: {
    getInframeHight() {
      const _this = this
      window.addEventListener('message', function(e) {
        const data = e.data
        const sc = document && document.documentElement.scrollTop || document && document.body.scrollTop
        if (data && typeof data === 'object') {
          // 动态设置iFrame高度
          if (data.height) {
            _this.iframeHight = data.height
            _this.loading = false
          }
          // 点击企业详情页 栏目名动态设置滚动高度
          if (data.scrollHeight) {
            window.scrollTo(sc, parseInt(data.scrollHeight) + _this.navHeight + 15)
          }
          // 点击企业详情页 栏目下子标签动态设置滚动高度
          if (data.clientHeight) {
            window.scrollTo(sc, sc - 30)
          }
          // 点击下拉子标签动态设置滚动高度
          if (data.initHeight || data.initHeight===0) {
            window.scrollTo(sc, data.initHeight)
          }
          // 根据子页面弹窗显示隐藏控制当前页面是否固定不可滚动
          if (data.scrollDisabled) {
            document.body.style.overflow = data.scrollDisabled
          }
        }
      })
    },
    scrolling() {
      // 滚动条距文档顶部的距离
      const scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop
      // 准备传值
      this.$nextTick(() => {
        const ifa = this.$refs.companyIframe
        ifa.contentWindow.postMessage({ 'scrollTop': scrollTop }, '*')
      })
    }
  }
}
</script>

<style lang="scss" scoped>
  .app-container{
    padding: 0;
  }
</style>
