<template>
	<view class="container">
		<view style="padding-left: 16px;color: rgba(0,0,0,0.5);font-size: 12px;margin-bottom: -10px;margin-top: 16px;">
			账号信息</view>
		<uni-card padding="0">
			<uni-list :border="false">
				<uni-list-item title="账号" :rightText="userInfo.userName"></uni-list-item>
				<uni-list-item title="昵称" :rightText="userInfo.nickName"></uni-list-item>
				<uni-list-item title="手机号" :rightText="userInfo.phonenumber"></uni-list-item>
			</uni-list>
		</uni-card>
		<view style="padding-left: 16px;color: rgba(0,0,0,0.5);font-size: 12px;margin-bottom: -10px;margin-top: 16px;">
			版本信息</view>
		<uni-card padding="0">
			<uni-list :border="false">
				<uni-list-item title="版本号" :rightText="version">
				</uni-list-item>
				<uni-list-item clickable title="检查更新" :showBadge="!!versionName" :badgeText="versionName"
					badgeType="error" showArrow @click="checkUpdate">
				</uni-list-item>
			</uni-list>
		</uni-card>
		<button class="submit" type="warn" @click="logout" :loading="submiting">退出登录</button>
	</view>
</template>

<script>
	import helper from '../../common/helper'
	import {
		downloadApp, installApp
	} from '../../common/upgrade'
	export default {
		data() {
			return {
				userInfo: {},
				submiting: false,
				version: null,
				versionCode: null,
				versionName: null,
				downloadUrl: '',
				hasNewVersion: false,

				isDownloadFinish: false, //是否下载完成
				hasProgress: false, //是否能显示进度条
				currentPercent: 0, //当前下载百分比
				isStartDownload: false, //是否开始下载
				fileName: '', //下载后app本地路径名称
			}
		},
		onShow() {
			const systemImfo = uni.getSystemInfoSync()
			this.versionCode = systemImfo.appVersionCode
			console.log(systemImfo)
			this.version = systemImfo.appVersion;
			uni.request({
				url: `${helper.mgServerUrl}/app/config/version`,
				header: {
					Authorization: `Bearer ${helper.tempToken}`
				},
				success: (resp) => {
					const data = resp.data.data;
					console.log(data.versionCode, this.versionCode)
					if (data.versionCode > this.versionCode) {
						this.versionName = data.versionName;
						this.downloadUrl = data.downloadUrl;
					}
				},
				fail() {

				}
			})
		},
		onLoad() {
			uni.getStorage({
				key: 'tempUserInfo',
				success: (resp) => {
					console.log(resp)
					this.userInfo = resp.data.user;
				},
				fail: (err) => {
					console.log(err)
				}
			});

		},
		methods: {
			checkUpdate() {
				if (this.versionName) {
					uni.showModal({
						title: '提示',
						content: `是否更新到 ${this.versionName} 版本`,
						success: (e) => {
							if (e.confirm) {
								plus.runtime.openURL(this.downloadUrl);
								// downloadApp(this.downloadUrl, current => {
								// 	//下载进度监听
								// 	this.hasProgress = true
								// 	this.currentPercent = current
								// 	uni.showToast({
								// 		duration: 0,
								// 		title: '更新中，' + current
								// 	})
								// }).then(fileName => {
								// 	//下载完成
								// 	uni.hideToast()
								// 	this.isDownloadFinish = true
								// 	this.fileName = fileName
								// 	if (fileName) {
								// 		//自动安装App
								// 		this.handleInstallApp()
								// 	}
								// }).catch(e => {
								// 	console.log(e, 'e')
								// })
							}
						}
					})
				} else {
					uni.showToast({
						icon:'none',
						title: '当前已是最新版本'
					})
				}
			},
			handleInstallApp() {
				//下载完成才能安装，防止下载过程中点击
				if (this.isDownloadFinish && this.fileName) {
					installApp(this.fileName, () => {
						
					})
				}
			},
			logout() {
				const toLogin = (token) => {
					uni.request({
						url: `${helper.serverUrl}/user/logout`,
						method: 'POST',
						header: {
							Authorization: `Bearer ${token}`
						},
						success() {
							uni.reLaunch({
								url: '/pages/login/login'
							})
						},
						fail() {
							uni.reLaunch({
								url: '/pages/login/login'
							})
						}
					})
				}
				uni.showModal({
					content: '是否退出当前用户？',
					title: '提示',
					success(res) {
						if (res.confirm) {
							uni.getStorage({
								key: 'token',
								success(resp) {
									uni.removeStorage({
										key: 'token'
									})
									toLogin(resp.data);
								},
								fail() {
									toLogin();
								}
							})
						} else if (res.cancel) {}
					}
				})
			},
		}
	}
</script>

<style scoped>
	.container>>>.uni-list::after {
		height: 0;
	}

	.submit {
		border-radius: 47px;
		width: 375rpx;
	}
</style>