<template>
    <view>
        <cover-view id="customTabBar" class="tab-bar" v-if="canCustom">
            <cover-view class="tab-bar-border"></cover-view>
            <cover-view v-if="item.visiable" class="tab-bar-item" :data-path="item.pagePath" :data-index="index" @tap="switchTab" v-for="(item, index) in list" :key="index">
                <cover-view class="item-dot" v-if="item.showDot"></cover-view>

                <cover-view class="item-badge" v-if="item.badge">{{ item.badge }}</cover-view>

                <cover-image :src="selected === index ? item.selectedIconPath : item.iconPath"></cover-image>

                <cover-view :style="'color: ' + (selected === index ? selectedColor : color) + ';font-size: 24rpx;'">{{ item.text }}</cover-view>
            </cover-view>
        </cover-view>
    </view>
</template>

<script>
const app = getApp();
export default {
    data() {
        return {
            selected: 0,
            color: '#B6B6B6',
            selectedColor: '#67A4FD',
            canCustom: true,

            list: [
                {
                    pagePath: '/pages/tabBar/index/index',
                    iconPath: '/static/images/tab_home_pre.png',
                    selectedIconPath: '/static/images/tab_home.png',
                    text: '首页',
                    showDot: false,
                    badge: '',
                    visiable: true
                },
                {
                    pagePath: '/pages/tabBar/guidanceDiagnosis/guidanceDiagnosis',
                    iconPath: '/static/images/guidance_diagnosis_pre.png',
                    selectedIconPath: '/static/images/guidance_diagnosis.png',
                    text: '导诊',
                    showDot: false,
                    badge: '',
                    visiable: false
                },
                {
                    pagePath: '/pages/tabBar/order/order',
                    iconPath: '/static/images/tab_order_pre.png',
                    selectedIconPath: '/static/images/tab_order.png',
                    text: '账单',
                    showDot: false,
                    badge: '',
                    visiable: true
                },
                {
                    pagePath: '/pages/tabBar/message/message',
                    iconPath: '/static/images/tab_message_pre.png',
                    selectedIconPath: '/static/images/tab_message.png',
                    text: '消息',
                    showDot: false,
                    badge: '',
                    visiable: true
                },
                {
                    pagePath: '/pages/tabBar/mine/mine',
                    iconPath: '/static/images/tab_me_pre.png',
                    selectedIconPath: '/static/images/tab_me.png',
                    text: '我的',
                    showDot: false,
                    badge: '',
                    visiable: true
                }
            ],

            badge: ''
        };
    },
    mounted() {
        // 处理小程序 attached 生命周期
        this.attached();
    },
    methods: {
        attached() {
            this.updateTabbarItem();
        },

        updateTabbarItem() {
            const userData = uni.getStorageSync('userData');
            const { list } = this;
            if (userData && app.globalData.tabBarSettings.tabBarVisiableHospital.indexOf(userData.hisHospID) > -1) {
                const { triageUnreadMessageCount } = app.globalData.tabBarSettings.tabBarBadge;
                console.log('triageUnreadMessageCount === ', triageUnreadMessageCount);
                list[1].visiable = true;
                list[1].badge = triageUnreadMessageCount > 0 ? triageUnreadMessageCount : '';
            } else {
                list[1].visiable = false;
            }
            this.setData({
                list
            });
        },

        updateBadge() {
            const app = getApp();
            const { messageCount, triageUnreadMessageCount } = app.globalData.tabBarBadge;

            // 更新消息tab的badge
            this.setData({
                'list[3].badge': messageCount > 0 ? messageCount : '',
                'list[1].badge': triageUnreadMessageCount > 0 ? triageUnreadMessageCount : ''
            });
        },

        switchTab: function (e) {
            let { index, path } = e.currentTarget.dataset;
            console.log(index);
            let { selected } = this;
            if (selected === index) {
                return;
            }
            this.setData({
                selected: index
            });
            uni.reLaunch({
                url: path
            });
        }
    },
    created: function () {}
};
</script>
<style>
.tab-bar {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    height: 59px;
    background: white;
    display: flex;
    padding-bottom: env(safe-area-inset-bottom);
    z-index: 10000;
}

.tab-bar-border {
    background-color: #f7f7f7;
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 1px;
    transform: scaleY(0.5);
}

.tab-bar-item {
    flex: 1;
    text-align: center;
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
    position: relative;
}

.tab-bar-item cover-image {
    width: 20px;
    height: 20px;
    margin-bottom: 8rpx;
}

.tab-bar-item cover-view {
    font-size: 10px;
}

/* 小红点样式 */
.item-dot {
    position: absolute;
    top: 2px;
    right: 12px; /* 根据实际布局微调 */
    width: 8px;
    height: 8px;
    background-color: #fa5151;
    border-radius: 50%;
}

/* 数字 Badge 样式 */
.item-badge {
    position: absolute;
    top: 0px;
    right: 12px; /* 根据实际布局微调 */
    background-color: #fa5151;
    color: white;
    font-size: 10px;
    min-width: 16px;
    height: 16px;
    line-height: 16px;
    border-radius: 8px;
    padding: 0 4px;
    box-sizing: border-box;
}
</style>
