<script lang="ts">
import { storeToRefs } from 'pinia';
import userStore from '@/store/user';

const storeQrcode = 'https://zyt.youcan365.com/store?'; // 扫码店铺二维码进入小程序场景
export default {
	globalData: {
		elderMode: false, // 全局长辈模式状态
		confirmPopupData: null, // 全局确认弹窗数据
		showConfirmPopup: false // 是否显示确认弹窗
	},
	onLaunch: function (options) {
		// 读取长辈模式设置
		const savedElderMode = uni.getStorageSync('elderMode')
		if (savedElderMode !== null && savedElderMode !== undefined) {
			this.globalData.elderMode = savedElderMode
		}
		// #ifdef MP-ALIPAY
		const updateManager = my.getUpdateManager()
		updateManager.onCheckForUpdate(function (res) {
			// 请求完新版本信息的回调
			console.log(res.hasUpdate)
		})
		updateManager.onUpdateReady(function () {
			my.confirm({
				title: '更新提示',
				content: '新版本已经准备好，是否重启应用？',
				success: function (res) {
					if (res.confirm) {
						// 新的版本已经下载好，调用 applyUpdate 应用新版本并重启 
						updateManager.applyUpdate()
					}
				}
			})
		})
		updateManager.onUpdateFailed(function () {
			// 新版本下载失败
		})
		// #endif
	},
	onShow(options) {
		console.log('onShow.....')
		console.log('options:', options)
		this.setStore(options);
		this.autoLogin()
	},
	methods: {
		setStore(options) {
			// const storeQrcode = 'https://zyt.youcan365.com/rstore?'; // 扫码店铺二维码进入小程序场景
			const qrcode = options.query && typeof options.query.qrCode == 'string' ? options.query.qrCode : '';
			// 扫码店铺二维码进入小程序场景
			if (qrcode.startsWith(storeQrcode)) {
				const { storeId, launchOptions } = storeToRefs(userStore())
				const match = qrcode.match(/[?&]storeId=(\w+)/);
				const store_id = match && match[1] ? match[1] : '';
				storeId.value = store_id;
				launchOptions.value = {
					scene: options.scene,
					storeId: store_id
				}
			}
		},
		// 更新全局长辈模式状态
		updateElderMode(isElder) {
			// 记录到全局状态和本地存储
			this.globalData.elderMode = isElder
			uni.setStorageSync('elderMode', isElder)

			// 触发全局事件，通知所有页面更新
			uni.$emit('elderModeChanged', isElder)
		},
		autoLogin() {
			const deviceInfo = uni.getSystemInfoSync()
			// 避免开发过程中 频繁改动代码导致频繁触发 进而触发my.getAuthCode频繁调用(频繁调用会导致受限)
			if (deviceInfo && deviceInfo.platform === 'devtools') return;
			if (uni.getStorageSync("hasAuthed")) {
				try {
					const { logined } = storeToRefs(userStore())
					const { login } = userStore();
					if (!logined.value) {
						login(false)
					}
				} catch (e) { }
			};
		}
	}
};
</script>
<style lang="scss">
/* 导入适老化模式的样式 */
@import "./elder-mode.scss";

/* 应用全局样式 */
page {
	font-size: $uni-font-size-base;
}

/* common css for page */
uni-page-body,
html,
body,
page {
	width: 100% !important;
	height: 100% !important;
	/* #ifdef MP-ALIPAY */
	height: 100vh !important;
	/* #endif */
	overflow: hidden;
}

.safearet-bottom {
	flex-shrink: 0;
	height: env(safe-area-inset-bottom);
}

/* 全局长辈模式样式应用 */
/* 当任何页面的根元素有elder-mode-active类时，应用适老化样式 */
body {

	&.elder-mode-active,
	.elder-mode-active {
		@extend .elder-mode-active;
	}
}

