axios.get("/vue/comp/base").then(function(res){
  if(res.data){
    res.data.forEach(function(comp){
      var obj=eval("("+comp+")");
      Vue.component(obj.vname,obj);
    });
  }
  console.log("load base component finished...");
  function buildPromise(compath){
     return Promise.resolve(new Promise(function(resolve,reject){
       //异步加载组件
       axios.get(compath).then(function(res){
          console.log(compath);
         // var obj=eval("("+res.data+")");
          var obj = eval("(" + unescape(res.data) + ")");
          resolve(obj);
       }).catch(function(reason){
         console.log(reason)
       })
     }));
  }
  function componentFactory(comname){
    var compath="/vue/comp/"+comname;
    var tmpFunc=function(){
      return buildPromise(compath);
    }
    return tmpFunc;
  }
  window.componentFactory=componentFactory;
  const store = new Vuex.Store({
    state: {
      currentCodePath:"",
      currentUser:null,
      curSelRow:{},
      company:null,
    },
  });
  Vue.mixin({
    props:["groupName","modelName"],
    computed:Vuex.mapState({
      currentUser:state=>{
        return  state.currentUser;
      },
      codePath:state=>{
         return state.currentCodePath;
      },
      ctlName:function(){
        return this.groupName +"/"+this.modelName+"Ctl";
     },
      currentCompany:state=>{
        return state.company;
      },
    }),
    methods:{
      getUrl(md){
        return "web/"+this.ctlName+"/"+md;
      },
    }
  });
  var routes=[];
  var codepaths={};
  axios.get("/web/common/metaCtl/getRouteConfig").then(d=>{
    Object.keys(d.data.data.bizs).map((k)=>{
      //path是路由，静态的路由
      var pathTmp=d.data.data.bizs[k].path;
      var codepath=d.data.data.bizs[k].codepath;
      var tmp={"path":pathTmp,"components":{
        "default":componentFactory(d.data.data.bizs[k].comname)
      },};
       routes.push(tmp);
       codepaths[pathTmp]=codepath;//暂存每个叶子节点对应的codepath
    });
    console.log(routes);
    var router=new VueRouter({
      routes:routes,
    });

    router.beforeEach((to, from,next) => {
      store.state.currentCodePath = codepaths[to.path];
      store.state.company=companyinfo;
       next();
    });
    var app = new Vue({
      el:"#app",
      router:router,
      store:store,
      data:function(){
        return {
          loading:true,
          logins:[
          
          ],
          stageHeight:0,
          tocomany:tocompanyinfo,
        };
      },
      created(){
        $(window).resize(()=>{
          var h=$(window).height();
          var m=h-90;
          // $("#app").css("min-height",h+"px");
          // $("#app-stage").css("min-height",m+"px");
        });
        var h=$(window).height();
        var m=h-90;
        // $("#app").css("min-height",h+"px");
        // $("#app-stage").css("min-height",m+"px");
        this.stageHeight=m;
        // $("#app").css("visibility",'visible');
        this.loading=false;
        this.$store.state.company=companyinfo;
      },
      updated(){
  
      },
      mounted(){
        var self=this;
        this.checkLogin(()=>{
          if(!this.currentUser){
            // this.logins.push({"icon":"el-icon-location-outline","title":"登录","type":"text",key:"login","isOnGrid":true});
            // this.logins.push({"icon":"el-icon-edit-outline","title":"注册","type":"text",key:"register","isOnGrid":true});
            if(location.href.indexOf("gtb")>=0){
              this.$router.replace("/tlogin");
            }
          }else{
            this.logins.push({"icon":"el-icon-arrow-right","title":"退出","type":"text",key:"exit","isOnGrid":true});
            this.$router.replace("/gtbhome");
          }
        });
      
      },
      methods:{
        showMask(){

        },
        hideMask(){

        },
        localdata(key,item){
          if(item){
            localStorage.setItem(key,JSON.stringify(item));
          }else{
            var strobj=localStorage.getItem(key);
            if(strobj){
              return  JSON.parse(strobj);
            }else{
              return null;
            }

          }
      },
      
        appclick(){
          location.href="/gtb";
        },
        settingclick(){
          location.href="/admin"
        },
        async exitclick(){
          var d=  await this.$root.postReq("/web/auth/userCtl/exit",{});
          location.href="/gtb";
          
        },
        async checkLogin(cbk){
          var d=  await this.$root.postReq("/web/auth/userCtl/checkLogin",{});
          if(d && d.status==0 && d.data){
             this.$store.state.currentUser=d.data;
            //检查是否有公司信息
            if(!tocompanyinfo && this.currentUser && !this.currentUser.isSuper){
                this.$router.push({path:"/companyinfo"});
            }
            if(cbk){
              cbk();
            }
          }else{
            //alert("no login");
            if(cbk){
              cbk();
            }
          }
        },
       
        onselect(code){
            if(code=="login"){
              this.$router.push("/login");
            }
            if(code=="register"){
              this.$router.push("/register");
            }
        },
        getReq(path,data){
          var self=this;
          return axios.get(path,{params:data,"headers":{"codepath":this.currentCodePath}}).then(function(r){
            if(r && r.data && r.data.status==-99){
              self.$store.state.currentUser=null;
              self.hideMask();
              self.hideMenu();
              self.$router.push("/");
              self.$message({
                message: '扫码成为合伙人',
                type: 'warning'
              });
              self.showLogin();

              return null;
            }else{

              if(r.data && r.data.bizmsg && r.data.bizmsg!="empty"){
                self.$message({
                  message: r.data.bizmsg,
                  type: 'warning'
                });
              }
              return r.data?r.data:null;;
            }
          });
        },
        postReq(path,data){
          return axios.post(path,data,{"headers":{'codepath': this.currentCodePath}}).then(function(r){
            if(r && r.data && r.data.status==-99){
              self.$store.state.currentUser=null;
              self.hideMask();
              self.hideMenu();
              self.$router.push("/");
              self.$message({
                message: '扫码成为合伙人',
                type: 'warning'
              });
              self.showLogin();
              return null;
            }else{
              return r.data?r.data:null;
            }
          });
        },
      },
    });
  });
});
