<template>
	<view>
		<umask :show="uploadPopShow" @click="" :duration="0">
			<view class="page-height">
				<view class="page-content">
					<view class="wrap">
						<view class="popup-bg">
							<view class="popup-content popup-content-show">
								<view class="update-wrap">
									<!-- <image src="" class="top-img" mode="widthFix"></image> -->
									<view class="content_update">
										<text class="title">發現新版本V{{ newVision }}</text>
										<view class="title-sub">app新版本升級</view>
										<button class="btn" v-if="!pressShow" @click.stop="toUpLoad()">立即升級</button>
										<view class="sche-wrap" v-else>
											<view class="sche-bg">
												<view class="sche-bg-jindu" :style="{ 'width': pressValue + '%' }">
												</view>
											</view>
											<view class="down-text">
												下載進度: {{ (downSize / 1024 / 1024).toFixed(2) }}M /
												{{ (fileSize / 1024 / 1024).toFixed(2) }}M
											</view>
										</view>
									</view>
								</view>
								<image src="./static/img/close.png" class="close-ioc" @click.stop="closeUpdate()">
								</image>
							</view>
						</view>
					</view>
				</view>
			</view>
		</umask>
	</view>
</template>

<script>
	import umask from "./u-mask/u-mask.vue";
	import { getvision } from "@/request/api.js"
	export default {
		name: "am-upVersion",
		components: {
			umask
		},
		data() {
			return {
				newVision:"1.0.0",
				pressShow: false,
				downSize: 0,
				pressValue: 0,
				fileSize: 0,
				uploadPopShow: false,
				downloadTask:null
			};
		},
		watch: {
			newVision(newValue) {
			    if (this.compareVersions(newValue, plus.runtime.version) > 0 && uni.getStorageSync("nowVersion") != this.newVision) {
			        this.uploadPopShow = true;
			    }
			}
		},
		mounted() {
			this.getVersion();
		},
		methods: {
			async getVersion(){
				let res = await getvision();
				if (res.code === 1) {
					this.newVision = res.data.newversion;
					this.upLoadUrl = res.data.downloadurl;
				}
			},
			compareVersions(version1, version2) {
			    const v1 = version1.split('.').map(num => parseInt(num, 10));
			    const v2 = version2.split('.').map(num => parseInt(num, 10));
			
			    for (let i = 0; i < Math.max(v1.length, v2.length); i++) {
			        if ((v1[i] || 0) > (v2[i] || 0)) return 1;
			        if ((v1[i] || 0) < (v2[i] || 0)) return -1;
			    }
			    return 0;
			},
			toUpLoad() {
				if (!this.upLoadUrl) {
					uni.showToast({
						title: "未檢測到下載地址",
						icon: "none"
					});
					return;
				}
				this.pressShow = true;
				this.downloadTask = uni.downloadFile({
					url: this.upLoadUrl,
					success: (res) => {
						if (res.statusCode === 200) {
							this.pressShow = false;
							this.uploadPopShow = false;
							plus.runtime.install(res.tempFilePath, {
								force: true
							}, (suc) => {
								uni.setStorageSync("nowVersion",this.newVision)
								plus.runtime.restart();
							}, (error) => {
								console.log(error);
							});
						} else {
							uni.showToast({
								title: "安裝包下載失敗!請聯繫管理員。",
								icon: "none"
							});
						}
					},
					fail: () => {
						uni.showToast({
							title: "下載失敗，請檢查網絡",
							icon: "none"
						});
					},
				});

				this.downloadTask.onProgressUpdate((res) => {
					this.downSize = res.totalBytesWritten;
					this.fileSize = res.totalBytesExpectedToWrite;
					this.pressValue = res.progress;
				});
			},
			closeUpdate() {
				this.pressShow = false;
				this.uploadPopShow = false;
				if (this.downloadTask) {
					this.downloadTask.abort();
				}
			},
		},
	};
</script>

<style lang="scss">
	.page-height {
		// height: 100vh;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		background-color: rgba($color: #000000, $alpha: .7);
	}

	.popup-bg {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		width: 750rpx;
	}

	.popup-content {
		display: flex;
		flex-direction: column;
		align-items: center;
		height: 30vh;
		width: 65vw;
	}

	.popup-content-show {
		animation: mymove 300ms;
		transform: scale(1);
	}

	@keyframes mymove {
		0% {
			transform: scale(0);
			/*開始為原始大小*/
		}

		100% {
			transform: scale(1);
		}

	}

	.update-wrap {
		width: 100%;
		height: calc(100% - 100rpx);
		border-radius: 18rpx;
		position: relative;
		display: flex;
		flex-direction: column;
		background-color: #ffffff;

		.top-img {
			position: absolute;
			left: 0;
			width: 100%;
			height: 256rpx;
			// height: 100rpx;
			top: -180rpx;
			z-index: 999999999999;
		}

		.content_update {
			height: 100%;
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;

			.title {
				font-size: 32rpx;
				font-weight: bold;
				color: rgb(252, 122, 110);
			}

			.title-sub {
				text-align: center;
				font-size: 24rpx;
				color: #666666;
				padding: 30rpx 0;
				box-sizing: border-box;
			}

			.btn {
				width: 80%;
				display: flex;
				align-items: center;
				justify-content: center;
				color: #ffffff;
				font-size: 30rpx;
				height: 60rpx;
				line-height: 60rpx;
				border-radius: 100px;
				background-color: rgb(252, 122, 110);
				margin-top: 20rpx;
			}
		}
	}

	.close-ioc {
		width: 70rpx;
		height: 70rpx;
		margin-top: 30rpx;
	}

	.sche-wrap {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: flex-end;
		padding: 10rpx 50rpx 0;

		.sche-wrap-text {
			font-size: 24rpx;
			color: #666;
			margin-bottom: 20rpx;
		}

		.sche-bg {
			position: relative;
			background-color: #cccccc;
			height: 30rpx;
			border-radius: 100px;
			width: 480rpx;
			display: flex;
			align-items: center;

			.sche-bg-jindu {
				position: absolute;
				left: 0;
				top: 0;
				height: 30rpx;
				min-width: 40rpx;
				border-radius: 100px;
				background: url(./static/img/round.png) rgb(252, 122, 110) center right 4rpx no-repeat;
				background-size: 26rpx 26rpx;
			}
		}

		.down-text {
			font-size: 24rpx;
			color: rgb(252, 122, 110);
			margin-top: 16rpx;
		}
	}
</style>