//axios.get("/vue/comp/base").then(function(res){

  basecomp.forEach(function (comp) {
    try{
      var obj = eval("(" + comp + ")");
      Vue.component(obj.vname, obj);
    }catch(e){
      console.log(e);
    }
  });

  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:{},
    },
  });
  Vue.mixin({
    props:["modelName"],
    computed:Vuex.mapState({
      currentUser:state=>{
        return  state.currentUser;
      },
      codePath:state=>{
         return state.currentCodePath;
      },
      ctlName:function(){
         return this.modelName+"Ctl";
      },
    }),
    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) => {
       next();
    });
    var app = new Vue({
      el:"#app",
      router:router,
      store:store,
      data:function(){
        return {
          loading:true,
          logins:[
          
          ],
        };
      },
      created(){
        $(window).resize(()=>{
          $("#app").css("min-height",$(window).height());
        });
        $("#app").css("visibility",'visible');
        this.loading=false;
      },
      updated(){
  
      },
      mounted(){
        var self=this;
        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});
        }else{
          this.logins.push({"icon":"el-icon-arrow-right","title":"退出","type":"text",key:"exit","isOnGrid":true});
        }
        this.checkLogin();
      },
      methods:{
        async bizadmin(){
           location.href="/admin";
        },
        async settingclick(){
          var d=  await this.$root.postReq("/web/auth/userCtl/navSysSetting",{});
          location.href=d.data.jumpUrl;
        },
        async exitclick(){
          var d=  await this.$root.postReq("/web/auth/userCtl/exit",{});
          location.reload();
          
        },
        async checkLogin(){
          var d=  await this.$root.postReq("/web/auth/userCtl/checkLogin",{});
          if(d && d.status==0 && d.data){
             this.$store.state.currentUser=d.data;
          }else{
            //alert("no login");
          }
        },
        onselect(code){
            if(code=="login"){
               location.href=appinfo.oauthlogin;
            }
            if(code=="register"){
              location.href=appinfo.oauthreg;
            }
        },
        getReq(path,data){
          var self=this;
          return axios.get(path,{params:data,"headers":{"codepath":this.codePath}}).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.codePath}}).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;
            }
          });
        },
      },
    });
  });
//});
