<template>
	<view class="content">
		<view class="info">
			<view class="logo">
				<image src="../../static/logo.png"></image>
				<view>青测</view>
				<text>V{{phoneInfo.info.version}}</text>
			</view>
			<view class="list">
				<view class="item" @click="handleVersion">
					<view class="left">版本更新</view>
					<view class="right">
						<!--  -->
						<text class="new" v-if="phoneInfo.info.version !== phoneInfo.versionUrl.version">新版本</text>
						<uni-icons type="right"></uni-icons>
					</view>
				</view>
			</view>
		</view>
		<view class="xieyi">
			<text>《隐私声明》</text>
		</view>
	</view>
</template>

<script>
	import {
		mapState
	} from "vuex";
	let dtask;
	export default {
		data() {
			return {
				progress: 0
			}
		},
		computed: {
			...mapState(["phoneInfo"]),
		},
		onLoad() {
			console.log("版本号", this.phoneInfo)
		},
		onBackPress() {
			dtask.onProgressUpdate((res) => {
				if (res.progress != 100) {
					dtask.abort();
				}
			});
		},
		watch: {
			progress() {
				let that = this
				console.log("devicesList", that.progress)
			},
		},
		methods: {
			handleVersion() {
				let that = this
				if (that.phoneInfo.info.version === that.phoneInfo.versionUrl.version) {
					that.$tools.msg("已经是最新版本了！")
				} else {
					if (that.phoneInfo.platform === 'android') {
						uni.showLoading({
							title: '正在升级,请耐心等待'
						})
						uni.setStorageSync('VERSION', that.phoneInfo.versionUrl.version)
						that.downloadNewVersion(that.phoneInfo.versionUrl.url);
					} else {
						uni.showModal({
							title: '发现新版本 ' + that.phoneInfo.versionUrl.version,
							content: '请到App store进行升级',
							showCancel: false
						})
					}
				}
			},
			downloadNewVersion(downloadUrl) {
				let that = this
				dtask = uni.downloadFile({
					url: downloadUrl,
					success: (downloadRes) => {
						if (downloadRes.statusCode === 200) {
							uni.hideLoading();
							plus.runtime.install(
								downloadRes.tempFilePath, {
									force: true
								},
								function() {
									console.log('install success...');
									plus.runtime.restart();
								},
								function(e) {
									console.error('install fail...');
								});
						}
					},
					fail: () => {
						uni.showToast({
							title: '升级失败',
							icon: 'none'
						});
					}
				});
				dtask.onProgressUpdate((res) => {
					that.progress = res.progress
					console.log('下载进度' + that.progress + '%');
				});
			},
		}
	}
</script>

<style scoped lang="scss">
	.content {
		min-height: calc(100vh - 30px);
		padding: 15px;
		display: flex;
		align-items: center;
		flex-direction: column;
		justify-content: space-between;
		background-color: #f7f7f7;
	}

	.info {
		width: 100%;
	}

	.logo {
		width: 100%;
		text-align: center;
		font-size: 20px;
		line-height: 30px;

		image {
			width: 60px;
			height: 60px;
		}

		text {
			display: block;
			font-size: 14px;
			color: #999;
		}
	}

	.list {
		width: calc(100% - 20px);
		border-radius: 10px;
		padding: 0 10px;
		background-color: #fff;
		margin: 50px 0;

		.item {
			width: 100%;
			height: 50px;
			line-height: 50px;
			display: flex;
			justify-content: space-between;

			.new {
				color: #fff;
				background-color: red;
				font-size: 12px;
				border-radius: 10px;
				padding: 3px 5px;
			}
		}
	}

	.xieyi {
		color: $textcolor;
	}
</style>