import Vue from "vue";
import Router from "vue-router";
import ReportData from "@/components/ReportData";
import SaleData from "@/components/SaleData";
import CollectData from "@/components/collectData";
import ajaxAuth from "../script/api/auth.js";
import globalData from "@/components/common/globaldata/global.data.vue";

import TargetImport from "@/components/targetImport";
import saleGmv from "@/components/saleGmv";
import businessData from "@/components/businessData";
import serviceQualityData from "@/components/serviceQualityData";
import serviceAbility from "@/components/serviceAbility";
import saleDetail from "@/components/saleDetail";
import businessDetail from "@/components/businessDetail";
import serviceQualityDetail from "@/components/serviceQualityDetail";
import serviceAbilityDetail from "@/components/serviceAbilityDetail";
import servantDetail from "@/components/servantDetail";
import nannyData from "@/components/nannyData";
import marketRebuyRemind from "@/components/marketRebuyRemind"

import callTask from "@/components/callTask"; //电访任务报表
import onlineChatData from "@/components/onlineChatData";
import mallRecommendData from "@/components/mallRecommendData";
import orderHeatMap from "@/components/orderHeatMap";
import serviceQualityDataHm from "@/components/serviceQualityDataHm";

import serviceRebuy from "@/components/serviceRebuy"; //服务力服务单次卡T+7复购
import serviceSalary from "@/components/serviceSalary"; //服务力薪资
import marketrRebuyPR from "@/components/marketrRebuyPR"; //复购率报表
import cleaningDataMonitor from "@/components/cleaningDataMonitor"; //保洁数据监控
import bjManagerTool from "@/components/bjManagerTool"; //保洁数据监控
import stockIndex from "@/components/stockIndex"; //库存指标
import repurchaseDetail from "@/components/repurchaseDetail"; //复购明细
import marketActivity from "@/components/marketActivity"; //市场活动

import cpJoinByUser from "@/components/cpJoinByUser"; //企微添加数 by user
import cpJoinData from "@/components/cpJoinData"; //企微添加数 （预采，日常回访2个维度）

import douyinPage from "@/components/douyin/douyinPage"; //抖音页面点击数据
import douyinSales from "@/components/douyin/douyinSales"; //抖音销售服务相关数据
import douyinFiance from "@/components/douyin/douyinFiance"; //抖音财务对账
import douyinShopScore from "@/components/douyin/douyinShopScore"; //抖音门店分数
import douyinBoard from "@/components/douyin/douyinBoard"; //抖音财务对账
import douyinRebuy from "@/components/douyin/douyinRebuy"; //抖音复购
import douyinServant from "@/components/douyin/douyinServant"; //抖音服务力看板
import douyinRepurchase from "@/components/douyin/douyinRepurchase"; //抖音复购看板
import douyinBusiness from "@/components/douyin/douyinBusiness"; //抖音业务看板
import reportViewLog from "@/components/reportViewLog"; //报表浏览记录查看
import  douyinOrderAssign from '@/components/douyin/douyinOrderAssign';//私域服务工单分配

