<template>
	<view class="ezy-custom-tabbar">
		<view class="tabbar-item-box" v-for="(item, index) in tabList" :key="index" @click="switchTab(item.path,index)">
			<view class="tabbar-item"
				:style="{ backgroundImage: 'url(' + (currentTab == index ? item.activePath : item.iconPath) + ')' }">
			</view>
		</view>
	</view>
	<tip-big-dialog ref="youkeDialogRef" @confirm-btn="ykConfirm" :imgShow="true"></tip-big-dialog>
	<tishiDlVue ref="popupRef"></tishiDlVue>
</template>

<script>
	import {
		MESSAGE_VISITER_TO_LOGIN
	} from "@/utils/constant.js"
	import tipMiddleDialog from '@/components/dialog/tipMiddleDialog.vue';
	import tipBigDialog from '@/components/dialog/tipBigDialog.vue';
	import {
		useTabBarHistory
	} from '@/utils/emitEvents.js';
	import cacheManager from "@/utils/cacheManager.js";
	import {
		toast,
		getUserIdentity
	} from "@/utils/common";
	import {
		nextTick,
	} from "vue";
	import tishiDlVue from './tishiDl.vue';
	export default {
		components: {
			tipMiddleDialog,
			tipBigDialog,
			tishiDlVue
		},
		data() {
			return {
				tabList: [{
						iconPath: 'static/images/common/tabbar/unselect/xuanke-sj.png',
						activePath: 'static/images/common/tabbar/select/xuanke-sj.png',
						path: `/pages/chanpinXuanze/index`
					},
					{
						iconPath: 'static/images/common/tabbar/unselect/xuexi-sj.png',
						activePath: 'static/images/common/tabbar/select/xuexi-sj.png',
						path: `/pages/chanpinneirong/index`
					},
					{
						iconPath: 'static/images/common/tabbar/unselect/zl-sj.png',
						activePath: 'static/images/common/tabbar/select/zl-sj.png',
						path: '/pages/game/index'
					},
					{
						iconPath: 'static/images/common/tabbar/unselect/wode-sj.png',
						activePath: 'static/images/common/tabbar/select/wode-sj.png',
						path: `/pages/chanpinMy/my`
					},
				],
				currentTab: 0,
				isNavigating: false,
				MESSAGE_VISITER_TO_LOGIN
			};
		},
		props: {
			levelId: {
				type: String,
			},
			typeId: {
				type: String,
			},
			subjectId: {
				type: String,
			},
			currentTabNumber: {
				type: Number,
			},
			tipFlag: {
				type: String,
			},

		},
		methods: {
			// 游客弹窗---确定
			ykConfirm() {
				uni.redirectTo({
					url: '/pages/login/index'
				});
			},
			// 新增：判断是否是目标页面的方法
			isTargetPage(route, tabIndex) {
				const routeMap = {
					0: 'pages/chanpinXuanze/index',
					1: 'pages/chanpinneirong/index',
					2: 'pages/game/index',
					3: 'pages/chanpinMy/my'
				};
				return route === routeMap[tabIndex];
			},
			switchTab(path, index) {

				if (index == this.currentTab) {
					// 同页面不刷新
					return;
				}

				if (path == '/pages/chanpinneirong/index' && !cacheManager.get('auth').chanpinId) {
					this.$refs.popupRef.open();
					return false
				}
				// 新增：检查目标页面是否已经在页面栈中
				const pages = getCurrentPages();
				console.log('pages1', pages);
				let targetPage = null;

				for (let i = pages.length - 1; i >= 0; i--) {
					const page = pages[i];
					console.log('pages2', page);
					// 判断是否是目标页面（这里需要根据你的页面路径判断）
					if (page.route && this.isTargetPage(page.route, index)) {
						targetPage = page;
						break;
					}
				}
				this.currentTab = index
				console.log('targetPage', targetPage);
				if (targetPage) {
					// 如果页面已经在栈中，使用 navigateBack 返回
					const delta = pages.length - pages.indexOf(targetPage) - 1;
					console.log('delta', delta);
					uni.navigateBack({
						delta: delta
					});
				} else {
					console.log('11111');

					this.navigateToEditPage(path);
				}

			},
			navigateToEditPage(path) {
				if (this.isNavigating) {
					uni.showToast({
						title: '正在跳转中...',
						icon: 'none'
					});
					return;
				}
				this.isNavigating = true;
				uni.showLoading({
					title: '加载中'
				}); // 显示Loading

				uni.navigateTo({
					url: path,
					complete: () => {
						uni.hideLoading();
						this.isNavigating = false;
					}
				});
			}
		},
		created() {

			this.currentTab = this.currentTabNumber
		}
	}
</script>
