<script>
// import HcPage from "./components/Birt/Page";
// import { BirtModelFactory } from "./templates/BirtModelFactory";
import cmps from 'zhc-birt-cmps'
import { LoadUtil } from "./funclib/LoadUtil";
import { getToken } from "./utils/auth";
// import { Tool } from "./funclib/Tool";
import { projectName, requestUrl } from "@birt/config";
// import 'echarts-liquidfill/src/liquidFill.js'
export default {
  extends: cmps.BirtPage,
  data() {
    return {
      birtModel: {},
      VUE_APP_BASE_API: requestUrl,
      pn: projectName || "birt",
    };
  },
  watch: {
    // $route() {
    //   this.loadBirtModel(this.$route.path);
    // },
  },
  methods: {
    // 动态加载报表模型方法
    birtModelMethod(routePath, birtModelMethod, scope) {
      return new Promise((resolve, reject) => {
        let me = this;
        let token = getToken();
        // let token = '3d3fe30c-3d99-41e8-9aea-addd8528aa07'
        // let vId = this.getQueryVariable('viewItemId')
        let vId = window.scpRequestData && window.scpRequestData.__viewItemId
        LoadUtil.loadJsBirtModel({
          loadModelUrl: this.$HI.birtModel,
          routePath: routePath,
          token: token,
          pn: this.pn,
          vId: vId
        })
          .then(() => {
            try {
              if(!window.getBirtModel){
                me.$message.warning("model1接口未响应，请稍等")
                return
              }
              let birtModel = cmps.BirtModelFactory.buildModel(
                getBirtModel(scope || me, cmps.Tool),scope || me
              );
              if(birtModel.css){
                let css = birtModel.css
                if(Array.isArray(css) && css.length > 0){
                  css.forEach(c =>{
                    LoadUtil.loadCss(c)
                  })
                }
                if(typeof css == 'string'){
                  LoadUtil.loadCss(css)
                }
              }
              birtModel.token = token;
              birtModel.birtModelMethod = birtModelMethod;
              resolve(birtModel);
            } catch (e) {
              console.error(e);
            }
          })
          .catch(() => {
            reject();
          });
      });
    },
    loadBirtModel(routePath) {
      return new Promise((resolve, reject) => {
        let me = this;
        this.birtModelMethod(routePath, this.birtModelMethod)
          .then((birtModel) => {
            me.birtModel = birtModel;
            /**
             *网络环境比较差的情况下，打开查找定义model1接口未响应时，
             再次打开查找定义弹框，会应用已加载的getBirtModel，
             导致打开的弹框显示的是上个model1加载的页面，故这边删去挂载在window的getBirtModel方法
             *   */ 
            me.$nextTick(() =>{
              if(window.getBirtModel){
                window.getBirtModel = undefined
              }
            })
            me.applyRouteParam();
            me.setWorkBookParam();
            resolve();
          })
          .catch(() => {
            reject();
          });
      });
    },
    // 获取地址栏参数
    getQueryVariable(variable) {
      var query = window.location.search.substring(1);
      var vars = query.split("&");
      for (var i = 0; i < vars.length; i++) {
        var pair = vars[i].split("=");
        if (pair[0] == variable) {
          return pair[1];
        }
      }
      return false;
    },
    _needsDecodeURI(str) {  
      // 使用正则表达式检查字符串是否包含%字符  
      return /%[0-9A-F]{2}/i.test(str);  
    },
  },
  mounted() {
    var path = window.location.pathname;
    // 编译后多e5
    if (window.HIVUI_SETTING) {
      var serverName = window.HIVUI_SETTING.serverName;
      if (serverName && path.startsWith(serverName)) {
        path = path.substring(serverName.length);
      }
    }
    var __push = window && window.scpRequestData && window.scpRequestData.__push
    if(__push){
      if (this._needsDecodeURI(__push)) {
        __push = decodeURI(__push);
      }
      this.param["__push"] = JSON.parse(__push);
    }
    //     var _push = window.scpRequestData
    // var path = '/platf/user-info/func/query.func'
    this.loadBirtModel(path).then(() => {
      // if (path == "/platf/permission/func/assign.func") this.getOrgData(-1);
      // else if (path == "/platf/permission/func/modify.func")
      //   this.getOrgDataM(-1);
    });
  },
};
</script>
<style>
html,body{
  height: 100%;
}
</style>