Vue.use(Router);
var isRouteFinish = false;
var iframeList = [];
const router = new Router({
    // mode: 'history',  //去掉url中的#
    routes: [{
        path: "/",
        redirect: "/dashboard"
    },
    {
        path: "/",
        component: () =>
            import("../components/common/Home.vue"),
        meta: {
            title: "自述文件"
        },
        children: [{
            path: "/dashboard",
            component: () =>
                import("../components/page/Dashboard.vue"),
            meta: {
                title: "首页"
            }
        },
        {
            path: "/reportData",
            name: "ReportData",
            component: ReportData,
            meta: {
                title: "报表数据"
            }
        },
        {
            path: "/saleData",
            name: "SaleData",
            component: SaleData,
            meta: {
                title: "销售报表"
            }
        },
        {
            path: "/collectData",
            name: "CollectData",
            component: CollectData,
            meta: {
                title: "服务Gmv统计报表"
            }
        },
        {
            path: "/saleGmv",
            name: "saleGmv",
            component: saleGmv,
            meta: {
                title: "销售看板"
            }
        },
        {
            path: "/businessData",
            name: "businessData",
            component: businessData,
            meta: {
                title: "招商看板"
            }
        },
        {
            path: "/targetImport",
            name: "TargetImport",
            component: TargetImport,
            meta: {
                title: "目标值导入"
            }
        },
        {
            path: "/serviceQualityData",
            name: "serviceQualityData",
            component: serviceQualityData,
            meta: {
                title: "服务质量看板"
            }
        },
        {
            path: "/serviceAbility",
            name: "serviceAbility",
            component: serviceAbility,
            meta: {
                title: "服务力看板"
            }
        },
        {
            path: "/saleDetail",
            name: "saleDetail",
            component: saleDetail,
            meta: {
                title: "销售明细"
            }
        },
        {
            path: "/marketRebuyRemind",
            name: "marketRebuyRemind",
            component: marketRebuyRemind,
            meta: {
                title: "销售复购提醒"
            }
        },
        {
            path: "/businessDetail",
            name: "businessDetail",
            component: businessDetail,
            meta: {
                title: "招商明细"
            }
        },
        {
            path: "/serviceQualityDetail",
            name: "serviceQualityDetail",
            component: serviceQualityDetail,
            meta: {
                title: "服务质量明细"
            }
        },
        {
            path: "/serviceAbilityDetail",
            name: "serviceAbilityDetail",
            component: serviceAbilityDetail,
            meta: {
                title: "服务力明细"
            }
        }, {
            path: "/callTask",
            name: "callTask",
            component: callTask,
            meta: {
                title: "电访任务报表"
            }
        },
        {
            path: "/servantDetail",
            name: "servantDetail",
            component: servantDetail,
            meta: {
                title: "保姆明细"
            }
        },

        {
            path: "/onlineChatData",
            name: "onlineChatData",
            component: onlineChatData,
            meta: {
                title: "在线客服报表"
            }
        },
        {
            path: "/mallRecommendData",
            name: "mallRecommendData",
            component: mallRecommendData,
            meta: {
                title: "商城引荐看板"
            }
        },
        {
            path: "/orderHeatMap",
            name: "orderHeatMap",
            component: orderHeatMap,
            meta: {
                title: "订单热力图"
            }
        },
        {
            path: "/serviceQualityDataHm",
            name: "serviceQualityDataHm",
            component: serviceQualityDataHm,
            meta: {
                title: "服务质量"
            }
        },
        {
            path: "/serviceSalary",
            name: "serviceSalary",
            component: serviceSalary,
            meta: {
                title: "服务力薪资"
            }
        }, {
            path: "/serviceRebuy",
            name: "serviceRebuy",
            component: serviceRebuy,
            meta: {
                title: "服务力复购"
            }
        },
        {
            path: "/marketrRebuyPR",
            name: "marketrRebuyPR",
            component: marketrRebuyPR,
            meta: {
                title: "复购分析报表"
            }
        },
        {
            path: "/cleaningDataMonitor",
            name: "cleaningDataMonitor",
            component: cleaningDataMonitor,
            meta: {
                title: "保洁监控工具"
            }
        },
        {
            path: "/bjManagerTool",
            name: "bjManagerTool",
            component: bjManagerTool,
            meta: {
                title: "保洁管理工具"
            }
        },
        {
            path: "/stockIndex",
            name: "stockIndex",
            component: stockIndex,
            meta: {
                title: "库存指标"
            }
        }, {
            path: "/reportViewLog",
            name: "reportViewLog",
            component: reportViewLog,
            meta: {
                title: "报表浏览记录"
            }
        }, {
            path: "/repurchaseDetail",
            name: "repurchaseDetail",
            component: repurchaseDetail,
            meta: {
                title: "复购看板"
            }
        }, {
            path: "/marketActivity",
            name: "marketActivity",
            component: marketActivity,
            meta: {
                title: "销售活动"
            }
        }, {
            path: "/cpJoinData",
            name: "cpJoinData",
            component: cpJoinData,
            meta: {
                title: "企微添加分析"
            }
        }, {
            path: "/douyinPage",
            name: "douyinPage",
            component: douyinPage,
            meta: {
                title: "小程序数据"
            }
        }, {
            path: "/douyinSales",
            name: "douyinSales",
            component: douyinSales,
            meta: {
                title: "销售服务"
            },
        },
        {
            path: "/douyinFiance",
            name: "douyinFiance",
            component: douyinFiance,
            meta: {
                title: "财务对账"
            },
        },
        {
            path: "/douyinBoard",
            name: "douyinBoard",
            component: douyinBoard,
            meta: {
                title: "抖音看板"
            },
        },
        {
            path: "/douyinRebuy",
            name: "douyinRebuy",
            component: douyinRebuy,
            meta: {
                title: "抖音复购"
            },
        },
        , {
            path: "/douyinRepurchase",
            name: "douyinRepurchase",
            component: douyinRepurchase,
            meta: {
                title: "复购看板"
            }
        },
        {
            path: "/douyinShopScore",
            name: "douyinShopScore",
            component: douyinShopScore,
            meta: {
                title: "门店评分"
            },
        },
        {
            path: "/douyinServant",
            name: "douyinServant",
            component: douyinServant,
            meta: {
                title: "服务力"
            },
        },
        {
            path: "/douyinBusiness",
            name: "douyinBusiness",
            component: douyinBusiness,
            meta: {
                title: "抖音业务看板"
            },
        },
        {
            path: "/nannyData",
            name: "nannyData",
            component: nannyData,
            meta: {
                title: "三嫂数据"
            }
        },
          {
            path: "/douyinOrderAssign",
            name: "douyinOrderAssign",
            component: douyinOrderAssign,
            meta: {
              title: "私域服务工单分配"
            }
          },

        ]
    },
    {
        path: "/login",
        component: () =>
            import("../components/page/Login.vue")
    },
    {
        path: "/hsMap",
        component: () =>
            import("../components/page/hsMap.vue")
    }
    ]
});

