<template>
	<view class="tab-bar" :style="{ backgroundColor: mainColor }">
		<!--  循环每个 item 菜单 -->
		<view v-for="(item,index) in tabBarList" :key="index"
			:class="{'tab-bar-item': true,currentTar: selected == item.id}" @click="switchTab(item, index)">
			<view class="tab_text" :style="{color: selected == index ? selectedColor : color}">
				<image class="tab_img" :src="selected == index ? item.selectedIconPath : item.iconPath"></image>
				<view>{{ item.text }}</view>
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		onMounted,
		defineProps,
		ref
	} from 'vue'
	import {
		useColortore
	} from '@/store/index.js';
	import {
		useI18n
	} from 'vue-i18n'
	const {
		locale,
		t
	} = useI18n();

	const mainColor = ref('')
	onMounted(() => {
		const colorStore = useColortore()
		mainColor.value = colorStore.mainRed
	})

	// 子组件传递参数
	const props = defineProps({
		selected: {
			type: String,
			default: '0'
		}
	})
	let color = ref('#ffffff')
	let selectedColor = ref('#ffffff')
	let tabBarList = ref([{
		"id": 0,
		"pagePath": "/pages/index/index",
		"iconPath": "../../static/tabBar/home.png",
		"selectedIconPath": "../../static/tabBar/home-selected.png",
		"text": t("tabbar.home")
	}, {
		"id": 1,
		"pagePath": "/pages/project/index",
		"iconPath": "../../static/tabBar/project.png",
		"selectedIconPath": "../../static/tabBar/project-selected.png",
		"text": t("tabbar.project")
	}, {
		"id": 2,
		"pagePath": "/pages/user/index",
		"iconPath": "../../static/tabBar/user.png",
		"selectedIconPath": "../../static/tabBar/user-selected.png",
		"text": t("tabbar.me")
	}])
	// 跳转tabBar菜单栏
	const switchTab = (item) => {
		let url = item.pagePath;
		uni.switchTab({
			url
		})
	}

	// 隐藏原生TabBar
	uni.hideTabBar();
</script>

<style lang="scss" scoped>
	// 外部装修
	.tab-bar {
		position: fixed;
		bottom: 0;
		width: 100vw;
		height: 100rpx;
		// background: #EC0000;
		padding: 20rpx 0;
		display: flex;
		justify-content: center;
		align-items: center;
		z-index: 9999;

		.currentTar {
			border-radius: 15rpx;
			transition: all 0.5s ease-in-out;
		}

		.tab-bar-item {
			//flex: 0.5;
			text-align: center;
			display: flex;
			justify-content: center;
			align-items: center;
			flex-direction: column;
			width: 150rpx;
			background-color: transparent;
			transition: all 0.5s ease-in-out;
			margin: auto;

			.tab_img {
				width: 50rpx;
				height: 50rpx;
			}

			.tab_text {
				font-size: 24rpx;
				margin-top: 10rpx;
				flex: 1;
			}
		}
	}
</style>