<template>
    <van-nav-bar
        :title="title"
        fixed
        left-text=" "
        right-text=" "
        :left-arrow="false"
        v-if="show"
        z-index="20"
        placeholder
        id="navBar"
        :border="false"
        :class="{ dark: tabColor }"
    >
        <template #left>
            <div class="back">
                <van-icon @click="back" size="24" name="arrow-left" :color="fontColor || $colors.font0" />
            </div>
            <van-popover
                v-model:show="showPopover"
                placement="bottom-start"
                theme="dark"
                :actions="actions"
                @select="onSelect"
            >
                <template #reference>
                    <van-icon size="24" :color="fontColor || $colors.font0" name="bars" />
                </template>
            </van-popover>
        </template>
    </van-nav-bar>
</template>

<script>
export default {
    data() {
        return {
            show: false,
            showPopover: false,
            actions: [
                { text: '首页', icon: require('@assets/tabbar_icon_01_pre.png'), value: '/home' },
                { text: '发现', icon: require('@assets/tabbar_icon_02_pre.png'), value: '/discover' },
                { text: '展览室', icon: require('@assets/tabbar_icon_03_pre.png'), value: '/store' },
                { text: '我的', icon: require('@assets/tabbar_icon_04_pre.png'), value: '/mine' }
            ],
            tabColor: '#201B15',
            title: ''
        };
    },
    computed: {
        fontColor() {
            return this.tabColor ? '#fff' : '';
        }
    },
    watch: {
        $route() {
            if (this.$route.meta.menuPage) {
                this.show = false;
            } else {
                this.show = true;
            }
            if (window.cordova && StatusBar && StatusBar.isVisible) {
                StatusBar.backgroundColorByHexString(this.$route.meta.tabColor);
                StatusBar.styleLightContent();
            }
            this.getTitle();
            // if (this.$route.meta.tabColor) {
            //     if (window.cordova && StatusBar && StatusBar.isVisible) {
            //         StatusBar.backgroundColorByHexString(this.$route.meta.tabColor);
            //         StatusBar.styleLightContent();
            //     }
            //     this.tabColor = this.$route.meta.tabColor;
            // } else {
            //     if (window.cordova && StatusBar && StatusBar.isVisible) {
            //         StatusBar.backgroundColorByHexString('#ffffff');
            //         StatusBar.styleDefault();
            //     }
            //     this.tabColor = '';
            // }
        },
        tabColor() {
            this.$nextTick(() => {
                if (this.$el) {
                    let children = this.$el.children;
                    if (children && children.length > 0) {
                        children[0].style.backgroundColor = this.tabColor;
                    }
                }
            });
        }
    },
    mounted() {
        if (window.cordova && StatusBar && StatusBar.isVisible) {
            StatusBar.backgroundColorByHexString(this.$route.meta.tabColor);
            StatusBar.styleLightContent();
        }
        if (this.$route.meta.menuPage) {
            this.show = false;
        } else {
            this.show = true;
        }
        this.getTitle();

        this.emitter.on('setAppTitle', this.getTitle);
    },
    methods: {
        getTitle(title) {
            if (this.$route.meta.title) {
                this.title = this.$route.meta.title;
            } else {
                this.title = title;
            }
        },
        onSelect(val) {
            if (val.value === 'back') {
                this.$router.go(-1);
            } else {
                this.$router.push(val.value);
            }
        },
        back() {
            if (window.history.length <= 1) {
                this.$router.push({ path: '/' });
                return false;
            } else {
                this.$router.go(-1);
            }
            //上面都没执行就说明卡在当前页不是最后一条， histroy记录数量大于1，又没有回退记录，只能返回首页，
            //如果上面都执行了 页面都跳走了，这个也就不用管了
            // setTimeout(() => {
            //     this.$router.push({ path: '/' });
            // }, 500);
        }
    }
};
</script>
<style lang="less" scoped>
.back {
    padding-right: 12px;
}
.dark {
    background-color: #201B15;
}

/deep/.van-nav-bar__title {
    font-size: 17px;
}
</style>
<style lang="less">
#navBar {
    .van-nav-bar__content {
        padding-top: var(--safe-top);
    }
    .van-nav-bar__left {
        top: var(--safe-top) !important;
    }
    .van-nav-bar {
        line-height: initial !important;
    }
}
</style>
