import { postAction } from '/@/api/manage/manage'
import { inject } from 'vue';

// TODO 先兼容vue2代码迁移，后期替换为vue3的写法， 当前from src/mixins/iframeMixin.js
export const useIframe = {
  data() {
    return {
      src: '',
      loading: true,
      isUsing: true,
      wordpressSwitch: '3',
      pageError: false,
      wpConfig: {},
      wordpressSetting: '',
      openSetting: []
    }
  },

  mounted() {
    // 监听子页面的消息
    if (typeof window.addEventListener != 'undefined') {
      window.addEventListener('message', this.dealMessage, false)
    } else if (typeof window.attachEvent != 'undefined') {
      window.attachEvent('onmessage', this.dealMessage)
    }
    this.queryWordPressConfig()
  },

  methods: {
    dealMessage(e) {
      if (e.data.status == 2000) {
        if (!e.data.data['wp-auth-check']) {
          this.src = ''
          this.queryWordPressConfig()
        }
      }
    },
    // 调用方法
    iframeLoad() {
      this.$nextTick(() => {
        this.loading = true
        const iframe = this.$refs.iframeCom.iframeRef
        //表示页面加载错误
        this.pageError = true

        iframe.onload = () => {
          this.loading = false
        }
      })

    },

    async queryWordPressConfig() {
      let that = this
      that.wordpressSwitch = ''
      that.pageError = false
      let url = ''
      let params = {
        siteCode: localStorage.getItem('siteCode'),
      }
      await postAction('/adweb/adwebSiteManage/queryWordPressConfig', params).then(function (res) {

        if (res.code === 200) {

          that.wordpressSwitch = res.result.wordpressSwitch ? res.result.wordpressSwitch : 2
          if (res.result.wordpressSetting != null) {
            that.wordpressSetting = res.result.wordpressSetting
          }
          if (res.result.openSetting != null) {
            that.openSetting = res.result.openSetting
          }

          if (res.result.productType == 'woocommerce') {
            url = that.wooUrl ? that.wooUrl : that.url
          }else{
            url = that.url
          }

          let isIframe = 0
          if (that.wordpressSwitch == 1 && (that.wordpressSetting == 'all' || that.openSetting.includes(that.iframePage))) {
            isIframe = 1
          }

          if (that.parantEvent) {
            that.parantEvent(res.result.wordpressSwitch)
          }

          if (res.result.wordpressSwitch == 1 &&
            (that.wordpressSetting == 'all' || that.openSetting.includes('product') || that.openSetting.includes('article')) &&
            isIframe == 1) {
            let domainUrl = res.result.domain
            let token = res.result.wordpressToken
            let username = res.result.wordpressName
            let page_no_login = domainUrl + '/wp-html.php?token=' + token + '&username=' + username + '&jumpUrl=' + encodeURIComponent(url)
            // let page_ready =  domainUrl + "/wp-admin/"+ that.loginUrl +""
            // let ssoResKey =  res.result.ssoResKey
            // if(ssoResKey){
            //   that.src =   page_ready
            // }else{
            that.src = page_no_login
            // }
            that.iframeLoad()
          }
        }
      }).catch((err) => {
        console.log(err)
        that.wordpressSwitch = '3'
      })
    },

  }

}
