<template>
  <div class="scene-layer" id="iframediv">
    <!-- <iframe ref="lookLive" :name="getUrlname()" allow="geolocation; microphone; camera; midi; encrypted-media;" :src="url" ></iframe> -->
  </div>

  <com-dialog title="访问密码" enterText="确 定" v-model:show="confirm.show" @submit="loadData()" width="480" :showClose="false">
    <el-input v-model="confirm.psw" placeholder="请输入访问密码" @keydown.enter="loadData"></el-input>
  </com-dialog>

  <div v-if="showImg" class="img-layer">
    <i class="el-icon-close" @click="showImg = ''"></i>
    <img :src="showImg" alt="">
  </div>
</template>

<script>
// import { fireDetailByPsw, getAttachListByPsw } from '@/request/config'
// import axios from 'axios';
import comDialog from "@/components/dialog";
import { types } from '@/constant'

export default {
  data() {
    return { 
      detail: {

      },
      showImg: '',
      confirm: {show: false, psw: ''},
      list: [],
      showInfo: false,
      loadSuccess: false,
    }
  },
  watch: {
    'confirm.show'() {
      console.log(this.confirm.show, !this.showInfo)
      if (this.confirm.show || !this.showInfo) {
        this.$router.back()
      }
    },
  },
  computed: {
    url() {
      let ssurl =  window.location.hash.substr(window.location.hash.indexOf('?') || 0)
      let dataurl = sessionStorage.getItem('visitorurl')
    //   console.log('dataurl',dataurl,dataurl.substr(dataurl.indexOf('?') || 0))
      if(dataurl){
        return process.env.VUE_APP_DOMAIN + '/fire-rtc-live.html' + dataurl.substr(dataurl.indexOf('?') || 0)
      }
    //   const {m,p_id} = this.$route.query
      if (ssurl) {
        // return process.env.VUE_APP_DOMAIN + '/fire-rtc-live.html?m=' + sceneNum +'&p_id='+id
        return process.env.VUE_APP_DOMAIN + '/fire-rtc-live.html' + ssurl
      } else {
          console.log('url获取=>',this.$route)
        return 'javascript:void(0)'
        // return process.env.VUE_APP_DOMAIN + '/spc.html?m=' + this.detail.sceneNum
      }
    },
    tableData() {
      let dataList = this.list || [];
      let cache = [];
      let data = types.reduce((t, c) => {
        let children = dataList.filter(
          (item) => c.name === item.attachmentType
        );
        t.push({ title: c.name, children });
        cache.push(...children);
        return t;
      }, []);

      data[data.length - 1].children.push(
        ...dataList.filter((item) => !cache.includes(item))
      );
      return data;
    },
  },
  methods: {
    getUrlname(){
      return window.location.origin + window.location.pathname
    },
    async loadData() {
      // if (!this.confirm.psw || this.confirm.psw.length === 0) return this.$message.error('请输入访问密码', '提示')
    //   let {data: detail} = await axios.get(fireDetailByPsw, {params: {projectId: this.$route.params.projectId, randCode: this.confirm.psw,type:1}})
    //   this.detail = detail

    //   let {data: list} = await axios.get(getAttachListByPsw, {params: {projectId: this.$route.params.projectId, randCode: this.confirm.psw,type:1}})
    //   this.list = list

    //   this.loadSuccess = true
    //   this.confirm.show = false
    //   this.showInfo = false  //不默认开启

    },
    goto(item) {
      if (item.type !== 1) {
        window.open(item.fileOssUrl)
      } else {
        this.showImg = item.fileOssUrl
      }
    },
    async handleMessage(event){
        var that = this
        if(event.data){
            const {liveStatus,url} = event.data
            url&&sessionStorage.setItem('visitorurl',url)
            if(liveStatus == 0){//已结束
                sessionStorage.removeItem('visitorurl')
                let isOk = await this.$alert(
                    "带看已结束",
                    "确定"
                    ,{
                        showClose:false,
                        showCancelButton:false,
                        closeOnClickModal:false,
                    }
                );
                if(isOk){
                    that.$router.push({name: 'home'})
                }
            }
        console.log('handleMessage===>',event.data)
        }
    }
  },
  mounted() {
    this.loadData()
    window.addEventListener('message',this.handleMessage)    
    var iframe = document.createElement('iframe'); 
    iframe.src=this.url;  
    iframe.width='100%';  
    iframe.height='100%';  
    iframe.frameBorder = '0' 
    iframe.allowFullscreen  = 'true' 
    iframe.name = this.getUrlname()
    if(iframe){
        iframe.allow = 'geolocation; microphone; camera; midi; encrypted-media;'
    }
    document.getElementById('iframediv').appendChild(iframe);
  },
  beforeDestroyed(){
    window.removeEventListener("message", this.handleMessage);
    sessionStorage.removeItem('visitorurl')
  },
  components: {
    "com-dialog": comDialog,
  }
}
</script>

<style lang="scss" scoped>
@import "./style.scss";
</style>