<template>
	<view class="containar">
		<view class="infoList">
			<text>头像</text>
			<view class="avatarUrl">
				<button type="balanced" open-type="chooseAvatar" @chooseavatar="onChooseavatar">
					<image :src="avatarUrl?avatarUrl:'../../static/ucenter/big-avatar.png'" class="refreshIcon"></image>
				</button>
				<view class="dayu" style="line-height: 110rpx;"><image src="../../static/ucenter/dayu.png"></image></view>
			</view>
			
		</view>
		<view class="infoList">
			<text style="width: 400rpx;">昵称</text>
			<input type="nickname" class="weui-input" maxlength="10" :value="nickName" @blur="bindblur" placeholder="请输入昵称"
				@input="bindinput" />
			<view class="dayu" style="padding-top: 10rpx;"><image src="../../static/ucenter/dayu.png"></image></view>
		</view>
		<view class="infoList">
			<text>手机号码</text>
			<view class="lastText">{{userinfo.telephone}} 不可修改</view>
		</view>
		<view class="infoList">
			<text>加入智小狸</text>
			<view class="lastText">{{days}}天</view>
		</view>
		<view class="btn">
			<view class="btn-sub" @click="onSubmit">保存信息</view>
		</view>
	</view>
</template>

<script>
	import {pathToBase64} from 'image-tools'
	export default {
		data() {
			return {
				avatarUrl: '',
				nickName: '小狸',
				imgBase:'',
				days:null,
				userinfo: this.$store.state.users,
			};
		},
		onShow() {
			this.getUserDays()
			this.getUseravatar()
			
		},
		onLoad(option) {},
		methods: {
			getUseravatar(){
				if(this.$store.state.users.nickName){
					this.nickName=this.$store.state.users.nickName
				}
				if(this.$store.state.users.avatar){
					this.avatarUrl=this.$store.state.users.avatar
				}
			},
			//获取加入天数
			getUserDays() {
				this.$http.getDays().then(res => {
					if (res.code === 0) {
						this.days = res.data
					} else {
						this.$utils.msg(res.msg);
					}
				})
			},
			bindblur(e) {
				this.nickName = e.detail.value; // 获取微信昵称
			},
			bindinput(e) {
				this.nickName = e.detail.value; //这里要注意如果只用blur方法的话用户在输入玩昵称后直接点击保存按钮，会出现修改不成功的情况。
			},
			onChooseavatar(e) {
				this.avatarUrl=e.detail.avatarUrl
				pathToBase64(e.detail.avatarUrl).then(res=>{
					this.imgBase=res
				})
			},
			onSubmit() {
				if (this.nickName === '小狸' && this.avatarUrl === '') {
					uni.showToast({
						icon: 'none',
						title: '没有更改，勿提交'
					})
					return false;
				} else {
					let params = {
						nickName: this.nickName,
						avatar: this.imgBase
					}
					this.$store.state.users.avatar = this.avatarUrl
					this.$store.state.users.nickName = this.nickName
					this.$store.commit("UPDATEUSERS", this.$store.state.users)
					this.$http.wxLogin(params).then(res=>{
						if(res.code === 0){
							uni.switchTab({
								url:'/pages/ucenter/index'
							})
						}else{
							this.$utils.msg(res.msg);
						}
					}).catch(error=>{
						this.$utils.msg(error);
					});
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.containar {
		height: 100vh;
		background: #fff;
		display: flex;
		flex-direction: column;
		padding: 0 30rpx;
		padding-top: 30rpx;
		.infoList{
			display: flex;
			width: 100%;
			justify-content: space-between;
			border-bottom: #F2F3F5 solid 1px;
			padding: 30rpx 0rpx;
			align-items: center;
			text{
				font-weight: 600;
				color: #1D2129;
				line-height: 35rpx;
				font-size: 30rpx;
				font-family: PingFang SC-Regular, PingFang SC;
			}
			.dayu image{
				width: 36rpx;
				height: 36rpx;
			}
			.lastText{
				color: #C9CDD3;
				padding-right: 35rpx;
			}
		}

		.avatarUrl {
			display: flex;
			justify-content: center;
			justify-items: center;

			button {
				background: #fff;
				line-height: 80rpx;
				height: 110rpx;
				margin: 0;
				display: flex;
				justify-content: center;
				align-items: center;
				border: none;
				.refreshIcon {
					height: 110rpx;
					width: 110rpx;
					border-radius: 50%;
				}
			}
			button::after{
				border: none;
			}
		}

		.weui-input {
			padding-left: 60rpx;
			color: #86909C;
			font-weight: 500;
			text-align: right;
		}

		.btn {
			width: 100%;
			padding: 0 20rpx;
			box-sizing: border-box;

			.btn-sub {
				margin: 80rpx auto 0;
				height: 90rpx;
				background: #58be6b;
				border-radius: 45rpx;
				line-height: 90rpx;
				text-align: center;
				font-size: 36rpx;
				color: #fff;
			}
		}
	}
</style>
