<template>
    <van-nav-bar
        fixed
        left-text=" "
        right-text=" "
        :left-arrow="false"
        v-if="show"
        z-index="20"
        placeholder
        :border="false"
        id="navBar"
        :class="{ dark: tabColor }"
        ref="navBar"
    >
        <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>

        <template #title>
            <span class="title" :style="{ color: fontColor || $colors.font0 }">{{ title }}</span>
        </template>
    </van-nav-bar>
</template>

<script>
import { mapState } from 'vuex';
import { useCssVar } from '@vueuse/core';
export default {
    inject: ['setKeeps', 'safeTop', 'barHeight', 'keeps'],
    data() {
        return {
            show: false,
            showPopover: false,
            tabColor: '',
            title: ''
        };
    },
    computed: {
        ...mapState(['showConsole']),
        fontColor() {
            return this.tabColor ? '#fff' : '';
        },
        actions() {
            if (this.showConsole) {
                return [
                    { 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' },
                    { text: '刷新', icon: 'replay', value: 'reload' }
                ];
            } else {
                return [
                    { 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' }
                ];
            }
        },
        height() {
            return this.barHeight;
        }
    },
    watch: {
        $route() {
            if (this.$route.meta.menuPage) {
                this.show = false;
            } else {
                this.show = true;
            }
            this.getColor();
            this.setTitle();
        },
        tabColor() {
            this.$nextTick(() => {
                if (this.$el) {
                    let children = this.$el.children;
                    if (children && children.length > 0) {
                        children[0].style.backgroundColor = this.tabColor;
                    }
                }
            });
        },
        show() {
            this.safeTop = useCssVar('--safe-top').value;
        }
    },
    mounted() {
        if (this.$route.meta.menuPage) {
            this.show = false;
        } else {
            this.show = true;
        }
        this.getColor();
        this.setTitle();
    },
    methods: {
        setTitle(title) {
            console.log(this.$route.meta.title);
            if (this.$route.meta.title) {
                this.title = this.$route.meta.title;
            } else if (title) {
                this.title = title;
            } else {
                this.title = '';
            }
        },
        getColor(color = '') {
            if (this.$route.meta.tabColor) {
                if (window.cordova && StatusBar && StatusBar.isVisible) {
                    StatusBar.styleLightContent();
                }
                this.tabColor = this.$route.meta.tabColor;
            } else if (color) {
                if (window.cordova && StatusBar && StatusBar.isVisible) {
                    StatusBar.styleLightContent();
                }
                this.tabColor = color;
            } else {
                if (window.cordova && StatusBar && StatusBar.isVisible) {
                    StatusBar.styleDefault();
                }
                this.tabColor = '';
            }
        },
        onSelect(val) {
            console.log(val);
            if (val.value === 'back') {
                this.$router.go(-1);
            } else if (val.value === 'reload') {
                location.reload();
            } else {
                setTimeout(() => {
                    this.setKeeps([], true, true);
                }, 10);
                this.$router.push(`/${this.$route.params.companyId}${val.value}`);
            }
        },
        back() {
            if (window.history.length <= 1) {
                this.$router.push({ path: '/' + this.$route.params.companyId + '/' });
                return false;
            } else {
                this.$router.go(-1);
            }
            //上面都没执行就说明卡在当前页不是最后一条， histroy记录数量大于1，又没有回退记录，只能返回首页，
            //如果上面都执行了 页面都跳走了，这个也就不用管了
            // setTimeout(() => {
            //     this.$router.push({ path: '/' });
            // }, 500);
        }
    }
};
</script>

<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;
    }
}
.back {
    padding-right: 12px;
}
.title {
    font-size: 16px;
    font-weight: bold;
}
</style>
