<template>
	<view class="container">
		<view class="head">
			<u-navbar :is-fixed="false" :border-bottom="false" :is-back="false" title="个人中心"
				:background="{ background: 'transprent' }" title-color="#FFFFFF">
			</u-navbar>
			<!-- 用户信息 -->
			<view class="user-box">
				<view class="left" style="width: 60%;">
					<view style="display: flex;flex-direction: row;width: 100%;">
						<view style="display: flex;flex-direction: column;justify-content: center;">
							<image :src="userInfo.avatar" class="avatar" mode=""></image>
							<view class="user-name">{{ userInfo.nick_name }}</view>
						</view>
					</view>
					<view style="display: flex;flex-direction: row;width: 100%;color: #F0F0F0;">
						{{roleName}}/{{deptName}}
					</view>
				</view>
				<view style="display: flex;flex-direction: column;justify-content: flex-end;">
					<view @click="$u.func.route('/pages/user/profile')" class="edit-btn">编辑资料</view>
				</view>
			</view>
		</view>
		<!-- 功能列表 -->
		<view class="cell-box">
			<u-cell-group :border="false">
				<u-cell-item title="修改密码" :border-bottom="false" @click="$u.func.route('/pages/user/modify-password')">
					<image slot="icon" src="/static/images/user/c8.png" class="icon" mode=""></image>
				</u-cell-item>
			</u-cell-group>
		</view>
		<view class="cell-box">
			<u-cell-group :border="false">
			</u-cell-group>
		</view>
		<view class="cell-box">
			<u-cell-group :border="false">
				<u-cell-item title="检查更新" :border-bottom="false" @click="checkVersion">
					<image slot="icon" src="/static/images/news/search.png" class="icon" mode=""></image>
				</u-cell-item>
			</u-cell-group>
		</view>
		<view class="cell-box">
			<u-cell-group :border="false">
				<u-cell-item title="关于" :border-bottom="false" @click="$u.func.route('/pages/user/about')">
					<image slot="icon" src="/static/images/user/c7.png" class="icon" mode=""></image>
				</u-cell-item>
			</u-cell-group>
		</view>
		<u-gap height="50"></u-gap>
		<view class="cell-box">
			<u-cell-group :border="false">
				<button type="warn" @click="logout()">退出登录</button>
			</u-cell-group>
		</view>
	</view>
</template>

<script>
	import http from '@/http/api.js'
	export default {
		onLoad() {
			console.log(this.userInfo.nick_name)
			const that = this;

			http.request({
				url: '/galaxy-system/dept/detail?id=' + this.userInfo.dept_id,
				method: 'GET'
			}).then(res => {
				console.log(JSON.stringify(res.data))
				that.deptName = res.data.deptName;
			}).catch(err => {
				console.log(err)
			})

			http.request({
				url: '/galaxy-system/role/select?roleId=' + this.userInfo.role_id,
				method: 'GET'
			}).then(res => {
				console.log(JSON.stringify(res.data))
				var name = "";
				res.data.forEach(info => {
					name += name.length > 0 ? ":" : "";
					name += info.roleName;
				})
				that.roleName = name;
			}).catch(err => {
				console.log(err)
			})
		},
		data() {
			return {
				roleName: '',
				deptName: '',
				downloadurl: '',
			};
		},
		methods: {
			logout() {
				this.$u.func.logout();
			},
			checkVersion() {
				// #ifdef APP-PLUS  
				let that = this;
				let postData = {};
				postData['appId'] = plus.runtime.appid;
				postData['version'] = plus.runtime.version;
				http.request({
					url: '/galaxy-business/version/check',
					method: 'GET',
					data: postData,
				}).then(res => {
					console.log(JSON.stringify(res))
					if (res.data != null) {
						if (res.data.updateStatus == 1) {
							that.downloadurl = res.data.apkUrl;
							uni.showModal({
								title: "版本检查",
								content: "有新的APP版本需要更新",
								success: (res) => {
									if (res.confirm) {
										that.updateApp();
									}
								},
							})
						} else {
							uni.showModal({
								title: "提示",
								content: "当前已是最新版本！",
								showCancel: false,
								success: (res) => {

								}
							})
						}
					}
				}).catch(err => {
					console.log(err)
				})
				// #endif  
			},
			updateApp() {
				// #ifdef APP-PLUS  
				let task = plus.downloader.createDownload(this.downloadurl, {},
					function(d, status) {
						//下载完成
						if (status == 200) {
							var path = plus.io.convertLocalFileSystemURL(d
								.filename);
							plus.runtime.install(path, {}, function() {
								plus.nativeUI.closeWaiting();
								uni.showModal({
									title: "更新提示",
									content: "安装已完成,APP需重新启动！",
									showCancel: false,
									success: (res) => {
										plus.runtime.restart();
									}
								})
							}, function(e) {
								uni.showModal({
									title: "更新提示",
									content: "版本更新失败[" + e.code + "]：" + e.message,
									mask: false,
									duration: 1500,
								})
							})
						} else {
							uni.showModal({
								title: "更新提示",
								content: "下载安装包文件失败！",
								mask: false,
								duration: 1500,
							})
						}
					}
				)
				task.addEventListener("statechanged", function(download, status) {
					switch (download.state) {
						case 1:
							loadingDig.setTitle("开始下载");
							break;
						case 2:
							loadingDig.setTitle("已连接");
							break;
						case 3:
							//if (undefined != task.downloadedSize && null != task.downloadedSize && parseFloat(task
							//	.downloadedSize) > 0.0) {
							// let prg = parseInt((parseFloat(task
							// 			.downloadedSize) /
							// 		parseFloat(task.totalSize)
							// 	) *
							// 	100.0);
							// if (undefined != prg && null != prg && (prg >= 0.0 && prg <= 100.0)) {
							// 	loadingDig.setTitle("安装包下载 " + prg + "%");
							// }
							loadingDig.setTitle("安装包下载中...");
							break;
						case 4:
							plus.nativeUI.closeWaiting();
							break;
					}
				})
				task.start();
				var loadingDig = plus.nativeUI.showWaiting("开始下载安装包");
				// #endif  
			}
		}

	};
