<template>
	<view class="setting-page">
		<ch-nav-bar :height="66" title="设置"></ch-nav-bar>
		<uni-list>
			<uni-list-item title="版本更新" show-arrow="true" clickable @click="checkAppUpdate">
				<template v-slot:footer>
					<uni-tag v-if="hadUpdate" text="新版本" type="primary"></uni-tag>
					<text v-else>{{ appInfo.appWgtVersion }}</text>
				</template>
			</uni-list-item>
			<uni-list-item title="家长二维码" show-arrow="true" clickable @click="checkQrcode">
			</uni-list-item>
			<uni-list-item title="清除数据" show-arrow="true" clickable @click="clearStorage">
			</uni-list-item>
		</uni-list>
		<uni-popup ref="qrcodePopupRef" type="dialog">
			<image :src="userInfo.qrcode" mode="aspectFit"></image>
		</uni-popup>
	</view>
</template>

<script setup>
import checkUpdate from '@/uni_modules/uni-upgrade-center-app/utils/check-update';
import { reactive, ref, toRefs } from 'vue';
import { useSystemStore } from '@/store/system.js';
import { useUserStore } from '@/store/user.js';

const sysStore = useSystemStore();
const userStore = useUserStore();
const qrcodePopupRef = ref(null)

const data = reactive({
	hadUpdate: false,
	appInfo: sysStore.appInfo,
	userInfo: userStore.userInfo,
	count: 0
})

const { hadUpdate, appInfo, userInfo, count } = toRefs(data);

// 检查更新
function checkAppUpdate () {
	// #ifdef APP
	uni.showLoading({
		title: '检查更新...'
	})
	checkUpdate().then(res => {
		console.log('检查更新结果：', res)
		if (res.code === 0) {
			uni.showToast({
				icon: 'none',
				title: '暂无更新'
			})
		}
	}).catch(error => { 
		console.log("检查更新错误：", error)
	}).finally(() => {
		uni.hideLoading()
	})
	// #endif
}

function checkQrcode () {
	qrcodePopupRef.value.open('center')
}

function clearStorage () {
	count.value++;
	if (count.value < 3) return; 
	uni.clearStorageSync();
	uni.showToast({
		title: '清除成功',
		success() {
			uni.reLaunch({
				url: '/pages/login/login'
			})
		}
	})
}
</script>

<style lang="scss">

</style>
