<template>
    <div id="app-wrap">
        <el-container>
            <!-- <el-aside width="300px" class="aside" style="background: #34424f">
                <div class="logo-name">
                    <img
                        class="img-styl"
                        src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.pconline.com.cn%2Fimages%2Fupload%2Fupc%2Ftx%2Fitbbs%2F1705%2F10%2Fc35%2F46579542_1494425402317_mthumb.jpg&refer=http%3A%2F%2Fimg.pconline.com.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1639808068&t=27aeead479ad57ca3a0c604d80173cb4"
                        alt=""
                    />
                    <span class="logo">大数据审核系统</span>
                </div>
                <el-menu
                    class="el-menu-vertical-demo"
                    background-color="#2B3642"
                    text-color="#fff"
                    active-text-color="#eB1200"
                    :unique-opened="true"
                    :default-active="menuIndex"
                >
                    <div v-for="(menu, index) in menuList" :key="index">
                        <el-submenu
                            :index="index + ''"
                            v-if="menu.list && menu.list.length"
                        >
                            <div slot="title" class="active-nav">
                                <i class="el-icon-s-check"></i>
                                <span class="menu-name">{{ menu.name }}</span>
                            </div>
                            <el-menu-item
                                style="padding: 0px"
                                v-for="(item, a) in menu.list"
                                :key="a"
                                :index="index + '-' + a"
                                @click="
                                    goPage(
                                        item.router,
                                        index + 1 + '-' + (a + 1)
                                    )
                                "
                            >
                                <div slot="title" class="active-nav">
                                    <div
                                        slot="title"
                                        class="active-nav"
                                        :class="{
                                            'check-styl':
                                                menuIndex == item.index,
                                        }"
                                    >
                                        <span class="val-name">{{
                                            item.name
                                        }}</span>
                                    </div>
                                </div>
                            </el-menu-item>
                        </el-submenu>
                    </div>
                </el-menu>
                <a
                    class="special-styl"
                    href="http://192.168.1.60:8080/"
                    target="_blank"
                >
                    <i class="el-icon-menu"></i>
                    <div class="self-use">自用区</div>
                </a>
            </el-aside> -->
            <el-main>
                <div class="detail">
                    <!-- <div class="head">
                        <div class="user-styl">
                            <div class="organ-title">机构名称</div>
                            <div class="shun">|</div>
                            <i class="el-icon-s-custom"></i>
                            <div class="organ-name">烟台市委托人</div>
                            <div class="shun">|</div>
                            <i class="el-icon-switch-button"></i>
                        </div>
                        <div class="history-styl">
                            <div
                                class="card-styl"
                                :class="{
                                    'click-styl': currentIndex === index,
                                }"
                                v-for="(item, index) in history"
                                :key="index"
                                @click="clickHistor(index, item)"
                            >
                                <div class="card-name">
                                    {{ tabObj[item].name }}
                                </div>
                                <i
                                    class="el-icon-close"
                                    v-if="currentIndex === index"
                                    @click.stop="deleteHistory(item, index)"
                                ></i>
                                <div
                                    v-else
                                    style="width: 14px; height: 14px"
                                ></div>
                            </div>
                        </div>
                    </div> -->
                    <keep-alive :include="getKeepAliveList">
                        <router-view />
                    </keep-alive>
                </div>
            </el-main>
        </el-container>
        <!-- </el-container> -->
    </div>
</template>

