<template>
  <div :id="$route.query.isMobile!=='true'?'containt':''">
    <component v-if="showWfContent && wfContent" :is="wfContent" visible-key="showWfContent" :outParam.sync="outParam"
               :_dataId.sync="dataId" :defaultShowWfContent="defaultShowWfContent" :_isOutLink="true"
               :parent-target="_self"
               @reload="$reloadHandle" v-bind="queryParam"></component>
  </div>
</template>

<script>
import indexUtil from "@base/utils/index.js"
import {
  setReportGlobalParam
} from "@base/components/xform/utils/util";

export default {
  data() {
    return {
      reportCode: null,
      showWfContent: true,
      wfContent: null,
      outParam: null,
      dataId: null,
      defaultShowWfContent: null,
      queryParam: {}
    };
  },
  created() {
    this.login();
  },
  mounted() {
    history.pushState(null, null, document.URL);
    window.addEventListener('popstate', this.handlePopState); // 在Vue组件的mounted钩子中添加事件监听
  },
  beforeDestroy() {
    window.removeEventListener('popstate', this.handlePopState); // 在beforeDestroy钩子中移除事件监听
  },
  methods: {
    initGlobalReqData() {
      let fd = this.$route.query.fd;
      if (fd) {
        setReportGlobalParam(fd);
      }
    },
    async initData() {
      const isMobile = /iPhone|iPad|iPod|Android/i.test(navigator.userAgent);
      let queryParam = this.$baseLodash.cloneDeep(this.$route.query);
      let murl = this.$route.query.urlmobile;
      let purl = murl && isMobile ? murl : this.$route.query.url;
      let dataId = this.$route.query.dataId;
      if (purl == '/index') {
        location.hash = '/home';
        location.reload();
        return;
      }
      /*let param = this.$route.query.param;
      if (param) {
        this.outParam = JSON.parse(param)
      }*/
      this.dataId = dataId;
      if (this.$route.query.showWfContent == 'true') {
        this.defaultShowWfContent = true;
      }

      delete queryParam.urlmobile;
      delete queryParam.url;

      if (purl.indexOf('.html') >= 0) {
        let eUrl = purl;
        let pstr = Object.keys(queryParam).map(key => {
          return key + "=" + queryParam[key]
        }).join("&")

        if (pstr) {
          if (purl.indexOf('.html?') >= 0) {
            eUrl = eUrl + '&' + pstr
          } else {
            eUrl = eUrl + '?' + pstr
          }
        }

        location.href = eUrl;
        return
      }

      delete queryParam.dataId;
      delete queryParam.showWfContent;
      delete queryParam.access_token;
      delete queryParam.tp;
      delete queryParam.thirdparty_info;
      delete queryParam.i18nLang;

      let url = purl + '.vue';
      this.queryParam = queryParam;
      this.showWfContent = true;
      // this.wfContent = require('@/views' + url).default;
      let t = "@base/views";
      let urlArr = [
        '/user/bill_setting/h5_view.vue',
        '/user/form/view/list.vue',
        '/user/form/view/edit.vue'
      ]
      this.initUserInfo(async () => {
        await this.initI18n();
        if (urlArr.includes(url)) {
          this.wfContent = require('@base/views' + url).default;
        } else if (url.startsWith(t)) {
          let path = url.slice(t.length);
          this.wfContent = require('@base/views' + path).default;
        } else {
          this.wfContent = require('@/views' + url).default;
        }
      })
    },
    initUserInfo(callback) {
      this.$store
        .dispatch('user/getInfo2')
        .then(() => {
          callback && callback();
        });
    },
    getI18nLang() {
      let i18nLang = this.$route.query.i18nLang || "zh";
      return i18nLang;
    },
    initI18n() {
      let lang = this.getI18nLang();
      this.$i18n.locale = lang // 设置给本地的i18n插件
      localStorage.setItem("i18n-lang", lang);
      return indexUtil.initI18nMessage("business", lang, true);
    },
    login() {
      let token = this.$route.query.access_token;
      let url = this.$route.query.url;
      let hasToken = this.$route.query.hasToken;
      let tp = this.$route.query.tp || this.$route.query.thirdparty_info;
      if (url) {
        if (token) {
          this.initByToken(token);
        } else if (tp) {
          this.thirdpartyLogin(tp);
        } else if (hasToken == "true") {
          this.$nextTick(() => {
            this.initData();
          });
        }
        this.initGlobalReqData();
      }
    },
    initByToken(token) {
      if (!token) return;
      this.$store
        .dispatch('user/addToken', {
          token
        })
        .then(() => {
          this.$nextTick(() => {
            this.initData();
          });
        });
    },
    thirdpartyLogin(tp) {
      let lang = this.getI18nLang();
      this.$http({
        url: USER_PREFIX + `/thirdparty_auth/login`,
        method: `post`,
        data: {
          tp: tp,
          language: lang
        },
        isLoading: true,
        modalStrictly: true,
        useToken: false,
        success: res => {
          let thirdpartyToken = res.objx;
          this.initByToken(thirdpartyToken);
        }
      });
    },
    handlePopState(event) {
      // 阻止回退
      history.go(1);
    }
  }
};
</script>

<style lang="scss" scoped>
::v-deep .tab-box > .el-tabs__content {

  .designer-view {
    height: calc(100vh - 33px)
  }

  .grid-height,
  .designer-view .el-form.render-form {
    height: calc(100vh - 41px)
  }
}

::v-deep #containt .detail-wrap {
  .d-header {
    margin: -8px -12px 0;
  }

  .d-cont {
    margin: 0 -12px;
    height: calc(100vh - 73px)
  }
}
</style>
