<template>

	<view class="my">
		<view class="topCard" :style="{backgroundImage:'url('+imgurl+')'}">
			<!-- 用户名 -->
			<view class="user">
				<view v-if="admin.avatar">
					<button class="avatar" open-type="chooseAvatar" @click="handleGetWechatUserInfo">
						<image class="user-avatar" style="height: 176rpx;width: 176rpx;border-radius: 50%;"
							:src="admin.avatar? admin.avatar : '/static/icon/avatar.png'" />
					</button>
					<view class="userinfo">
						<text class="unlogin">{{admin.nickname}}</text>
					</view>
				</view>
				<view v-else>
					<image class="user-avatar" src="/static/icon/avatar.png" style="height: 176rpx;width: 176rpx;">
					</image>
					<view class="userinfo">
						<text class="unlogin" v-if="!islogin" @click="login">请点击登录</text>
					</view>
				</view>
				<image src="/static/icon/setting.png" @click="goSetting"
					style="width: 44rpx;height: 44rpx; position: absolute;top:0rpx;right: 24rpx;"></image>
			</view>

			<!-- 会员 -->
			<view class="membership" @click="goJoin">
				<view class="flex">
					<view style="display: flex;align-items: center;">
						<image style="width: 68rpx ;height: 62rpx;" src="../../static/icon/vip.png"></image>
						<text class="join">加入会员</text>
					</view>

					<view style="display: flex;align-items: center;">
						<text class="gojoin">去加入</text>
						<image style="width: 12rpx ;height: 20rpx;" src="../../static/icon/right05.png"></image>
					</view>
				</view>
			</view>

			<!-- 订单 -->
			<view class="orders">
				<view class="nav">
					<view class="myorder">我的订单</view>
					<view class="more">
						<text style="margin-right: 8rpx;" @click="goMyorders(0)">查看更多</text>
						<image style="width: 12rpx ;height: 20rpx;" src="../../static/icon/right05.png"></image>
					</view>
				</view>

				<view class="content">
					<view class="flexbox" @click="goMyorders(1)">
						<view
							style="width: 56rpx; height: 56rpx; display: flex;align-items: center;justify-content: center; ">
							<image style="width: 56rpx ;height: 42rpx;" src="../../static/icon/exchange.png"></image>
						</view>
						<text>待兑换</text>
					</view>
					<view class="flexbox" @click="goMyorders(2)">
						<view
							style="width: 56rpx; height: 56rpx;display: flex;align-items: center;justify-content: center; ">
							<image style="width: 54rpx ;height: 52rpx;" src="../../static/icon/shipments.png"></image>
						</view>
						<text>待发货</text>
					</view>
					<view class="flexbox" @click="goMyorders(3)">
						<view
							style="width: 56rpx; height: 56rpx;display: flex;align-items: center;justify-content: center; ">
							<image style="width: 56rpx ;height: 56rpx;" src="../../static/icon/Receipt.png"></image>
						</view>
						<text>待收货</text>
					</view>
					<view class="flexbox" @click="goMyorders(4)">
						<view
							style="width: 56rpx; height: 56rpx;display: flex;align-items: center;justify-content: center; ">
							<image style="width: 54rpx ;height: 54rpx;" src="../../static/icon/finish.png"></image>
						</view>
						<text>已完成</text>
					</view>
				</view>
			</view>
		</view>

		<!-- 列表 -->
		<view style="padding:0 30rpx;">
			<view class="list" v-for="(item,index) in list" @click="goDetail(index)">
				<view class="title">{{item.title}}</view>
				<view class="container">{{item.content}}</view>
				<image src="../../static/icon/right.png"></image>
			</view>

		</view>
		<u-modal :show="modal.show" :title="modal.title" :show-confirm-button="false">
			<view>
				<button class="avatar" open-type="chooseAvatar" @chooseavatar="handleChooseAvatar">
					<image class="user-avatar" style="height: 176rpx;width: 176rpx;border-radius: 50%;"
						:src="modal.avatar?modal.avatar:'/static/icon/avatar.png'" />
				</button>
				<input class="avatar" type="nickname" :value="modal.nickname" placeholder="填写昵称"
					@change="handleChangeNickname">
				<button class="confirm" @click="handleConfirmWechatUserInfo">提交</button>
			</view>
		</u-modal>
		<!-- 底部导航栏 -->
		<view style="height: 140rpx; width: 100%;"></view>
		<tab-bar checked="my"></tab-bar>
	</view>
</template>