<script type="text/ecmascript-6">
    import { mapGetters, mapMutations } from "vuex";
    export default {
        name: "",
        data() {
            return {
                menuList: [
                    // 菜单列表
                    {
                        mid: "1",
                        name: "开放区",
                        list: [
                            {
                                index: "1-1",
                                name: "申请审核",
                                nav: "applyCheck",
                                router: "/applyCheck",
                            },
                            {
                                index: "1-2",
                                name: "历史报告",
                                nav: "reportQuery",
                                router: "/reportQuery",
                            },
                            {
                                index: "1-3",
                                name: "日志查询",
                                nav: "logSearch",
                                router: "/logSearch",
                            },
                            // {
                            //     index: "1-1",
                            //     name: "山东开放区",
                            //     list: [
                            //         {
                            //             index: "1-1-1",
                            //             name: "保障申请审核",
                            //             nav: "applyCheck",
                            //             router: "/applyCheck",
                            //         },
                            //         {
                            //             index: "1-1-2",
                            //             name: "审批报告查询",
                            //             nav: "reportQuery",
                            //             router: "/reportQuery",
                            //         },
                            //         {
                            //             index: "1-1-3",
                            //             name: "年度复核",
                            //             nav: "yearCheck",
                            //             router: "/yearCheck",
                            //         },
                            //         {
                            //             index: "1-1-4",
                            //             name: "日常查询",
                            //             nav: "daySearch",
                            //             router: "/daySearch",
                            //         },
                            //         {
                            //             index: "1-1-5",
                            //             name: "查询日志管理",
                            //             nav: "logSearch",
                            //             router: "/logSearch",
                            //         },
                            //     ],
                            // },
                        ],
                    },
                ],
            };
        },
        mounted() {},
        computed: {
            ...mapGetters(["userInfo", "history", "currentIndex", "menuIndex"]),

            getKeepAliveList() {
                // console.log("缓存路由", this.history.toString());
                return this.history.toString();
            },
            tabObj() {
                let result = {};
                const arr = JSON.parse(JSON.stringify(this.menuList));
                arr.forEach((item, index) => {
                    if (item.list && item.list.length) {
                        item.list.forEach((item1, inddex) => {
                            result[item1.nav] = item1.name;
                            result[item1.nav] = {
                                name: item1.name,
                                index: item1.index,
                            };

                            // if (item1.list && item1.list.length) {
                            //     item1.list.forEach((val, vindex) => {
                            //         result[val.nav] = val.name;
                            //         result[val.nav] = {
                            //             name: val.name,
                            //             index: val.index,
                            //         };
                            //     });
                            // }
                        });
                    }
                });
                return result;
            },
        },
        watch: {
            // 如果路由有变化，会再次执行该方法
            $route: "historyRouter",
        },
        methods: {
            ...mapMutations([
                "clearToken",
                "setHistory",
                "setCurrent",
                "setMenuIndex",
            ]),

            //缓存路由
            historyRouter(res) {
                //处理不需要缓存的路由
                if (res.meta.isCache === false) return;
                const length = this.history.length;
                // 记录历史路由7
                const item = res.name;
                // 排查之前如果点击该界面，获取之前点击的index并高亮
                let clickIndex = this.history.indexOf(item);
                if (clickIndex > -1) {
                    // 高亮之前重复的路由
                    this.setCurrent(clickIndex);
                } else {
                    if (item == "welcome") {
                        // 欢迎页不计入标签
                        return false;
                    }
                    this.setHistory([...this.history, item]);
                    this.setCurrent(length);
                }
            },
            //删除历史页签
            deleteHistory(item, index) {
                // 删除此窗口
                let arr = this.history;
                arr.splice(index, 1);
                if (arr.length <= 0) {
                    // 最后一项也删除，跳转至欢迎页
                    this.$router.replace({
                        name: "welcome",
                    });
                    this.setCurrent("0-0");
                    this.setMenuIndex("0-0");
                } else {
                    // 如果删除的是当前高亮页并且前方有标签时，高亮向前挪一步,并跳转至前一页
                    if (index !== 0) {
                        const url = this.history[index - 1];
                        this.setMenuIndex(this.tabObj[url].index);
                        this.$router.replace({
                            name: this.history[index - 1],
                        });
                    } else {
                        // 高亮向后挪一步
                        const url = this.history[index];
                        this.setMenuIndex(this.tabObj[url].index);
                        this.$router.replace({
                            name: this.history[index],
                        });
                    }
                }
                this.setHistory(arr);
            },
            //点击历史页签
            clickHistor(index, item) {
                if (this.$route.name == item) return;
                // 设置当前路由高亮
                this.setCurrent(index);
                this.setMenuIndex(this.tabObj[item].index);
                this.$router.replace({
                    name: this.history[index],
                });
            },
            goPage(val, index) {
                if (this.$route.path == val) return;
                this.setMenuIndex(index);
                this.$router.push(val);
            },
            logout() {
                // 退出账号
                this.httpPost(this.$root.managerLogout).then(
                    () => {
                        this.clearToken();
                        this.$router.push("login");
                    },
                    () => {
                        this.$message({
                            message: "退出失败",
                            type: "error",
                        });
                    }
                );
            },
        },
    };
