<script lang="uts">
  // #ifdef APP-ANDROID || APP-HARMONY
	let firstBackTime = 0
  // #endif
	export default {
		onLaunch: function () {
			console.log('App Launch')
			// 检查是否已选择模式
			this.checkModeSelection()
		},
		onShow: function () {
			console.log('App Show')
		},
		onHide: function () {
			console.log('App Hide')
		},
		// #ifdef APP-ANDROID || APP-HARMONY
		onLastPageBackPress: function () {
			console.log('App LastPageBackPress')
			if (firstBackTime == 0) {
				uni.showToast({
					title: '再按一次退出应用',
					position: 'bottom',
				})
				firstBackTime = Date.now()
				setTimeout(() => {
					firstBackTime = 0
				}, 2000)
			} else if (Date.now() - firstBackTime < 2000) {
				firstBackTime = Date.now()
				uni.exit()
			}
		},
		// #endif
		onExit: function () {
			console.log('App Exit')
		},
		methods: {
			// 检查并选择模式
			checkModeSelection() {
				const mode = uni.getStorageSync('userMode')
				if (!mode) {
					this.showModeSelector()
				}
			},
			// 显示模式选择器
			showModeSelector() {
				uni.showActionSheet({
					itemList: ['员工模式 (17857100375)', '访客模式 (17857100377)'],
					success: (res) => {
						let wechatId = ''
						let userMode = ''
						let phone = ''
						if (res.tapIndex === 0) {
							wechatId = '17857100375'
							phone = '17857100375'
							userMode = 'staff'
						} else {
							wechatId = '17857100377'
							phone = '17857100377'
							userMode = 'guest'
						}
						// 存储选择
						uni.setStorageSync('userMode', userMode)
						uni.setStorageSync('wechatId', wechatId)
						uni.setStorageSync('phone', phone)
						console.log('已选择模式:', userMode, 'wechatId:', wechatId)
						uni.showToast({
							title: userMode === 'staff' ? '员工模式' : '访客模式',
							icon: 'success'
						})
					},
					fail: () => {
						// 用户取消，默认使用访客模式
						uni.setStorageSync('userMode', 'guest')
						uni.setStorageSync('wechatId', '17857100377')
						console.log('默认使用访客模式')
					}
				})
			}
		}
	}
</script>

<style>
	/*每个页面公共css */
	view {
		border: none;
		background-color: transparent;
		box-sizing: border-box;
	}
	
	.uni-row {
		flex-direction: row;
	}

	.uni-column {
		flex-direction: column;
	}
</style>