<template>
    <div class="sidebar" :style="collapse ? 'width:85px' : ''">
        <div class="logo" v-if="!collapse">
            <img src="@/assets/img/img_logo2.png" />
            <div class="title"><span class="point"></span>系统管理</div>
        </div>
        <div class="logo shrink" v-else>
            <div class="text">系统管理</div>
        </div>
        <div class="menu-wrap no-scrollbar">
            <el-menu
                class="sidebar-el-menu"
                :default-active="onRoutes"
                :collapse="collapse"
                background-color="#171F32"
                text-color="#aaadba"
                unique-opened
                router
                :style="collapse ? 'width:84px' : 'width:220px'"
            >
                <menu-tree :menuList="thisMenuList" :collapse="collapse" :openArray="openArray"></menu-tree>
                <!-- <template v-for="item in thisMenuList">
                    <template v-if="item.children">
                        <el-submenu :index="item.linkPath" :key="item.linkPath">
                            <template slot="title">
                                <img :src="envConfig.baseImgApi + item.menuImage" style="width: 14px; height: 14px; margin-right: 10px" />
                                <span slot="title">{{ item.name }}</span>
                            </template>
                            <template v-for="subItem in item.children">
                                <el-submenu v-if="subItem.children" :index="subItem.linkPath" :key="subItem.linkPath">
                                    <img
                                        slot="title"
                                        :src="envConfig.baseImgApi + subItem.menuImage"
                                        style="width: 14px; height: 14px; margin-right: 10px"
                                    />
                                    <template slot="title">{{ subItem.name }}</template>
                                    <el-menu-item
                                        style="padding-left: 88px"
                                        v-for="threeItem in subItem.children"
                                        :key="threeItem.linkPath"
                                        :index="threeItem.linkPath"
                                    >
                                        <template slot="title">{{ threeItem.name }}</template>
                                    </el-menu-item>
                                </el-submenu>
                                <el-menu-item v-else :index="subItem.linkPath" :key="subItem.linkPath">
                                    <img
                                        :src="envConfig.baseImgApi + subItem.menuImage"
                                        style="width: 14px; height: 14px; margin-right: 10px"
                                    />
                                    <template slot="title"> {{ subItem.name }}</template>
                                </el-menu-item>
                            </template>
                        </el-submenu>
                    </template>
                    <template v-else>
                        <el-menu-item :index="item.linkPath" :key="item.linkPath">
                            <img :src="envConfig.baseImgApi + item.menuImage" style="width: 14px; height: 14px; margin-right: 10px" />
                            <span slot="title">{{ item.name }}</span>
                        </el-menu-item>
                    </template>
                </template> -->
            </el-menu>
        </div>
        <div class="menu-footer" :class="theme == 'dark' ? 'dark' : 'light'" :style="collapse ? 'width:84px' : 'width:220px'">
            <div></div>
        </div>
        <div class="footer">
            <img
                :class="[collapse ? 'expandhover' : 'packuphover']"
                :src="collapse ? btnCollpaseright : btnCollpaseleft"
                @click="collapseChage"
            />
        </div>
    </div>
</template>

<script>
import envConfig from '@/config';
import MenuTree from './MenuTree.vue';
import { mapState, mapGetters, mapMutations, mapActions } from 'vuex';
const darkTheme = new Set(['/dispatchCenter', '/waterSupplyMap', '/operation/video', '/operation/pipe-network']);
const collapsePages = new Set(['/viewDetail', '/installplanView']);
export default {
    data() {
        return {
            envConfig: envConfig,
            siderMenuList: [],
            btnCollpaseright: require('@assets/img/btn_collpaseright.png'),
            btnCollpaseleft: require('@assets/img/btn_collpaseleft.png'),
            openList: []
        };
    },
    components: {
        MenuTree
    },
    computed: {
        thisMenuList() {
            return this.$store.getters['getThisMenuList'];
        },
        onRoutes() {
            return this.$route.path;
            if (this.$route.path) {
                let theme = darkTheme.has(this.$route.path) ? 'dark' : 'light';
                this.$store.commit('setTheme', darkTheme.has(this.$route.path) ? 'dark' : 'light');
                let iscollapse = collapsePages.has(this.$route.path);
                this.$store.dispatch('collapse', iscollapse);
            }
            return this.$route.path.replace('/', '');
        },
        collapse() {
            return this.$store.getters['getCollapse'];
        },
        theme() {
            return this.$store.getters.getTheme;
        },
        openArray() {
            return this.openList;
        }
    },

    methods: {
        // 侧边栏折叠
        collapseChage() {
            this.$store.dispatch('collapse', !this.collapse);
        },
        thisfindMen(arr, indexPath) {
            
            let status = [];
            arr.map((v) => {
                if (v.children) {
                    v.children.map((s) => {
                        if ('/' + s.linkPath === indexPath) {
                            status.push(v.linkPath, s.linkPath);
                        }
                    });
                } else {
                    if ('/' + v.linkPath === indexPath) {
                        status.push(v.linkPath);
                    }
                }
            });
            return status;
        }
    },
    watch: {
        $route(newValue, oldValue) {
            this.openList = this.thisfindMen(this.thisMenuList, newValue.path);
        }
    },
    created() {
        this.openList = this.thisfindMen(this.thisMenuList, this.$route.path);
    }
};
</script>


