<template>
  <div class="ifrcon">
    <Header :bgmUrl="tempBgm" @toggleBGM="handleBGM" :somedatainfo="somedatainfo" :bgmstatus="bgmstatus" :params="params" />
    <iframe allowfullscreen="true" ref="iframe" id="showifr" :src="url" frameborder="0" @load="onIframLoad"></iframe>
  </div>
</template>

<script>
import Header from "./ui/Show.Header";
import browser from "@/utils/browser"

export default {
  props:['url','bgmUrl','somedatainfo'],
  components:{Header},
  data(){
    return {
      params:'',
      bgmstatus: false
    }
  },
  computed:{
    tempBgm(){
      return this.bgmUrl
    }
  },
  methods:{
    onIframLoad(){
      window.onmessage = e =>{
        if (e.data.source != "4dage") {
            return
        }

        if (e.data.event == "guide-rooms") {
          this.$bus.emit('guideRoomsData',e.data.params)
        }

          if (e.data.event == "link-click") {
            if (e.data.params.url.indexOf('https://www.4dkankan.com/spc.html?')>-1
            ||e.data.params.url.indexOf('https://www.4dkankan.com/smobile.html?')>-1
            ||e.data.params.url.indexOf('https://test.4dkankan.com/spc.html?')>-1
            ||e.data.params.url.indexOf('https://test.4dkankan.com/smobile.html?')>-1) {
              let m = browser.urlHasValueFromUrl('m',e.data.params.url)
              this.$emit('changeUrl',m)
            } else{
              this.$emit('otherUrl',e.data.params.url)
            }

        }




        if (e.data.event == "action") {
          this.$bus.emit('currentMode',e.data.params)
          
          if (e.data.params.type == "playMusic") {
            console.log(e.data.params.data.status,'e.data');
            this.bgmstatus = e.data.params.data.status
            
          }
        }

        if (e.data.event == "loaded") {
          console.log(e.data.params);
          this.params = e.data.params
          this.$refs.iframe.contentWindow.postMessage({
              source: "mingyuan",
              event: 'guide-rooms'
          },"*")

          this.$refs.iframe.contentWindow.postMessage({
              source : "mingyuan",
              event:"settings",
              params:{
                  playMusic:true
              }
          },"*")
        }

      }
    },
    handleBGM(status){

       this.$refs.iframe.contentWindow.postMessage({
            source : "mingyuan",
            event:"settings",
            params:{
                playMusic:status
            }
        },"*")
    }
  }
}
</script>

<style lang="less" scoped>
.ifrcon{
  width: 100%;
  height: 100%;
  >iframe{
    width: 100%;
    height: 100%;
  }
}
</style>