<template>
    <div class="website-breadcrumb-box">
        <div class="st-breadcrumb">
            <router-link :to="item.path" :class="!item.last ? 'st-breadcrumb-item' : 'st-breadcrumb-item-last'"
                         v-for="item in nodes"
                         :key="item.path">
                <div v-text="item.display" class="breadcrumb-div"></div>
            </router-link>
        </div>
    </div>
</template>

<script>
    import * as commonUtils from '@/utils/common';

    export default {
        name:    'MtaBreadcrumb',
        props:   {
            options: {
                type: Object,
                default() {
                    return {};
                },
            },
        },
        data() {
            return {
                nodes: [
                    /*{
                        path:    '/c/kecheng/lists',
                        display: '课程列表',
                        last:    false,
                    },
                    {
                        path:    '/c/kecheng/details',
                        display: '课程大纲',
                        last:    false,
                    },
                    {
                        path:    '/c/kecheng/courseInfo',
                        display: '第一章 第一节',
                        last:    true,
                    },*/
                ],
            };
        },
        methods: {
            getParentRoute(route) {

                let parentPath = route.meta.parentPath;
                if (parentPath && parentPath !== 'croot') {
                    if (typeof parentPath === 'string') {
                        return this.$router.match(parentPath, this.current);
                    } else if (typeof parentPath === 'object' && Array.isArray(parentPath)) {
                        if (parentPath.length < 2) {
                            throw new Error(' 业务逻辑错误 parentPath 作为数组长度不能小于2');
                        }
                        let arrPaths = commonUtils.getCache('Breadcrumb', 'arrPaths');
                        let maxIndex = -1;
                        let retJ = 0;
                        for (let j = 0; j < parentPath.length; j++) {
                            const pathRoute = parentPath[j];
                            for (let i = arrPaths.length - 1; i > -1; i--) {
                                const pathCache = arrPaths[i];
                                if (pathRoute === pathCache) {
                                    if (maxIndex < i) {
                                        maxIndex = i;
                                        retJ = j;
                                    }
                                }
                            }
                        }
                        return this.$router.match(parentPath[retJ], this.current);
                    } else {
                        console.log(route);
                        throw new Error('Route meta.parentPath 异常');
                    }
                } else {
                    return null;
                }

            },
            getDisplayByRoute(route) {
                let chaptersection = commonUtils.getCache('Breadcrumb', 'chaptersection');
                let breadcrumbDisplayParamsObj = {};
                if (chaptersection) {
                    breadcrumbDisplayParamsObj.courseInfo = {
                        p1: chaptersection.chapter,
                        p2: chaptersection.section,
                    }
                }

                let retDisplay = '';
                // key value
                let p1 = '';
                let p2 = '';
                for (var key in breadcrumbDisplayParamsObj) {
                    if (route.path.indexOf(key) > -1) {
                        p1 = breadcrumbDisplayParamsObj[key].p1;
                        p2 = breadcrumbDisplayParamsObj[key].p2;
                        break;
                    }
                }

                if (!route.meta.breadcrumb) {
                    throw new Error('路由:' + route.path + '  未标记breadcrumb');
                }
                // console.log(route, route.meta.breadcrumb, p1, p2);

                if (p2 !== '') {
                    retDisplay = route.meta.breadcrumb.display.stformat(p1, p2);
                } else if (p1 !== '') {
                    retDisplay = route.meta.breadcrumb.display.stformat(p1);
                } else {
                    retDisplay = route.meta.breadcrumb.display;
                }
                return retDisplay;
            },
            /**
             * 通过路由构筑单节点
             * @param nodesReverse
             * @param route 路由
             */
            getNodeByRoute(nodesReverse, route) {
                let node = {};
                /*{
                        path:    '/c/kecheng/courseInfo',
                        display: '第一章 第一节',
                        last:    true,
                    }*/
                node.path = route.path;
                node.display = this.getDisplayByRoute(route);

                if (node) {
                    nodesReverse.push(node);
                }

                const parentRoute = this.getParentRoute(route);
                if (parentRoute) {
                    this.getNodeByRoute(nodesReverse, parentRoute);
                }
            },
        },
        created() {
            /*// console.log('this.$route');
            console.dir(this.$route);
            // console.log('this.$router');
            console.dir(this.$router);*/


            let nodesReverse = [];
            // 通过路由构筑单节点
            this.getNodeByRoute(nodesReverse, this.$route);

            // 添加首页
            let tenantFlag_key = window.localStorage.getItem(`tenantFlag_key`)
            if (tenantFlag_key === '1') {
                nodesReverse.push({
                                      path:    '/c/Index',
                                      display: '首页',
                                  });
            } else if (tenantFlag_key === '2') {
                nodesReverse.push({
                                      path:    '/c/IndexPeixun123',
                                      display: '首页',
                                  });
            }else {
                nodesReverse.push({
                                      path:    '/index',
                                      display: '首页',
                                  });
            }


            for (let i = nodesReverse.length - 1; i > -1; i--) {
                const nodeReverse = nodesReverse[i];
                if (i !== 0) {
                    nodeReverse.last = false;
                } else {
                    nodeReverse.last = true;
                }
                this.nodes.push(nodeReverse);
            }
        },
        mounted() {
        },
        watch:   {},
    };
</script>
