<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"
    >
        <template #left>
            <div class="back">
                <van-icon @click="back" size="24" name="arrow-left" color="#fff" />
            </div>
            <van-popover
                v-model:show="showPopover"
                placement="bottom-start"
                theme="dark"
                :actions="actions"
                @select="onSelect"
            >
                <template #reference>
                    <van-icon size="24" color="#fff" 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' }
            ]
        };
    },
    watch: {
        $route() {
            if (this.$route.meta.menuPage) {
                this.show = false;
            } else {
                this.show = true;
            }
        }
    },
    mounted() {
        if (this.$route.meta.menuPage) {
            this.show = false;
        } else {
            this.show = true;
        }
        console.log(this.$route);
    },
    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>
