<template>
	<view class="setting-container" :style="{height: `${windowHeight}px`}">
		<view class="menu-list">
			<view class="list-cell list-cell-arrow" @click="handleToPwd">
				<view class="menu-item-box">
					<view class="iconfont icon-password menu-icon"></view>
					<view>修改密码</view>
				</view>
			</view>
			<view class="list-cell list-cell-arrow" @click="handleToUpgrade">
				<view class="menu-item-box">
					<view class="iconfont icon-refresh menu-icon"></view>
					<view>检查更新</view>
				</view>
			</view>
			<view class="list-cell list-cell-arrow" @click="handleCleanTmp">
				<view class="menu-item-box">
					<view class="iconfont icon-clean menu-icon"></view>
					<view>清理缓存</view>
				</view>
			</view>
		</view>
		<view class="cu-list menu">
			<view class="cu-item item-box">
				<view class="content text-center" @click="handleLogout">
					<text class="text-black">退出登录</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getAdminNewestAppInfo
	} from "@/api/px/app";

	export default {
		data() {
			return {
				windowHeight: uni.getSystemInfoSync().windowHeight
			}
		},
		methods: {
			handleToPwd() {
				this.$tab.navigateTo('/pages/mine/pwd/index')
			},
			handleToUpgrade() {
				uni.showToast({
					icon: 'none',
					title: '正在检查更新···'
				})
				getAdminNewestAppInfo().then(res => {
					if (getApp().globalData.config.appInfo.version === res.data.version) {
						uni.showModal({
							title: '更新提示',
							content: '没有可用的更新版本',
							success: (res) => {}
						});
					} else {
						uni.showModal({
							title: '更新提示',
							content: '存在新版本，确认更新？',
							success: (res) => {
								if (res.confirm) {
									const task = plus.downloader.createDownload(res.data
										.downloadUrl, {}, (d,
											status) => {
											if (status === 200) {
												console.log("下载更新成功：" + d.filename);
												this.installWgt(d.filename);
											} else {
												console.log("下载更新失败！");
												plus.nativeUI.toast("下载更新失败！");
											}
											plus.nativeUI.closeWaiting();
										});
									task.start();

									// 下载时实时显示下载进度
									let prg = 0;
									this.showProgress = true;
									task.addEventListener('statechanged', (task, status) => {
										// 给下载任务设置一个监听 并根据状态  做操作
										switch (task.state) {
											case 1:
												//'正在下载';
												plus.push.createMessage(`正在下载···`);
												break;
											case 2:
												// '已连接到服务器';
												plus.push.createMessage(`已连接到服务器···`);
												break;
											case 3:
												prg = parseInt(
													(parseFloat(task.downloadedSize) /
														parseFloat(task.totalSize)) *
													100
												);
												this.percentageNum = prg; // 赋值给进度条组件
												break;
											case 4:
												this.showProgress = false; // 进度隐藏
												break;
										}
									});
								}
							}
						});
					}
				})
			},
			/**
			 * 下载
			 * @param path
			 */
			installWgt(path) {
				plus.nativeUI.showWaiting("安装更新");
				plus.runtime.install(path, {}, () => {
					plus.nativeUI.closeWaiting();
					console.log("安装更新成功！");
					plus.nativeUI.alert("更新完成！", () => {
						plus.runtime.restart(); //安装成功后重启应用
					});
				}, (e) => {
					plus.nativeUI.closeWaiting();
					console.log("安装更新失败！[" + e.code + "]：" + e.message);
					plus.nativeUI.toast("安装更新失败！");
				});
			},
			handleCleanTmp() {
				uni.showModal({
					title: '清理缓存',
					content: '清理缓存后将退出登录，确认清理缓存？',
					success: (res) => {
						if (res.confirm) {
							try {
								uni.clearStorageSync();
								this.$store.dispatch('LogOut').then(() => {
									this.$tab.reLaunch('/pages/index')
								})
							} catch (e) {
								// error
							}
						}
					}
				})
			},
			handleLogout() {
				this.$modal.confirm('确定注销并退出系统吗？').then(() => {
					this.$store.dispatch('LogOut').then(() => {
						this.$tab.reLaunch('/pages/index')
					})
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.page {
		background-color: #f8f8f8;
	}

	.item-box {
		background-color: #FFFFFF;
		margin: 30rpx;
		display: flex;
		flex-direction: row;
		justify-content: center;
		align-items: center;
		padding: 10rpx;
		border-radius: 8rpx;
		color: #303133;
		font-size: 32rpx;
	}
</style>