<script setup>
	/**
	 * app版本更新弹窗
	 */

	import {
		defineExpose,
		ref,
		reactive,
		getCurrentInstance,
		computed,
		nextTick,
	} from 'vue'
	// 工具库
	import util from '@/common/js/util';
	// api
	import api from '@/api/index.js'

	const {
		proxy
	} = getCurrentInstance()
	// 云端版本信息
	const versionCloud = reactive({})
	// 程序版本信息
	const versionkApp = reactive({})

	// 初始化
	function init() {
		// 获取最新版本信息
		api.getAppVersion().then(rs => {
			if (rs.code == 200) {
				console.log('getAppVersion', rs)
				// 结果
				const result = rs.data
				let system = uni.getSystemInfoSync()
				Object.assign(versionCloud, result)
				Object.assign(versionkApp, system)
				console.log('if', system.appVersionCode, result.versionCode)
				// 如果需要更新 则打开手机浏览器
				if (system.appVersionCode < result.versionCode || true) open()
				return
			}
		})
	}

	// 打开弹窗
	function open() {
		proxy.$refs.update.open()
	}

	// 关闭弹窗
	function close() {
		proxy.$refs.update.close()
	}

	// 更新弹窗点击确认
	function handleConfirm() {
		// 下载地址
		// let downloadUrl = 'https://www.doubao.com/chat/'
		let downloadUrl = versionCloud.downloadUrl
		plus.runtime.openURL(downloadUrl, function(res) {
			console.log('打开浏览器结果：', res);
		}, function(err) {
			console.log('打开浏览器失败：', err);
		});
	}

	defineExpose({
		init,
		open,
		close,
	})
</script>

<template>
	<uni-popup ref="update" type="center">
		<view class="updateAlt popMid ptb30 plr30 bfff">
			<view class="header">
				<view class="df aic">
					<image src="/static/logo.png" mode="aspectFit" class="wh120 br10" />
				</view>
				<text class="mt30 f48">九亿有新版本啦~</text>
			</view>

			<view class="main mtb50 mlr20">
				<text class="f32 c333">最新版本为{{versionCloud.versionName}}</text>
				<view class="content mt20 ptb30 plr20">
					<text class="f30 c333">更新说明：</text>
					<text class="mt10 f26 c666">{{versionCloud.updateContent}}</text>
				</view>
			</view>

			<view class="btn focus bar mlr50">
				<text class="tac cfff f28" @click="handleConfirm">更新</text>
			</view>
		</view>
	</uni-popup>
</template>

<style lang="scss">
	// 更新弹窗
	.updateAlt {
		width: 600rpx;

		// 内容
		.content {
			background-color: #f8f8f8;
			border-radius: 10rpx;
		}
	}
</style>