<template>
	<view class="mask flex-center">
		<view class="content-update 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/images/bg_top.png">
				</image>
			</view>
			<view class="content-header"></view>
			<view class="content-body">
				<view class="title">
					<text>纽迪希亚ACS系统又有新版本了，升级到最新版本，享受更丰富、稳定、快速的功能和体验！</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: 14px;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/images/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: 15px;
		border-bottom-right-radius: 15px;
	}

	.content-update {
		position: relative;
		top: 0;
		width: 300px;
		background-color: #fff;
		box-sizing: border-box;
		padding: 0 20px;
		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: -97.5px;
		left: 0;
		width: 300px;
		height: 135px;
	}

	.content-top-text {
		font-size: 22px;
		font-weight: bold;
		color: #F8F8FA;
		position: absolute;
		top: 60px;
		left: 25px;
		z-index: 1;
	}

	.content-header {
		height: 40px;
	}

	.title {
		font-size: 16px;
		font-weight: bold;
		color: #3DA7FF;
		line-height: 20px;
		color: #333;
	}

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

	.box-des-scroll {
		box-sizing: border-box;
		padding: 0 20px;
		height: 25px;
		text-align: left;
	}

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

	.progress-box {
		width: 100%;
	}

	.progress {
		width: 90%;
		height: 20px;
		border-radius: 13px;
	}

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

	.content-button {
		text-align: center;
		flex: 1;
		font-size: 15px;
		font-weight: 400;
		color: #FFFFFF;
		border-radius: 20px;
		margin: 0 10px;

		height: 40px;
		line-height: 40px;

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

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