// 用router的钩子函数beforeEach()做拦截操作
router.beforeEach((to, from, next) => {
    // console.log("beforeEach");
    // console.log(to);
    // console.log(from);
    // console.log(next);
    let token = window.localStorage.getItem("token");
    let path = to.fullPath.replace("/", "");
    // let menuDesc=to.meta.menuDesc;
    // console.log(menuDesc);
    // console.log("token:" + token + "  to.name:" + to.name + " from:" + from.name + "to.fullPath" + to.fullPath);
    //console.log(path);
    let boo = true;

    if (path == "login" || path == "dashboard" || path == "hsMap") {
        boo = true;
        next(boo);
    } else {
        if (token == null || token == "" || token == "null") {
            // console.log("无token权限");
            boo = false;
            // next({
            //     path: '/login',
            //     query: {
            //         redirect: to.fullPath
            //     } 将跳转的路由path作为参数，登录成功后跳转到该路由
            // })
            next(boo);
        } else {
            // if (!checkurl(path)) {//一定是登陆过的
            //   console.log("无菜单权限！");
            //   boo = false;
            // }

            //如果动态路由已经加载完毕就判断是否为iframe类型，否则加载动态路由

            if (isRouteFinish) {
                let isFrame = iframeList.some(item => {
                    return path == item.menuUrl;
                });
                // console.log(isFrame);
                if (isFrame) {
                    // to.query.urls=baseIframeUrl+path+'?token='+token;
                    let menuDesc = to.meta.menuDesc;
                    console.log(menuDesc);
                    let goUrl = menuDesc + "?token=" + token;
                    // console.log(goUrl);
                    to.query.iframe = true;
                    to.query.urls = goUrl;
                    // console.log('intoframe');
                    // next(boo);
                    next({ ...to, replace: true });
                } else {
                    next(boo);
                }
            } else {
                // 加载动态路由
                // $ajaxSelectusermenu
                ajaxAuth
                    .$ajaxSelectiframemenu({
                        userId: globalData.userInfo.id
                    })
                    .then(res => {
                        if (
                            res.data.errno == 401 ||
                            res.data.errno == "401" ||
                            res.data.errno != 0
                        ) {
                            this.$message.warning("身份认证失败，请登陆！");
                            this.$router.push("/login");
                            next(boo);
                        } else {
                            isRouteFinish = true;
                            iframeList = res.data.data;
                            // console.log(iframeList);

                            iframeList.forEach(iframe => {
                                let dynamicRoute = {
                                    path: "/" + iframe.menuUrl,
                                    component: resolve =>
                                        require(["../components/page/iframe.vue"], resolve),
                                    meta: {
                                        title: iframe.menuName,
                                        menuDesc: iframe.menuDesc,
                                        iframe: true
                                    }
                                };
                                router.options.routes[1].children.push(dynamicRoute);
                                router.matcher = new Router().matcher
                                router.addRoutes(router.options.routes);
                                // console.log(router.options.routes[1]);
                            });

                            let isFrame = iframeList.some(item => {
                                return path == item.menuUrl;
                            });
                            // console.log(isFrame);
                            if (isFrame) {
                                // to.query.urls=baseIframeUrl+path+'?token='+token;
                                let menuDesc = to.meta.menuDesc;
                                // console.log(menuDesc);
                                // let goUrl=menuDesc+'?token='+token;
                                let goUrl = menuDesc;
                                // console.log(goUrl);
                                to.query.urls = goUrl;
                                to.query.iframe = true;
                                // console.log('intoframe');
                            }

                            next({ ...to, replace: true });
                        }
                        next(boo);
                    })
                    .catch(err => {
                        console.log(err);
                    });
            }
        }
    }

    // next(boo); //进入后还会验证token
});

function checkurl(url) {
    let userMenu = JSON.parse(window.localStorage.getItem("menulist")); //菜单list
    let boo = false;
    if (userMenu == undefined) {
        return boo;
    }
    for (let i = 0; i < userMenu.length; i++) {
        let murl = userMenu[i].menuUrl;
        if (url.indexOf(murl) != -1) {
            boo = true;
            break;
        }
    }

    return boo;
}

export default router;