.shadow-up {
	box-shadow: rgba(0, 0, 0, 0) 0px 0px 0px 0px, rgba(0, 0, 0, 0) 0px 0px 0px 0px,
		rgba(0, 0, 0, 0.1) 0px -10px 15px -3px, rgba(0, 0, 0, 0.1) 0px -4px 6px -4px;
}


.shadow-lg {
	box-shadow: 0 10px 15px -3px rgb(0 0 0 / 0.1), 0 4px 6px -4px rgb(0 0 0 / 0.1);
}

.relative {
	position: relative;
}

.flex {
	display: flex;
}

.flex-col {
	flex-direction: column;
}

.flex-wrap {
	flex-wrap: wrap;
}

.flex-grow {
	flex-grow: 1;
}

.flex-shrink-0 {
	flex-shrink: 0;
}

.items-center {
	align-items: center;
}

.justify-center {
	justify-content: center;
}

.justify-between {
	justify-content: space-between;
}

.justify-around {
	justify-content: space-around;
}

.justify-end {
	justify-content: flex-end;
}

.bg-gray {
	background: #f5f5f5;
}

.bg-primary {
	background: $theme-brown-primary;
}

.bg-success {
	background: green;
}

.bg-white {
	background: #fff;
}

.py-5 {
	padding-top: 10rpx;
	padding-bottom: 10rpx;
}

.p-15 {
	padding: 30rpx;
}

.p-10 {
	padding: 20rpx;
}

.px-10 {
	padding-left: 20rpx;
	padding-right: 20rpx;
}

.px-15 {
	padding-left: 30rpx;
	padding-right: 30rpx;
}

.pt-5 {
	padding-top: 10rpx;
}

.break-all {
	word-break: break-all;
}

.pb-5 {
	padding-bottom: 10rpx;
}

.pb-10 {
	padding-bottom: 20rpx;
}

.py-10 {
	padding-top: 20rpx;
	padding-bottom: 20rpx;
}

.py-12 {
	padding-top: 24rpx;
	padding-bottom: 24rpx;
}

.py-15 {
	padding-top: 30rpx;
	padding-bottom: 30rpx;
}

.mr-5 {
	margin-right: 10rpx;
}

.mr-10 {
	margin-right: 20rpx;
}

.mb-10 {
	margin-bottom: 20rpx;
}

.mt-10 {
	margin-top: 20rpx;
}

.mt-15 {
	margin-top: 30rpx;
}

.mt-12 {
	margin-top: 24rpx;
}

.mx-10 {
	margin-left: 20rpx;
	margin-right: 20rpx;
}

.mx-auto {
	margin-left: auto;
	margin-right: auto;
}

.rounded {
	border-radius: 16rpx;
}

.rounded-sm {
	border-radius: 8rpx;
}

.rounded-full {
	border-radius: 999px;
}

.text-center {
	text-align: center;
}

.text-right {
	text-align: right;
}

.text-gray {
	color: #999;
}

.text-black {
	color: #000;
}

.text-dark {
	color: #333;
}

.text-danger {
	color: #f56c6c;
}

.text-primary {
	color: $theme-brown-primary;
}

.text-success {
	color: #67c23a;
}

.text-white {
	color: #fff;
}

.text-warning {
	color: #f56c6c;
}

.text-xs {
	font-size: var(--text-xs);
}

.text-sm {
	font-size: var(--text-sm);
}

.text-base {
	font-size: var(--text-base);
}

.text-lg {
	font-size: var(--text-lg);
}

.text-xl {
	font-size: var(--text-xl);
}

.leading-normal {
	line-height: 1.5;
}

.border {
	border: 1px solid #eee;
}

.border-b {
	border-bottom: 1px solid #eee;
}

.border-primary {
	border: 1px solid $theme-brown-primary;
}

.font-semibold {
	font-weight: bold;
}

.truncate {
	white-space: nowrap;
	overflow: hidden;
	text-overflow: ellipsis;
}


.w-0 {
	width: 0;
}

.w-full {
	width: 100%;
}

.overflow-hidden {
	overflow: hidden;
}

.border-box {
	box-sizing: border-box;
}
</style>
