<template>
	<view class="pages">
		<u-navbar
			title="个人信息"
			:autoBack="true"
			 :safeAreaInsetTop="true"
		>
		</u-navbar>
		<view class="page-wrap">
			<u-cell-group>
				<!-- <u-cell  title="头像">
					<u-avatar slot="right-icon" :src="memberInfo.avatar" size="80rpx"></u-avatar>
				</u-cell> -->
				<view class="avatar u-flex u-row-between u-border-bottom">
					<text>头像</text>
					<view class="right">
						<u--image :src="memberInfo.avatar" shape="circle" width="80rpx" height="80rpx"></u--image>
						<u-upload
							 width="80rpx"
							 height="80rpx"
						    :fileList="fileList"
						    @afterRead="afterRead"
							:previewImage="false"
							multiple
						    name="name"
						    :maxCount="2"
						></u-upload>
					</view>
				</view>
				<u-cell title="昵称" @click="nameShow=true" :value="memberInfo.name"></u-cell>
				<u-cell title="性别" @click="showSex=true" :value="memberInfo.sex|filterSex"></u-cell>
				<u-cell title="生日" :value="memberInfo.birthdayTime||'未设置'" @click="timeShow = true" :isLink="true"></u-cell>
				<u-cell title="会员等级" :value="memberInfo.levelName"></u-cell>
			</u-cell-group>
			<u-button text="取消登录" type="warning" @click="clearStorage" style="margin-top: 30rpx;"></u-button>
			<u-datetime-picker
				:show="timeShow"
				:minDate="new Date().getTime()-365*100*24*3600*1000"
				v-model="birthday"
				:maxDate="new Date().getTime()"
				@confirm="updateBirthday"
				mode="date"
				:closeOnClickOverlay="true"
				@close="timeShow = false"
				@cancel="timeShow = false"
			></u-datetime-picker>
		</view>
		<u-modal :show="nameShow"  title="修改昵称" @confirm="changeName" @cancel="nameShow=false" :showCancelButton="true" >
			<view class="slot-content">
				<u--input
				    placeholder="请输入内容"
				    border="surround"
				    v-model="tempName"
					maxlength="8"
				  >
				</u--input>
			</view>
		</u-modal>
		<u-action-sheet
				:show="showSex"
				:actions="actions"
				title="请选择性别"
				@close="showSex = false"
				@select="sexSelect"
		>
		</u-action-sheet>
	</view>
</template>

<script>
	import {uploadImg} from '../utils/uploadImg.js'
	export default {
		components:{
			
		},
		data() {
			return {
				showSex:false,
				sex:'',
				nameShow:false,
				tempName:'',
				uploadFileUrl:this.$commonConfig.uploadFileUrl,
				timeShow:false,
				birthday:'',
				memberInfo:{},
				fileList:[],
				avatarUrl:'',
				actions: [
					{name: '男',val:1},
					{name: '女',val:2},
					{name: '保密',val:0},
				],
			}
		},
		onShow() {	
			this.getMemberInfo();
			// console.log('1111',this.uploadFileUrl);
		},
		onLoad() {

		},
		methods: {
			clearStorage(){
				uni.clearStorage();
				uni.showToast({
					title:'清楚完成,请手动重新进入小程序',
					icon:'none',
					complete() {
					}
				})
			},
			getMemberInfo(){
				this.$u.api.memberInfo({id:this.vuex_user_info.userid}).then(res=>{
					this.memberInfo = res.data;
					this.avatar =  res.data.avatar;
					console.log('memberInfo',this.memberInfo);
					}).catch(err=>{
					console.log('memberInfo',err.data);
				})
			},
			updateBirthday(e){
				// console.log('000',e);
				let birthday = uni.$u.timeFormat(e.value, 'yyyy-mm-dd');
				this.birthday = birthday;
				this.updateMemberInfo('birthday');
				this.timeShow = false;
			},
			// 新增图片
			async afterRead(event){
				//使用这个封装
				const result = await uploadImg(event,this.fileList)
				let parseResult = JSON.parse(result.data).data;
				console.log('urlurl',parseResult.url);
				this.avatarUrl = parseResult.url;
				if(!this.avatarUrl){
					uni.showToast({
						title: '上传失败!',
						icon: "error"
					});
					return
				};
				this.updateMemberInfo('avatar');
				let item = this.fileList[result.fileListLen]
				this.fileList.splice(result.fileListLen, 1, Object.assign(item, {
					status: 'success',
					message: '成功',
					url: JSON.parse(result.data).data
				}));
			},
			updateMemberInfo(type){
				let params ={
					id:this.vuex_user_info.userid
				};
				if(type == 'avatar'){
					params.avatar = this.avatarUrl;
				}else if(type == 'birthday'){
					params.birthdayTime = this.birthday;
				}else if(type == 'name'){
					if(!this.tempName){
						uni.showToast({
							title:'请输入昵称',
							icon:'error'
						})
						return
					}
					params.name = this.tempName;
				}else if(type == 'sex'){
					params.sex = this.sex;
				}
				this.$u.api.updateMemberInfo(params).then(res=>{
					this.getMemberInfo();
					this.nameShow = false;
				}).catch(err=>{
					console.log('err',err);
				})
			},
			changeName(){
				this.updateMemberInfo('name');
				console.log('memberInfo',this.memberInfo);
			},
			sexSelect(e){
				this.sex = e.val;
				this.updateMemberInfo('sex')
				console.log('sexSelect',e);
			}
		}
	}
</script>
<style>
page{
	background-color: #fff;
}
</style>
<style lang="scss" scoped>
.avatar{
	padding: 10px 15px;
	font-size: 15px;
	color: #303133;
	.right{
		position: relative;
		/deep/ .u-upload{
			position: absolute;
			left: 0;
			top: 0;
			opacity: 0;
		}
	}
}
</style>