<template>
	<view class="content">
		<view class="info">
			<view class="logo">
				<image src="../../static/logo2.png"></image>
				<view>Reedaw</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="version==-1">新版本{{phoneInfo.versionUrl.version}}</text>
						<uni-icons type="right"></uni-icons>
					</view>
				</view>
				<view class="item">
					<a href="http://tc.pcxbc.com/technology/privacy_index" class="href">
						<text class="left">隐私协议</text>
						<uni-icons type="right"></uni-icons>
					</a>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapState
	} from "vuex";
	let dtask;
	export default {
		data() {
			return {
				version: 0,
			}
		},
		computed: {
			...mapState(["phoneInfo"]),
		},
		onLoad() {
			let that = this
			if (!that.phoneInfo.versionUrl) {
				that.handleoginversion()
			} else {
				that.version = that.$tools.compareVersions(that.phoneInfo.info.version, that.phoneInfo.versionUrl.version)
			}
		},
		onBackPress() {
			console.log("dtask", dtask)
			if (dtask != undefined) {
				dtask.onProgressUpdate((res) => {
					if (res.progress != 100) {
						dtask.abort();
					}
				});
			}
		},
		methods: {
			handleVersion() {
				let that = this
				if (that.version == -1) {
					if (that.phoneInfo.platform === 'android') {
						uni.setStorageSync('VERSION', that.phoneInfo.versionUrl.version)
						let showLoading = plus.nativeUI.showWaiting('正在下载')
						dtask = uni.downloadFile({
							url: that.phoneInfo.versionUrl.url,
							success: (downloadRes) => {
								if (downloadRes.statusCode === 200) {
									plus.nativeUI.closeWaiting()
									plus.runtime.install(
										downloadRes.tempFilePath, {
											force: false
										},
										function() {
											console.log('install success...');
											plus.runtime.restart();
										},
										function(e) {
											console.error('install fail...');
										});
								}
							},
							fail: () => {
								uni.showToast({
									title: '升级失败',
									icon: 'none'
								});
							}
						});
						dtask.onProgressUpdate((res) => {
							showLoading.setTitle(" 正在下载" + res.progress + "% ");
						});
					} else {
						plus.runtime.launchApplication({
							action: `itms-apps://itunes.apple.com/cn/app/id6654906497?mt=8`
						})
					}
				} else {
					that.$tools.msg("已经是最新版本了！")
				}
			},
			// 版本信息
			handleoginversion() {
				let that = this
				that.$model.getLoginVersion({}).then(res => {
					that.version = that.$tools.compareVersions(that.phoneInfo.info.version, res.data.version)
					that.$store.commit('changePhoneInfo', {
						versionUrl: res.data
					})
				})
			},
		}
	}
</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: 40rpx;
		line-height: 35px;

		image {
			width: 140rpx;
			height: 140rpx;
			border-radius: 5px;
		}

		text {
			display: block;
			font-size: 32rpx;
			color: #999;
		}
	}

	.list {
		width: calc(100% - 20px);
		margin: 50px 0;

		.item {
			width: 100%;
			height: 50px;
			line-height: 50px;
			margin-bottom: 15px;
			display: flex;
			background-color: #fff;
			border-radius: 10px;
			padding: 0 10px;
			justify-content: space-between;

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

			.href {
				display: flex;
				width: 100%;
				color: #000;
				text-decoration: none;
				justify-content: space-between;
			}
		}
	}

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