<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">{{$t("titleVersionUpdate")}}</view>
					<view class="right">
						<text class="new" v-if="version==-1">{{$t("titleNewVersion")}}{{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">{{$t("titleAgreementContntText")}}</text>
						<uni-icons type="right"></uni-icons>
					</a>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapState
	} from "vuex";
	let dtask;
	export default {
		data() {
			return {
				// 0版本号相通，1，：第一个版本号大于第二个版本号，-1：第一个版本号小于第二个版本号
				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)
			}
		},
		onReady() {
			uni.setNavigationBarTitle({
				title: this.$t('titleAboutUs'),
			})
		},
		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(that.$t("msgDownloading"))
						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: that.$t("msgUpgradeFail"),
									icon: 'none'
								});
							}
						});
						dtask.onProgressUpdate((res) => {
							showLoading.setTitle(that.$t("msgDownloading") + res.progress + "% ");
							// console.log('下载进度' + res.progress + '%');
						});
					} else {
						plus.runtime.launchApplication({
							action: `itms-apps://itunes.apple.com/cn/app/id6654906497?mt=8`
						})
					}
				} else {
					that.$tools.msg(that.$t("msgLatestVersion"))
				}
			},
			// 版本信息
			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>