<style  lang="scss">
.sidebar {
    width: 220px;
    display: flex;
    flex-direction: column;
    background: #171f32;
    border-radius: 0px 30px 30px 0px;
    z-index: 2;
    .logo {
        width: 100%;
        height: 110px;
        color: #ffffff;
        padding: 35px 10px 0 20px;
        border-bottom: 1px solid #2a335c;
        margin-bottom: 25px;
        img {
            width: 90px;
            height: 24px;
            display: inline-block;
            vertical-align: middle;
            margin-right: 6px;
        }
        .title {
            font-size: 16px;
            font-weight: 500;
            display: inline-block;
            vertical-align: middle;
            background: rgba(255, 255, 255, 0.1);
            border-radius: 4px;
            border: 1px solid rgba(255, 255, 255, 0.4);
            padding: 0px 7px;
        }
        .point {
            width: 4px;
            height: 4px;
            display: inline-block;
            // margin-right: 5px;
            vertical-align: middle;
            text-align: center;
        }
        .text {
            width: 40px;
            margin-left: -14px;
        }
    }
    .menu-wrap {
        overflow: auto;
        flex: 1;
        scrollbar-width: 0;
    }
    .menu-footer.dark {
        bottom: 0;
        left: 0;
        width: 220px;
        height: 30px;

        div {
            width: 100%;
            height: 100%;
            border-radius: 0px 0 30px 0px;
            background-color: #171f32;
            border: none;
        }
        &.dark {
            background-color: #070f22 !important;
            z-index: 1;
        }

        &.light {
            background-color: #f4f7f9 !important;
            z-index: 1;
        }
    }
    .footer {
        position: absolute;
        bottom: 30px;
        left: 20px;
        z-index: 11;
        width: 30px;
        height: 30px;
        img {
            display: block;
            width: 24px;
            height: 24px;
            cursor: pointer;
        }
    }
}
.el-menu {
    border: none;
}

.sidebar-el-menu .el-submenu.is-opened > .el-submenu__title {
    //设置一级菜单点开后的状态
    color: #fff !important;
    background: rgb(44, 53, 74);
    border-radius: 0;
}
.sidebar-el-menu > .el-submenu.is-opened > .el-menu--inline > .el-submenu.is-opened > .el-submenu__title {
    color: #fff !important;
}
.sidebar-el-menu > div > .el-submenu.is-opened.is-active > .el-submenu__title {
    background: #2c354a !important;
    border-radius: 32px 0px 0px 32px;
    color: rgb(39, 135, 241) !important;
    border-right: 2px solid #2787f1;
    .el-submenu__icon-arrow::before {
        color: rgb(39, 135, 241);
    }
}
.el-menu-item.is-active {
    color: rgb(39, 135, 241);
}
.el-submenu.is-active > div > span {
    color: rgb(39, 135, 241);
}

.sidebar .logo.shrink {
    display: flex;
    font-size: 16px;
    justify-content: center;
    // color: $mainTextColor;
    border-bottom: 2px solid #2a335c;
    margin-bottom: 20px;
    align-items: flex-start;
    .text {
        width: 48px;
        height: 48px;
        background: rgba(255, 255, 255, 0.1);
        border-radius: 4px;
        border: 1px solid rgba(255, 255, 255, 0.4);
        text-align: center;
    }
}

// .sidebar .logo.shrink {
//     display: flex;
//     justify-content: center;
//     color: #2787f1;
//     border-bottom: 2px solid #2a335c;
//     margin-bottom: 20px;
//     align-items: flex-start;
//     .text {
//         width: 48px;
//         height: 48px;
//         background: rgba(255, 255, 255, 0.1);
//         border-radius: 4px;
//         border: 1px solid rgba(255, 255, 255, 0.4);
//         text-align: center;
//     }
// }

.el-menu--collapse > .is-active {
    color: #2787f1;

    background: #2c354a;
    border-radius: 32px 0px 0px 32px;
}

.el-menu--collapse > .is-active > .el-submenu__title {
    background: transparent !important;
}
.el-submenu > .el-submenu__title:hover {
    //设置一级菜单的鼠标经过时候的样式
    background: rgb(44, 53, 74) !important;
    border-radius: 32px 0px 0px 32px !important;
}
.el-menu-item:hover {
    border-radius: 32px 0px 0px 32px;
}
</style>
