<template>
	<view >
		<view class="content">
			<view class="info-item">
				<view class="label">头像</view>
				<view class="right">
					<image  class="head"   :src="user.avatar==null?'https://cdhyt-1383856587.cos.ap-chengdu.myqcloud.com/shop/images/detault_head.jpg':user.avatar" mode=""></image>
					<button class="wx-head" type="balanced" open-type="chooseAvatar" @chooseavatar="onChooseAvatar">
					</button>
				</view>
			</view>
			<view class="info-item">
				<view class="label">昵称</view>
				<view class="right">
					<input type="nickname" @blur="bindblur" @input="bindinput"    v-model="user.nickname" class="input"></text>
				</view>
			</view>
			<view class="info-item">
				<view class="label">会员等级</view>
				<view class="right">{{user.level==1?'VIP会员':'普通会员'}}</view>
			</view>
			<view class="info-item">
				<view class="label">推广员</view>
				<view class="right">{{user.isPromoter==1?'是':'否'}}</view>
			
			</view>
			<view class="info-item">
				<view class="label">手机号</view>
				<view class="right" v-if="user!=null">{{utils.parsePhone(user.phone)}}</view>
			</view>
		</view>
		<view class="btn-box">
			<view class="sub-btn" @click="submit()">保存</view>
		</view>
	</view>
	
</template>

<script>
	import {getUserInfo,editUser} from '@/api/user'
	export default {
		data() {
			return {
				user: null 
			}
		},
		onLoad() {
			this.getUserInfo()
		},
		methods: {
			bindblur(e) {
				this.user.nickname = e.detail.value; // 获取微信昵称
			},
			bindinput(e){
				this.user.nickname = e.detail.value; //这里要注意如果只用blur方法的话用户在输入玩昵称后直接点击保存按钮，会出现修改不成功的情况。
			},
			onChooseAvatar(e){
				let {
					avatarUrl
				} = e.detail;
				uni.uploadFile({
					url: uni.getStorageSync('requestPath')+'/app/common/uploadOSS', //仅为示例，非真实的接口地址
					filePath: avatarUrl,
					name: 'file',
					formData: {
						'user': 'test'  // 上传附带参数
					},
					success: (uploadFileRes) => {
						this.user.avatar =JSON.parse(uploadFileRes.data).url
					}
				});
			},
			submit(){
				editUser(this.user).then(
					res => {
						if(res.code==200){
							uni.showToast({
								icon:'success',
								title: "修改成功",
							});
							setTimeout(function(){
								uni.navigateBack({
									delta:1,//返回层数，2则上上页
								})
							},2000);
						}else{
							uni.showToast({
								icon:'none',
								title: res.msg,
							});
						}
					},
					rej => {}
				);
			},
			getUserInfo(){
				getUserInfo().then(
					res => {
						if(res.code==200){
							if(res.user!=null){
								this.user=res.user;
							}
							 
							
						}else{
							uni.showToast({
								icon:'none',
								title: "请求失败",
							});
						}
					},
					rej => {}
				);
			},
			 
		}
	}
</script>

<style lang="scss" scoped>
	.content{
		padding-top: 20rpx;
	}
	.info-item{
		height: 104upx;
		background: #FFFFFF;
		padding: 0 30upx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		border-bottom: 1px solid #F5F6FA;
		&:last-child{
			border-bottom: none;
		}
		.label{
			font-size: 30upx;
			font-family: PingFang SC;
			font-weight: 400;
			color: #0F1826;
		}
		.right{
			display: flex;
			align-items: center;
			justify-content: center;
			position: relative;
			.wx-head{
				position: absolute;
				width: 80upx;
				height: 80upx;
				opacity: 0;
			}
			.text{
				font-size: 30upx;
				font-family: PingFang SC;
				font-weight: 400;
				color: #0F1826;
			}
			.head{
				border-radius: 50%;
				width: 80upx;
				height: 80upx;
			}
			.input{
				text-align: right;
				font-size: 30upx;
				font-family: PingFang SC;
				font-weight: 400;
				color: #0F1826;
			}
		}
		 
	}
	.btn-box{
		margin-top: 20rpx;
		height: 120upx;
		padding: 0 30upx;
		display: flex;
		align-items: center;
		justify-content: center;
		.sub-btn{
			width: 100%;
			height: 88upx;
			line-height: 88upx;
			text-align: center;
			font-size: 30upx;
			font-family: PingFang SC;
			font-weight: bold;
			color: #FFFFFF;
			background: #2BC7B9;
			border-radius: 44upx;
		}
	}
</style>