<script>
	// 引入组件
	import TabBar from '../../components/TabBar/tabbar.vue'
	export default {
		// 注册组件
		components: {
			TabBar
		},
		data() {
			return {
				modal: {
					show: false,
					title: '用户信息获取',
					nickname: '',
					avatar: ''
				},
				admin: '',
				// 顶部背景图
				imgurl: 'http://t9.9026.com/imgs/bg@2x.png',
				// 是否登录
				islogin: false,
				// 列表内容
				list: [{
					title: '积分',
					content: '积分兑换奖品'
				}, {
					title: '奖品',
					content: '查看所有奖品'
				}, {
					title: '点赞',
					content: '查看所有点赞'
				}]
			};
		},

		onShow() {
			this.admin = this.$store.getters.userInfo
		
		},

		methods: {
			// 微信获取头像
			handleGetWechatUserInfo() {
				console.log(1111);
				this.modal.show = true
				this.modal.nickname = this.admin.nickname
				this.modal.avatar = this.admin.avatar
			},
			//提交微信名称和头像
			handleConfirmWechatUserInfo() {
				if (!this.modal.avatar) {
					this.$u.toast('请上传头像')
					return
				}
				if (!this.modal.nickname) {
					this.$u.toast('请填写昵称')
					return
				}
				this.$loading('数据提交中...')
				this.$api.my.update({avatar: this.modal.avatar, nickname: this.modal.nickname}).then(res=>{
					  this.$hideLoading()  
					  this.$store.dispatch('user/info', res.data)
					  this.admin = this.$store.getters.userInfo
					  this.modal.show = false
				})
			},
			// 微信获取头像
			handleChooseAvatar(e) {
				console.log(e.detail);
				this.modal.avatar = e.detail.avatarUrl
			},
			handleChangeNickname(res) {
				this.modal.nickname = res.detail.value
			},

			// 跳转登录页
			login() {
				uni.navigateTo({
					url: '/pages/login/login'
				})
			},
			// 跳转个人信息
			goPersonData() {
				uni.navigateTo({
					url: '/pages/my/PersonalData/personalData'
				})
			},
			// 跳转设置页面
			goSetting() {
				uni.navigateTo({
					url: '/pages/my/setting/setting'
				})
			},
			// 跳转积分页
			goDetail(index) {
				if (index == 0) {
					uni.navigateTo({
						url: '/pages/my/integral/integral'
					})
				} else if (index == 1) {
					uni.navigateTo({
						url: '/pages/my/prize/prize'
					})
				} else {
					uni.navigateTo({
						url: '/pages/my/Kudos/Kudos'
					})
				}
			},
			// 跳转订单页
			goMyorders(index) {
				switch (index) {
					case 0:
						uni.navigateTo({
							url: '/pages/my/myorders/orders'
						})
						break;
					case 1:
						uni.navigateTo({
							url: '/pages/my/myorders/orders?isActive=' + 1
						})
						break;
					case 2:
						uni.navigateTo({
							url: '/pages/my/myorders/orders?isActive=' + 2
						})
						break;
					case 3:
						uni.navigateTo({
							url: '/pages/my/myorders/orders?isActive=' + 3
						})
						break;
					case 4:
						uni.navigateTo({
							url: '/pages/my/myorders/orders?isActive=' + 4
						})
						break;
				}

			},
			// 跳转其他小程序
			goJoin() {
				wx.navigateToMiniProgram({
					appId: 'wx255b58f0992b3c53', //appid
					path: 'newUIMain/enrollment/enrollment', //path
					extraData: { //参数
						foo: 'bar'
					},
					// envVersion: 'develop', //开发版develop 开发版 trial   体验版 release 正式版 
					success(res) {
						console.log('成功')
						// 打开成功
					},
					fail(e) {
						console.log(e, '失败')
					}
				})
			},
			// 上传头像
			uploadAvatar() {
				// let img = []
				// uni.chooseImage({
				// 	count: 1, //默认9
				// 	sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
				// 	sourceType: ['album'], //从相册选择
				// 	success: function (res) {
				// 		console.log(JSON.stringify(res.tempFilePaths));
				// 		img = res.tempFilePaths[0]
				// 		uni.uploadFile({
				// 			//后端接口地址
				// 			url: `${baseUrl}` + '/api/attachment/upload',
				// 			//图片临时地址
				// 			filePath: img,
				// 			// 上传文件类型
				// 			formData: {
				// 				tag: 'avatar'
				// 			},
				// 			success: (res) => {
				// 				//后端返回的图片名称
				// 				let data = JSON.parse(res.data)
				// 				// _this.formData.avatar = data.data.file
				// 			},
				// 		})
				// 	},
				// });
			}
		}
	}
