<template>
	<view class="page">
		<view class="status-bar"></view>
		<!-- 头像与名称 -->
		<view class="avatar">
			<image :src="userIcon" mode="aspectFill" class="icon" @click="choseImg"></image>
			<view class="text">
				<view class="name">{{userName}}</view>
				<view class="btn" @click="changeInfo">
					<image src="/static/images/ic_edit_icon.png"></image>
					<text style="margin-left: 10rpx;">完善个人资料</text>
				</view>
			</view>
		</view>
		<!-- 资料 -->
		<view class="amount">
	<!-- 		<view class="item" @click="openPage('/pages/user/mine_certif_search')">
				<view class="num">3333</view>
				<view>累计捐赠</view>
			</view> -->
			<view class="item">
				<view class="num">3333</view>
				<view>我的证书</view>
			</view>
			<view class="item">
				<view class="num">3333</view>
				<view>尽责参与</view>
			</view>
		</view>
		<!-- 功能 -->
		<view class="func-box">
			<view class="item" @click="openPage('/pages/user/mine_duty_certif')">
				<view class="item-content">
					<image src="../../static/images/ic_table_icon.png" mode="aspectFill"></image>
					<text>义务植树尽责证书</text>
				</view>
				<image src="../../static/images/ic_arrow_right.png" mode="aspectFill"></image>
			</view>
			<view class="item" @click="openPage('/pages/user/mine_honour_certif')">
				<view class="item-content">
					<image src="../../static/images/ic_from_icon.png" mode="aspectFill"></image>
					<text>国土绿化荣誉证书</text>
				</view>
				<image src="../../static/images/ic_arrow_right.png" mode="aspectFill"></image>
			</view>
			<view class="line"></view>
			<view class="item" @click="openPage('/pages/user/minejoin')">
				<view class="item-content">
					<image src="../../static/images/ic_order_icon.png" mode="aspectFill"></image>
					<text>我的劳动参与</text>
				</view>
				<image src="../../static/images/ic_arrow_right.png" mode="aspectFill"></image>
			</view>
			<view class="item" @click="changePwd">
				<view class="item-content">
					<image src="../../static/images/ic_print_icon.png" mode="aspectFill"></image>
					<text>修改密码</text>
				</view>
				<image src="../../static/images/ic_arrow_right.png" mode="aspectFill"></image>
			</view>
			<view class="login-btn" @click="doLoginOrOut">{{isLogin?'退出登录':'登录'}}</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				userIcon: "/static/images/ic_user_default.png",
				userName: "昵称",
				token: "",
				isLogin: false,
				imgUrl: this.$api.common.imgUrl,
				userInfo: {},
				isRefresh: false,
				loginOut: false,
				
			}
		},
		onLoad(res) {
			this.token = getApp().globalData.token
		},
		onShow() {
			var _self = this
			this.token = getApp().globalData.token
			if (this.token != "") {
				if (!this.isLogin && JSON.stringify(this.userInfo) == "{}") {
					this.isLogin = true;
					this.getUserInfo()
				}
				if (_self.isRefresh) {
					_self.isRefresh = false;
					_self.getUserInfo();
				}
				if (_self.loginOut) {
					_self.loginOut = false
					uni.removeStorage({
						key: 'token',
						success() {
							getApp().globalData.token = "";
							_self.userName = "义务植树";
							_self.userIcon = "/static/images/ic_user_default.png";
							_self.token = "";
							_self.isLogin = false;
						}
					})
					_self.userInfo = {}
					uni.removeStorage({
						key: 'userInfo',
						success() {}
					})
				}
			}

		},
		methods: {
			openPage(url) {
				uni.navigateTo({
					url: url
				})
			},
			choseImg() {
				var _self = this
				if (_self.isLogin) {
					uni.chooseImage({
						count: 1, //默认9
						sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
						sourceType: ['album'], //从相册选择
						success: function(res) {
							uni.showLoading({
								title: "头像上传中...",
							})
							var startIndex = res.tempFilePaths[0].lastIndexOf("/");
							var fileName = res.tempFilePaths[0].substring(startIndex + 1, res.tempFilePaths[0]
								.length);
							uni.uploadFile({
								url: _self.$api.common.doUploadImg,
								filePath: res.tempFilePaths[0],
								name: 'image',
								formData: {
									"image": fileName
								},
								header: {
									"token": _self.token
								},
								success: res => {
									if (res.statusCode == 200) {
										var imgData = res.data;
										var imgIdObj = JSON.parse(imgData);
										_self.userIcon = _self.imgUrl + imgIdObj.data
										_self.userInfo.avatar = imgIdObj.data
										uni.setStorageSync('userInfo', _self.userInfo);
										_self.changeUserInfo(_self.userInfo);

									} else {
										uni.hideLoading()
										uni.showToast({
											title: "上传失败,请重试",
											duration: 1000
										})
									}
								},

								fail: (error) => {
									uni.hideLoading()
									uni.showToast({
										title: "上传失败,请重试",
										duration: 1000
									})
								}
							});
						}
					});
				} else {
					uni.setStorageSync("loginOriginUrl", "/pages/user/index");
					uni.navigateTo({
						url: "/pages/user/login"
					})
				}
			},
			changeUserInfo(uStr) {
				var _self = this;
				uni.request({
					method: 'PUT',
					url: _self.$api.user.doUpdateUserIcon,
					data: {
						'email': uStr.email,
						'name': uStr.name,
						'phone': uStr.phone,
						'avatar': uStr.avatar
					},
					header: {
						'token': _self.token,
					},
					success(res) {
						uni.showToast({
							title: "上传成功",
							duration: 1000
						})
					},
					fail(error) {
						uni.hideLoading()
						uni.showToast({
							title: '上传失败,请重试',
							duration: 1000
						})
					}
				})
			},
			changePwd() {
				if (this.isLogin) {
					uni.navigateTo({
						url: './changePwdPage'
					})
				} else {
					uni.setStorageSync("loginOriginUrl", "/pages/user/index");
					uni.navigateTo({
						url: "/pages/user/login"
					})
				}
			},
			changeInfo() {
				if (this.isLogin) {
					uni.navigateTo({
						url: './changeMineInfo'
					})
				} else {
					uni.setStorageSync("loginOriginUrl", "/pages/user/index");
					uni.navigateTo({
						url: "/pages/user/login"
					})
				}
			},
			getUserInfo() {
				var _self = this;
				_self.$app.request({
					url: _self.$api.user.getUserInfo,
					method: 'GET',
					header: {
						token: getApp().globalData.token
					},
					success: res => {
						if (res) {
							console.log(res)
							getApp().globalData.userInfo = res;
							_self.userInfo = res
							uni.setStorageSync("userInfo", res);
							_self.userName = _self.userInfo.name
							if (_self.userInfo.avatar != "") {
								_self.userIcon = _self.imgUrl + _self.userInfo.avatar
							}
						}
					},
					complete: res => {

					}
				})
			},
			doLoginOrOut() {
				if (this.isLogin) {
					this.logout()
				} else {
					uni.setStorageSync("loginOriginUrl", "/pages/user/index");
					uni.navigateTo({
						url: "/pages/user/login"
					})
				}
			},
			logout() {
				var _self = this;
				uni.showModal({
					title: '提示',
					content: '确认退出吗？',
					showCancel: true,
					success(res) {
						if (res.confirm) {
							uni.removeStorage({
								key: 'token',
								success() {
									getApp().globalData.token = "";
									_self.token = "";
									_self.isLogin = false;
								}
							})
							uni.removeStorage({
								key: 'userInfo',
								success() {
									uni.reLaunch({
										url: '/pages/news/index'
									});
								}
							})
						}
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		height: 100%;

	}

	.page {
		display: flex;
		flex: 1;
		flex-direction: column;
		overflow: auto;
		height: 100%;
		background: #F2F2F2;
	}

	.avatar {
		width: 95%;
		display: flex;
		flex-direction: row;
		align-self: center;
		align-items: center;
		margin-top: 50rpx;

		.icon {
			width: 130rpx;
			height: 130rpx;
			border-radius: 50%;
		}

		.text {
			display: flex;
			flex-direction: column;
			margin-left: 20rpx;

			.name {
				font-size: 40rpx;
				font-weight: 200;
				color: black;
				margin-left: 10rpx;
			}


			.btn {
				display: flex;
				flex-direction: row;
				padding: 8rpx 35rpx;
				background: #fff;
				box-shadow: 0 4rpx 8rpx 0 rgba(0, 0, 0, 0.05), 0 6rpx 20rpx 0 rgba(0, 0, 0, 0.03);
				border-radius: 30rpx;
				align-items: center;

				image {
					width: 30rpx;
					height: 30rpx;
				}
			}
		}
	}

	.amount {
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: center;
		padding: 30rpx;
		margin-top: 20rpx;

		.item {
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;
			flex: 1;

			.num {
				font-weight: bold;
				font-size: 40rpx;
				color: black;
			}
		}
	}

	.func-box {
		display: flex;
		flex-direction: column;
		padding-top: 30rpx;
		border-top-left-radius: 40rpx;
		border-top-right-radius: 40rpx;
		background-color: #fff;
		height: 100%;

		.item {
			display: flex;
			flex-direction: row;
			justify-content: space-between;
			padding: 20rpx 40rpx;

			.item-content {
				display: flex;
				flex-direction: row;
				align-items: center;

				text {
					font-size: 30rpx;
					margin-left: 20rpx;
				}

				image {
					width: 40rpx;
					height: 40rpx;
				}
			}

			image {
				width: 30rpx;
				height: 30rpx;
			}
		}
	}

	.line {
		display: bock;
		width: 100%;
		height: 1rpx;
		margin-top: 40rpx;
		background: #e8e8e8;
	}

	.login-btn {
		text-align: center;
		padding: 20rpx;
		color: white;
		margin: 40rpx;
		background: #169602;
		border-radius: 10rpx;
	}
</style>
