<template>
    <van-nav-bar
        title=""
        fixed
        left-text=" "
        right-text=" "
        :left-arrow="false"
        v-if="show"
        z-index="20"
        placeholder
        safe-area-inset-top
        :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/menu1.png'), value: '/home' },
                { text: '发现', icon: require('@assets/menu2.png'), value: '/discover' },
                { text: '藏品室', icon: require('@assets/menu3.png'), value: '/store' },
                { text: '我的', icon: require('@assets/menu4.png'), value: '/mine' }
            ],
            tabColor: ''
        };
    },
    computed: {
        fontColor() {
            return this.tabColor ? '#fff' : '';
        }
    },
    watch: {
        $route() {
            if (this.$route.meta.menuPage) {
                this.show = false;
            } else {
                this.show = true;
            }
            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 (this.$route.meta.menuPage) {
            this.show = false;
        } else {
            this.show = true;
        }
    },
    methods: {
        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">
.back {
    padding-right: 12px;
}
</style>
