<template>
	<view class="container">
		<view class="pages">
			<Home ref="homeRef" v-if="current==0" />
			<Equipment ref="equipmentRef" v-if="current==1" />
			<Commodity ref="commodityRef" v-if="current==2" />
			<Account ref="accountRef" v-if="current==3" />
		</view>

		<u-tabbar v-if="menu&&menu.length>0" v-model="current" :list="list" :mid-button="false"></u-tabbar>
	</view>
</template>

<script setup>
	import Home from './components/home.vue'
	import Equipment from './components/equipment.vue'
	import Commodity from './components/commodity.vue'
	import Account from './components/account.vue'
	import {
		watch,
		computed,
		onBeforeMount,
		onMounted,
		reactive,
		toRefs,
		ref
	} from "vue";
	import {
		onShow,
		onLoad
	} from '@dcloudio/uni-app'

	import usePermissionStore from '@/stores/permission.js'

	const current = ref(0)
	const client = ref(null)
	const tabName = ref(null)

	const list = ref([{
			iconPath: "home",
			selectedIconPath: "home-fill",
			text: '平台运维',
		},
		{
			iconPath: "photo",
			selectedIconPath: "photo-fill",
			text: '质检出场',
		},
		{
			iconPath: "play-right",
			selectedIconPath: "play-right-fill",
			text: '数据中心',
		},
		{
			iconPath: "account",
			selectedIconPath: "account-fill",
			text: '商户运营',
		},
	])

	onShow(() => {
		console.log('onShow')
		// 版本自动更新代码
		const updateManager = wx.getUpdateManager()
		updateManager.onUpdateReady(function() {
			wx.showModal({
				title: '更新检测',
				content: '检测到新版本，是否重启小程序？',
				success: function(res) {
					if (res.confirm) {
						// 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
						updateManager.applyUpdate()
					}
				}
			})
		})
		updateManager.onUpdateFailed(function() {
			// 新的版本下载失败
			wx.showModal({
				title: '已有新版本咯',
				content: '请您删除当前小程序，重新打开呦~',
				showCancel: false
			})
		})
	})

	onLoad((o) => {
		console.log('onLoad', o)
		if (o.tabName) { //公库添加商品到私库完毕，显示商品界面
			tabName.value = o.tabName;
			if (menu.value && menu.value.length > 0) {
				menu.value.forEach((item, index) => {
					if (item.name == o.tabName) {
						tcurrent.value = index
					}
				})
			}
		}
	})

	const menu = computed(() => JSON.parse(usePermissionStore().permissions_menu))

	watch(
		() => menu,
		(menu, prevMenu) => {
			if (menu.length > 0) {
				tabName.value = menu[0].name
			} else {
				tabName.value = null
			}
		}
	)

	function tabChange(e) {
		current.value = e
	}

	function tabClick(e) {
		tabName.value = e.name
	}
</script>

<style lang="scss">
	.container {
		.pages {
			height: 100%;
		}

		.u-page__item__slot-icon {
			width: 44rpx;
			height: 44rpx;
		}
	}
</style>