<!-- 会员中心 -->
<template>
  <div class="member-center">
    <iframe
      ref="memberCenterIframe"
      width="100%"
      class="member-center-iframe"
      :src="iframeLink"
      :style="{ display: leave ? 'none' : 'block' }"
    />
    <!-- <cr-overlay :show="loading" /> -->
  </div>
</template>

<script>
import config from '@/config';
import { maxMember } from '@/api/member.api';
import localStorageService from '@/service/localStorage.service';
import iframeParentMixin from '@/mixins/iframeParent.mixin';
import { sStorage } from '@/service/sessionStorage.service';
import { getVccChannel } from '@/service/userInfo.service';

export default {
  mixins: [iframeParentMixin],
  data() {
    return {
      iframeLink: '',
      loading: true,
      leave: false
    };
  },
  computed: {
    memberCenterIframe() {
      return this.$refs.memberCenterIframe;
    }
  },
  watch: {
    loading(val) {
      this.loading = val;
    }
  },
  created() {
    this.getUserAndMaxInfo();
  },
  mounted() {
    this.$nextTick(() => {
      if (this.$refs.memberCenterIframe) {
        this.$refs.memberCenterIframe.onload = () => {
          const timer = setTimeout(() => {
            this.loading = false;
            clearTimeout(timer);
          }, 800);
          // 加载成功
        };
      }
    });
  },
  beforeDestroy() {
    this.leave = true;
    console.log(' this.leave', this.leave);
  },
  methods: {
    async getUserAndMaxInfo() {
      try {
        const [res] = await maxMember.getUserAndMaxInfo();
        if (res) {
          const { maxMemberInfo = {} } = res;
          const vccToken = this.$route.query.vccToken || localStorageService.get('vccToken');
          const vccChannel = getVccChannel('vccChannel');
          if (Object.keys(maxMemberInfo).length) {
            this.iframeLink = `${
              config.vipHost
            }/maxMember?vccToken=${vccToken}&SHUMEI_KEY=${sStorage.getItem(
              'shumeiKey'
            )}&vccChannel=${vccChannel}`; //max会员地址
            return false;
          }
          this.iframeLink = `${
            config.vipHost
          }/memberCenter?vccToken=${vccToken}&SHUMEI_KEY=${sStorage.getItem(
            'shumeiKey'
          )}&vccChannel=${vccChannel}`; // 普通会员地址
        }
        console.log('iframeLink', this.iframeLink);
      } catch (error) {
        return false;
      }
    }
  }
};
</script>

<style lang="less" scoped>
.member-center {
  height: 100%;
  &-iframe {
    height: 100%;
  }
  /deep/.cr-overlay {
    background-color: #212429;
    bottom: 56px;
  }
}
</style>
