<template>
	<view class="adminSetting">
		<navigation-bar-view ref="nav"></navigation-bar-view>
		<view class="group">
			<view class="label">
				<text>当前版本</text>
				<view class="more" style="color: #007AFF;" @click="uploadVersion" v-if="version">
					<text>有新版本</text>
				</view>
				<view class="more" v-else>
					<text>v{{$config.version}}</text>
				</view>
			</view>
			<view class="label" @click="clearCache">
				<text>清除缓存</text>
				<view class="more">
					<text>{{fileSizeString}}</text>
					<image class="arrow" src="../../static/images/icon-arrow-right-grey.png"></image>

				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import navigationBarView from '../../components/navigationBarView.vue';
	export default {
		components: {
			navigationBarView
		},
		data() {
			return {
				body: {},
				data: {},
				version: '',
				fileSizeString: '',
			}
		},
		onShow() {
			this.getVersion();
		},

		methods: {
			getVersion() {
				let sn = uni.getStorageSync('sn');
				this.$api.getCurrentVersion({
					deviceSn: sn
				}).then(res => {
					if (this.$config.version != res.version) {
						this.version = res.url;
					} else {
						this.version = ''
					}
				})
			},

			formatSize() {
				let that = this;
				plus.cache.calculate(function(size) {
					let sizeCache = parseInt(size);
					if (sizeCache == 0) {
						that.fileSizeString = "0B";
					} else if (sizeCache < 1024) {
						that.fileSizeString = sizeCache + "B";
					} else if (sizeCache < 1048576) {
						that.fileSizeString = (sizeCache / 1024).toFixed(2) + "KB";
					} else if (sizeCache < 1073741824) {
						that.fileSizeString = (sizeCache / 1048576).toFixed(2) + "MB";
					} else {
						that.fileSizeString = (sizeCache / 1073741824).toFixed(2) + "GB";
					}
				});
			},

			clearCache() {
				let that = this;
				let os = plus.os.name;
				if (os == 'Android') {
					let main = plus.android.runtimeMainActivity();
					let sdRoot = main.getCacheDir();
					let files = plus.android.invoke(sdRoot, "listFiles");
					let len = files.length;
					for (let i = 0; i < len; i++) {
						let filePath = '' + files[i]; // 没有找到合适的方法获取路径，这样写可以转成文件路径  
						plus.io.resolveLocalFileSystemURL(filePath, function(entry) {
							if (entry.isDirectory) {
								entry.removeRecursively(function(entry) { //递归删除其下的所有文件及子目录  
									uni.showToast({
										title: '缓存清理完成',
										duration: 2000
									});
									that.formatSize(); // 重新计算缓存  
								}, function(e) {
									console.log(e.message)
								});
							} else {
								entry.remove();
							}
						}, function(e) {
							console.log('文件路径读取失败')
						});
					}
				}
			},

			uploadVersion() {
				uni.showLoading({
					title: '更新中……'
				})
				console.log('ssaaa', this.version);
				const downloadTask = uni.downloadFile({ //执行下载
					url: this.version, //下载地址
					success: downloadResult => { //下载成功
						uni.hideLoading();
						if (downloadResult.statusCode == 200) {
							uni.showModal({
								title: '',
								content: '更新成功，确定现在重启吗？',
								confirmText: '重启',
								confirmColor: '#EE8F57',
								success: function(res) {
									if (res.confirm == true) {
										plus.runtime.install( //安装
											downloadResult.tempFilePath, {
												force: true
											},
											function(res) {
												utils.showToast('更新成功，重启中');
												plus.runtime.restart();
											}
										);
									}
								}
							});
						}
					},
					fail: err => {
						console.log('下载错误', err);
					}
				});

				downloadTask.onProgressUpdate((res) => {
					console.log('下载进度' + res.progress);
					console.log('已经下载的数据长度' + res.totalBytesWritten);
					console.log('预期需要下载的数据总长度' + res.totalBytesExpectedToWrite);

					// 测试条件，取消下载任务。
					// if (res.progress == 100) {
					// 	downloadTask.abort();
					// }
				});
			},
		}
	}
</script>

<style lang="scss">
	.adminSetting {
		min-height: 100vh;
		background-color: #f8f8f8;

		.group {
			border-radius: 2.7vw;
			margin: 2.7vw 0 0;
			background: #FFFFFF;
			padding: 0 4.1vw;

			.label {
				display: flex;
				align-items: center;
				height: 13.2vw;
				font-size: 4vw;
				border-bottom: 2rpx solid #F8F8F8;

				.line {
					width: 1.1vw;
					height: 4vw;
					border-radius: 1.1vw;
					background-color: #FDD302;
					margin-right: 2.1vw;
				}

				text {
					flex: 1;
				}

				.more {
					display: flex;
					align-items: center;
					font-weight: 300;
					font-size: 3.7vw;
					color: #333333;

					.arrow {
						width: 2vw;
						height: 3.6vw;
						margin-left: 2vw;
					}
				}
			}
		}
	}
</style>
