<template>
	<view class="main-view">
		<u-navbar :is-back="false" title="WMS PDA" :background="background"></u-navbar>
		<u-card :full="true">
			<view slot="head">
				<u-avatar />
				<view
					style="display: inline-block; margin-left: 17rpx; font-size: 16px; vertical-align: top; margin-top: 20rpx;">
					用户id:{{userid}}<br />
					<!-- 用户名称:{{username}}<br /> -->
					组织:{{ERPFACTORY}}<br />
					当前版本号:{{appversion}}<br />
					<text><u-button type="error" size="small" @click="pageTo">消息通知:{{ messageSum }}</u-button></text>
				</view>
			</view>
		</u-card>
		<u-button style="margin-top: 20rpx;" type="primary" @click="checkUpdate">检查更新</u-button>
		<br/>
		<u-button type="error" @click="handlerLogout">退出登录</u-button>
		<u-gap height="70"></u-gap>
	</view>
</template>

<script>
	import store from '../../store/index.js'
	import _app from '../../common/app_info.js'
	import _config from '../../common/api.js'
	import {
		MatchVersion
	} from '../../utils/utils.js'
	import {
		installWgt,
		downWgt
	} from '../../common/CheckUpdate.js'

	export default {
		data() {
			return {
				messageSum: "0",
				orgObject: {
					ERPFACTORY: '',
					DESCRIPTION: ''
				}, // 组织对象
				background: {
					backgroundImage: 'linear-gradient(45deg, rgb(28, 187, 180), rgb(141, 198, 63))'
				},
				ERPFACTORY: JSON.parse(uni.getStorageSync('orgObject')).ERPFACTORY, //组织
				text: '无头像',
				username: '', //用户名
				sitename: "", //工厂
				appversion: "", //版本号
				userid: "", //用户id,
			}
		},
		mounted() {
			
			let that = this
			uni.getStorage({
				key: 'userid',
				success(res) {
					that.userid = res.data
				}
			})

			uni.getStorage({
				key: 'username',
				success(res) {
					that.username = res.data
				}
			})
			uni.getStorage({
				key: 'sitename',
				success(res) {
					that.sitename = res.data
				}
			})
			
			this.orgObject = JSON.parse(uni.getStorageSync('orgObject'))
			
			this.appversion = plus.runtime.version
			uni.getStorage({
				key: 'appversion',
				success(res) {
					that.appversion = plus.runtime.version
				}
			})
			this.getMessageSum();
		},
		computed: {
			tabbar() {
				return store.vuex_tabbar
			}
		},
		methods: {
			handlerLogout() {
				uni.removeStorageSync('menulist')
				uni.removeStorageSync("token")
				uni.removeStorageSync("ut")
				uni.redirectTo({
					url: "/pages/login/index"
				})
			},
			pageTo() {
				uni.redirectTo({
					url: "/pages/message/index"
				})
			},
			getMessageSum() {
				let userid = uni.getStorageSync('userid')
				 this.$MyRequest('/websocketMessage/getMessageSum', {
				 	userId: userid
				 }).then(res => {
				 	this.messageSum = res?.data?.resultObj
					console.log(this.messageSum)
				 }).catch(err => {
				 	this.$showMessage(res)
				 })
			},
			checkUpdate() {
				const that = this;
				this.$MyRequest('/pdaDownload/getVersion', {vision:1}).then(res => {
					console.log("运行版本" + plus.runtime.version)
					const version = res.data.resultObj.PDA_VERSION;
					console.log("服务器版本" + res.data.resultObj.PDA_VERSION)
					const downloadUrl = res.data.resultObj.PDA_DOWNLOADURL;
					console.log("下载地址" + downloadUrl)
					const currentVersion = plus.runtime.version;
					if (version > currentVersion) {
						uni.showModal({
							title: '发现新版本',
							content: '是否更新应用？',
							success: function(res) {
								console.log(res, downloadUrl)
								if (res.confirm) {
									console.log(plus.downloader)
									const downObj = plus.downloader.createDownload(downloadUrl, {
										method: 'GET',
										timeout: 0
									}, function(d, status) {
										console.log("状态" + status, d)
										if (status == 200) {
											plus.runtime.install(d.filename, {}, function() {
												console.log("app安装成功")
												plus.runtime.restart();
											});
										} else {
											uni.showToast({
												title: '下载失败，请重试',
												icon: 'none'
											});
										}
									});
									downObj.start();
									uni.removeStorageSync('menulist')
									uni.removeStorageSync("token")
									uni.redirectTo({
										url: "/pages/login/index"
									})
								}
							}
						});
					} else {
						uni.showToast({
							title: '已经是最新版本，无需更新',
							icon: 'none'
						});
					}
				}).catch(err => {
					this.$showMessage(err)
				})
			},
			updateCheck() {
				let appVersion = "";
				plus.runtime.getProperty(plus.runtime.appid, function(wgtinfo) {
					appVersion = wgtinfo.version; //获取APP的版本号(包括热更新后的)
				});
				let downloadUrl = this.$URL.serverURL + '/version/downloadwgt' //下载wgt更新包的地址
				let checkVerUrl = this.$URL.serverURL + '/version/appnewestversion' //检查版本号的url地址

				uni.request({
					url: checkVerUrl, //检查更新的服务器地址
					method: 'GET',
					success: (res) => {
						let serverVersion = res.data.resultObj[0]["DESCRIPTION"];
						console.log("服务器版本:" + serverVersion); //打印服务器上的版本
						if (appVersion == '' || appVersion == null || appVersion == undefined) {
							appVersion = plus.runtime.version;
							console.log("初始版本号:" + appVersion);
						}
						if (appVersion != serverVersion) {
							console.log("走这里")
							uni.showModal({
								title: '检测到新版本',
								content: '当前版本:' + appVersion + '\r\n' + '最新版本:' + serverVersion +
									'\r\n请立即更新',
								success: function(cof) {
									if (cof.confirm) {
										console.log(downloadUrl);
										downWgt(downloadUrl, serverVersion); //下载wgt更新包
									} else if (cof.cancel) {
										console.log('用户点击取消' + appVersion)
									}
								}
							})
						}
					}
				})
			}



		}
	}
</script>

<style lang="scss" scoped>
	.main-view {
		margin: 15rpx;
	}
</style>