<template>
	<view class="mask flex-center">
		<view class="content botton-radius">
			<view class="content-top">
				<text class="content-top-text">发现新版本</text>
				<image class="content-top" style="top: 0;" width="100%" height="100%" src="../../static/image/bg_top.png">
				</image>
			</view>
			<view class="content-header"></view>
			<view class="content-body">
				<view class="title">
					<text>北安新生手持又有新版本了，升级到最新版本，享受更丰富、稳定、快速的功能和体验！</text>
				</view>
				<view class="footer flex-center">
					<template>
						<template v-if="!downloadSuccess">
							<view class="progress-box flex-column" v-if="downloading">
								<progress class="progress" border-radius="35" :percent="downLoadPercent" activeColor="#3DA7FF" show-info stroke-width="10" />
								<view style="width:100%;font-size: 28rpx;display: flex;justify-content: space-around;">
									<text>安装包下载中，请稍后</text>
									<text>({{downloadedSize}}/{{packageFileSize}}M)</text>
								</view>
							</view>
							<button v-else class="content-button" style="border: none;color: #fff;" plain @click="updateApp">立即升级</button>
						</template>
					</template>
				</view>
			</view>
			<image v-if="is_mandatory" class="close-img" src="../../static/image/app_update_close.png" @click.stop="closeUpdate"></image>
		</view>
	</view>
</template>

<script>
	export default {
		data () {
			return {
				downloadSuccess: false,
				downloading: false,
				downLoadPercent: 0,
				downloadedSize: 0,
				packageFileSize: 0,
				is_mandatory: true
			}
		},
		props: {
			androidUrl: String
		},
		methods: {
			updateApp() {
				this.is_mandatory = false
				this.downloading = true
				this.doUpData(this.androidUrl);
			},
			doUpData(Url) {
				const downloadTask = uni.downloadFile({
					url: Url,
					success: downloadResult => {
						if (downloadResult.statusCode == 200) {
							this.downloadSuccess = true;
							plus.runtime.install(
								//安装软件
								downloadResult.tempFilePath,
								{
									force: true
								},
								function(res) {
									plus.runtime.restart();
								}
							);
						}
					},
					complete: () => {
						this.downloading = false;
						this.downLoadPercent = 0
						this.downloadedSize = 0
						this.packageFileSize = 0
					}
				});
				// 下载进度
				downloadTask.onProgressUpdate(res => {
					this.downLoadPercent = res.progress;
					this.downloadedSize = (res.totalBytesWritten / Math.pow(1024, 2)).toFixed(2);
					this.packageFileSize = (res.totalBytesExpectedToWrite / Math.pow(1024, 2)).toFixed(2);
				});
			},
			closeUpdate () {
				this.$emit('closeUpdate')
			}
		}
	}
</script>

<style>
	page {
		background: transparent;
	}

	.flex-center {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		justify-content: center;
		align-items: center;
	}

	.mask {
		position: fixed;
		left: 0;
		top: 0;
		right: 0;
		bottom: 0;
		background-color: rgba(0, 0, 0, .65);
	}

	.botton-radius {
		border-bottom-left-radius: 30rpx;
		border-bottom-right-radius: 30rpx;
	}

	.content {
		position: relative;
		top: 0;
		width: 600rpx;
		background-color: #fff;
		box-sizing: border-box;
		padding: 0 50rpx;
		font-family: Source Han Sans CN;
	}

	.text {
		/* #ifndef APP-NVUE */
		display: block;
		/* #endif */
		line-height: 200px;
		text-align: center;
		color: #FFFFFF;
	}

	.content-top {
		position: absolute;
		top: -195rpx;
		left: 0;
		width: 600rpx;
		height: 270rpx;
	}

	.content-top-text {
		font-size: 45rpx;
		font-weight: bold;
		color: #F8F8FA;
		position: absolute;
		top: 120rpx;
		left: 50rpx;
		z-index: 1;
	}

	.content-header {
		height: 80rpx;
	}

	.title {
		font-size: 33rpx;
		font-weight: bold;
		color: #3DA7FF;
		line-height: 38rpx;
		color: #333;
	}

	.footer {
		height: 150rpx;
		display: flex;
		align-items: center;
		justify-content: space-around;
	}

	.box-des-scroll {
		box-sizing: border-box;
		padding: 0 40rpx;
		height: 50rpx;
		text-align: left;
	}

	.box-des {
		font-size: 26rpx;
		color: #000000;
		line-height: 50rpx;
	}

	.progress-box {
		width: 100%;
	}

	.progress {
		width: 90%;
		height: 40rpx;
		border-radius: 35px;
	}

	.close-img {
		width: 70rpx;
		height: 70rpx;
		z-index: 1000;
		position: absolute;
		bottom: -120rpx;
		left: calc(50% - 70rpx / 2);
	}

	.content-button {
		text-align: center;
		flex: 1;
		font-size: 30rpx;
		font-weight: 400;
		color: #FFFFFF;
		border-radius: 40rpx;
		margin: 0 18rpx;

		height: 80rpx;
		line-height: 80rpx;

		background: linear-gradient(to right, #1785ff, #3DA7FF);
	}

	.flex-column {
		display: flex;
		flex-direction: column;
		align-items: center;
	}
</style>