</script>

<style lang="stylus" rel="stylesheet/stylus" scoped>
    @import '~assets/main.styl';

    #app-wrap {
        height: 100%;
    }

    .aside {
        background: theme-color;

        .logo-name {
            background: theme-color;
            flex-x(flex-start, center);
            color: white;
            margin: 30px 0;

            .img-styl {
                width: 50px;
                height: 50px;
                margin: 0 10px 0 20px;
            }

            .logo {
                font-size: 20px;
                font-weight: bold;
            }
        }

        .menu-name {
            font-size: 15px;
            color: #C2C7CA;
        }

        .item-name {
            font-size: 15px;
            color: #C2C7CA;
            margin-left: 20px;
        }

        .val-name {
            margin-left: 80px;
        }

        .active-nav {
            color: #C2C7CA;
            width: 100%;

            i {
                color: #ffffff;
            }
        }

        .check-styl {
            background-color: btn-color;
            color: #ffffff;
        }

        .menu-title {
            font-size: 14px;

            span {
                font-size: 14px;
            }
        }
    }

    .detail {
        height: 100%;
        // padding: 0 15px;
        box-sizing: border-box;

        .head {
            box-sizing: border-box;
            position: fixed;
            top: 0;
            left: 300px;
            right: 0;
            background-color: #ffffff;
            padding: 20px 20px 0px 10px;
            box-shadow: 2px 2px 5px gray9;
            z-index: 100;

            .user-styl {
                flex-x(flex-end);

                i {
                    font-size: 20px;
                    color: btn-color;
                }

                .organ-title {
                    margin-right: 16px;
                    font-size: 12px;
                    color: gray9;
                }

                .organ-name {
                    margin: 0 10px 0 10px;
                }

                .shun {
                    margin-right: 10px;
                    color: gray9;
                }
            }

            .history-styl {
                flex-x(flex-start);
                margin-top: 10px;

                .card-styl {
                    flex-x();
                    margin: 0 2px 0 20px;
                    padding-bottom: 4px;
                    color: gray9;
                    border-bottom: 2px solid #ffffff;

                    .card-name {
                        margin-right: 4px;
                    }
                }

                .click-styl {
                    color: btn-color;
                    border-bottom: 2px solid btn-color;
                    transition-duration: 0.6s;
                }
            }
        }
    }

    >>>.el-menu-item-group__title {
        padding: 0px;
    }

    .el-menu--inline, >>>.el-menu-item {
        padding: 0px;
    }

    .el-container {
        height: 100vh;
    }

    .el-menu-item, >>>.el-submenu__title {
        height: 40px;
        line-height: 40px;
    }

    .el-main {
        background: bk;
        padding: 0;
    }

    .special-styl {
        height: 40px;
        padding: 0 20px;
        line-height: 40px;
        color: rgb(255, 255, 255);
        background-color: rgb(43, 54, 66);
        font-size: 15px;
        display: block;
        flex-x();
        text-decoration: none;

        .el-icon-menu {
            color: #ffffff;
            margin: 0 10px 0 3px;
            font-size: 16px;
        }

        .self-use {
            color: #c2c7ca;
            width: 100%;
        }
    }
</style>
