<template>
	<view class="container">
		<uni-section :title="item.version" type="line" v-for="(item, index) in releases" :key="index">
			<template #right>
				{{ getPublishTime(item.published_at) }}
			</template>
			<uni-card :isFull="true">
				<text>{{ item.description }}</text>
				<template #actions>
					<view class="btn-container" @click="handleDownload(item.download_url)">
						<uni-icons type="download" size="22" color="#007AFF" class="download-icon" />
						<text>下载</text>
					</view>
				</template>
			</uni-card>
		</uni-section>
	</view>
</template>

<script>
import { listReleases } from '@/api/system/update';
import { getReleases } from "@/utils/parseRelease";

export default {
	data() {
		return {
			releases: []
		};
	},
	created() {
		this.fetchReleases();
	},
	methods: {
		getPublishTime(time) {
			if (!time) return '';
			return time.replace('T', ' ').replace('Z', '').slice(0, -9);
		},
		fetchReleases() {
			this.$modal.loading('获取版本列表...');
			listReleases().then(res => {
				this.releases = getReleases(res);
			});
			this.$modal.closeLoading();
		},
		handleDownload(url) {
			const _this = this;

			var dtask = plus.downloader.createDownload(url, {
					filename: __uniConfig.name
				},
				function(d, status) {
					// 下载完成  
					if (status == 200) {
						const fileSaveUrl = plus.io.convertLocalFileSystemURL(d.filename);
						console.log('下载完喽')
						// plus.runtime.openFile(d.filename);

						_this.$modal.alert('下载完成', `已下载到\n${fileSaveUrl}\n\n请卸载当前版本后再进行安装!`)
					} else {
						_this.$modal.msgError('下载失败');
					}
				});
			try {
				console.log('开始下载');
				dtask.start(); // 开启下载的任务
				var prg = 0;
				var showLoading = plus.nativeUI.showWaiting("正在下载");
				dtask.addEventListener('statechanged', function(
					task,
					status
				) {
					// 给下载任务设置一个监听 并根据状态  做操作
					switch (task.state) {
						case 1:
							showLoading.setTitle("正在下载");
							break;
						case 2:
							showLoading.setTitle("已连接到服务器");
							break;
						case 3:
							prg = parseInt(
								(parseFloat(task.downloadedSize) /
									parseFloat(task.totalSize)) *
								100
							);
							showLoading.setTitle("  正在下载" + prg + "%  ");
							break;
						case 4:
							plus.nativeUI.closeWaiting();
							console.log("下载完成");
							//下载完成
							break;
					}
				});
			} catch (err) {
				plus.nativeUI.closeWaiting();
				_this.$modal.msgError('下载失败');
			}
		},
		onPullDownRefresh() {
			this.fetchReleases()
			setTimeout(() => {
				//结束下拉刷新
				uni.stopPullDownRefresh();
			}, 1000);
		},
	}
};
</script>

<style scoped lang="scss">
.container {
	/* #ifdef H5 */
	height: calc(100vh - 44px);
	/* #endif */
	/* #ifndef H5 */
	height: 100vh;
	/* #endif */
	padding-top: 15px;
}

.uni-section {
	&:not(:first-child) {
		margin-top: 15px;
	}

	.uni-section-header {
		line-height: 100%;
	}

	::v-deep .uni-section__content-title {
		font-size: 18px !important;
		font-weight: bold;
	}

	::v-deep .uni-card__actions {
		padding: 10px;
		border-top: 1px solid #d9d9d9;
		font-size: 16px;
		line-height: 16px;
		cursor: pointer;

		.btn-container {
			display: flex;
			align-items: center;
			justify-content: center;
			width: 100%;
		}

		.download-icon {
			margin-right: 4px;
		}
	}

	.uni-card {
		padding: 0 !important;
	}
}
</style>