</script>

<style lang="scss" scoped>
	$pageColor:#F9F9F9;
	$bgColor:#FFFFFF;

	.my {
		height: 100%;
		background-color: $pageColor;
	}

	.avatar {
		width: 176rpx;
		height: 176rpx;
		border-radius: 50%;
	}



	.topCard {
		width: 100%;
		border-radius: 0rpx 0rpx 16rpx 16rpx;
		padding: 40rpx 30rpx 38rpx 30rpx;
		box-sizing: border-box;
		background-position: center;
		background-repeat: no-repeat;
		background-size: cover;


		.user {
			width: 690rpx;
			height: 176rpx;
			position: relative;
			margin-bottom: 38rpx;

			.user-avatar {
				object-fit: cover;
				object-position: center;
			}

			.userinfo {
				position: absolute;
				top: 38rpx;
				left: 200rpx;

				.unlogin {
					width: 220rpx;
					font-size: 44rpx;
					font-family: PingFang-SC-Heavy, PingFang-SC;
					font-weight: 500;
					color: #FFFFFF;
					display: block;
					margin-top: 20rpx;
				}

				.username {
					width: 184rpx;
					height: 44rpx;
					font-size: 44rpx;
					font-family: PingFang-SC-Heavy, PingFang-SC;
					font-weight: 500;
					color: #FFFFFF;
					line-height: 44rpx;
					display: block;
					margin-bottom: 24rpx;
				}

				.userId {
					font-size: 32rpx;
					font-family: PingFang-SC-Medium, PingFang-SC;
					font-weight: 400;
					color: #FFFFFF;
					line-height: 32rpx;
				}
			}
		}

		.membership {
			width: 690rpx;
			height: 110rpx;
			background: $bgColor;
			border-radius: 8rpx;
			display: flex;
			align-items: center;
			padding-left: 28rpx;
			padding-right: 24rpx;
			box-sizing: border-box;
			margin-bottom: 24rpx;

			.flex {
				display: flex;
				align-items: center;
				justify-content: space-between;
				height: 68rpx;
				width: 650rpx;

				.join {
					font-size: 36rpx;
					font-family: PingFang-SC-Bold, PingFang-SC;
					font-weight: 600;
					color: #627885;
					margin-left: 8rpx;
				}

				.gojoin {
					font-size: 30rpx;
					font-family: PingFang-SC-Bold, PingFang-SC;
					font-weight: 600;
					color: #627885;
					margin-right: 8rpx;
				}
			}


		}

		.orders {
			width: 690rpx;
			background: $bgColor;
			border-radius: 8rpx;
			padding: 40rpx 30rpx 36rpx 36rpx;
			box-sizing: border-box;

			.nav {
				height: 50rpx;
				width: 633rpx;
				display: flex;
				align-items: center;
				justify-content: space-between;

				.myorder {
					font-size: 28rpx;
					font-family: PingFang-SC-Heavy, PingFang-SC;
					font-weight: 800;
					color: #333333;
				}

				.more {
					font-size: 28rpx;
					font-family: PingFang-SC-Bold, PingFang-SC;
					font-weight: bold;
					color: #627885;

				}
			}

			.content {
				display: flex;
				align-items: center;
				justify-content: space-between;
				padding: 0 20rpx;
				margin-top: 72rpx;

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

					text {
						margin-top: 16rpx;
						font-size: 28rpx;
						font-family: PingFang-SC-Medium, PingFang-SC;
						font-weight: 500;
						color: #627885;
					}
				}
			}
		}


	}

	.list {
		width: 100%;
		padding-top: 32rpx;
		padding-bottom: 32rpx;
		border-bottom: 2rpx solid #F0F0F0;
		display: flex;
		flex-direction: column;
		justify-content: center;
		position: relative;

		&:first-child {
			padding-top: 50rpx;
		}

		.title {
			font-size: 36rpx;
			font-family: PingFang-SC-Bold, PingFang-SC;
			font-weight: bold;
			color: #333333;
			margin-bottom: 16rpx;
		}

		.container {
			font-size: 28rpx;
			font-family: PingFang-SC-Medium, PingFang-SC;
			font-weight: 500;
			color: #333333;
		}

		image {
			width: 12rpx;
			height: 20rpx;
			position: absolute;
			top: 70rpx;
			right: 0rpx;
		}
	}
</style>