</script>

<style lang="scss">
	.container {
		background-color: #f7f7f7;
		min-height: 100vh;
		overflow: hidden;
	}

	.head {
		position: relative;
		top: 0;
		left: 0;
		z-index: 1;
		// min-height: 582rpx;
		min-height: 400rpx;
		overflow: hidden;
		background: #3F9EFF;

		.set-icon {
			vertical-align: middle;
			width: 41rpx;
			height: auto;
			margin-right: 35rpx;
		}

		.head-bg {
			position: absolute;
			left: 0px;
			top: 0px;
			z-index: -1;
			width: 750rpx;
			height: 582rpx;
			background: #0bb9c8;
		}
	}

	.user-box {
		display: flex;
		justify-content: space-between;
		padding: 0 20rpx 0 64rpx;
		margin-top: 36rpx;

		.left {
			display: flex;
			flex-direction: column;
			align-items: center;

			.avatar {
				width: 128rpx;
				height: 128rpx;
				background: #ffffff;
				border-radius: 50%;
			}

			.user-name {
				margin-top: 20rpx;
				font-size: 36rpx;
				font-family: Source Han Sans CN;
				font-weight: 500;
				color: #ffffff;
			}

			.tag {
				margin-top: 20rpx;
				display: flex;
				justify-content: center;
				align-items: center;
				padding: 5rpx 16rpx;
				border: 1px solid #f5f5f5;
				border-radius: 7rpx;

				font-size: 19rpx;
				font-family: Source Han Sans CN;
				font-weight: 300;
				color: #ffffff;
			}
		}

		.edit-btn {
			margin-top: 20rpx;
			margin-bottom: 10px;
			flex-shrink: 0;
			display: flex;
			justify-content: center;
			align-items: center;

			width: 165rpx;
			height: 54rpx;
			border: 2rpx solid #f5f5f5;
			border-radius: 11rpx;

			font-size: 27rpx;
			font-family: Source Han Sans CN;
			font-weight: 400;
			color: #ffffff;
		}
	}

	.nav {
		display: flex;
		//border-top: 2rpx solid #7dcdd6;
		margin: 36rpx 38rpx 0;
		padding: 36rpx 0 42rpx;

		.nav-item {
			width: calc(100% / 4);
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: space-between;

			font-size: 25rpx;
			font-family: Source Han Sans CN;
			font-weight: 400;
			color: #ffffff;

			&:not(:last-of-type) {
				position: relative;

				&::after {
					position: absolute;
					right: 0;
					top: 50%;
					transform: translateY(-50%);
					content: '';
					display: block;
					width: 2rpx;
					background-color: #3ac4d1;
					height: 30rpx;
				}
			}

			.icon {
				width: 48rpx;
				height: 48rpx;
				margin-bottom: 6rpx;
			}
		}
	}

	.cell-box {
		background: #ffffff;
		margin: 18rpx;

		.icon {
			width: 32rpx;
			height: 32rpx;
			margin-right: 22rpx;
		}
	}
</style>
