<template>
	<view class="register">
	
		<view class="content">
			<!-- 头部logo -->
			<view class="header">
				<image :src="fetch.avatar" @click="avatarSet(0)"></image>
				<avatar @upload="avatarUpload" ref="avatar"></avatar>
			</view>
			<!-- 主体 -->
			<view class="main">
				<wInput
					v-model="fetch.name"
					type="text"
					placeholder="昵称"
				></wInput>
				
				<wInput
					v-model="fetch.info"
					type="text"
					placeholder="介绍"
				></wInput>
				
			</view>
				
			<wButton 
				class="wbutton"
				text="修改资料"
				:rotate="isRotate" 
				@click.native="update()"
			></wButton>
			
			<!-- 其他登录 -->
			<view class="other_login cuIcon">
				<view class="login_icon">
					<view class="cuIcon-weixin" @tap="bind_weixin"></view>
				</view>
			</view>
			
			<!-- 底部信息 -->
			<view class="footer">
				<navigator url="forget" open-type="navigate" @click="index">返回首页</navigator>
				<text>|</text>
				<navigator url="register" open-type="navigate" @click="tixian">余额提现</navigator>
			</view>
		</view>
	</view>
</template>

<script>
	import wInput from '@/lib/watch-login/watch-input.vue' //input
	import wButton from '@/lib/watch-login/watch-button.vue' //button
	import avatar from "@/lib/yq-avatar/yq-avatar.vue";
	export default {
		data() {
			return {
				fetch : {
					name : '',
					avatar : '',
					sex : 3,
				},
				isRotate: false, //是否加载旋转
				openAvatar : false
			}
		},
		components:{
			wInput,
			wButton,
			avatar
		},
		// 重新加载
		onPullDownRefresh: function() {
			this.getData();
		},
		onLoad() {
			this.Dever.checkLogin();
			this.getData();
		},
		methods: {
			getData : function() {
				this.Dever.get(this, 'app/user/?l=api.getBaseUser');
			},
			bind_weixin : function() {
				var self = this;
				this.Dever.confirm('即将开始微信授权，授权后您的微信信息将覆盖现有个人信息，但不会影响您已设置好的某个合小记中的个人信息。', function() {
					if (self.Dever.source == 'h5') {
						var host = self.Dever.api_host + 'package/oauth/?l=request.auth&account=1&system=1&system_source=' + self.Dever.source_id + '&signature=' + self.Dever.getToken();
						self.Dever.location(host, 'go');
					} else if (self.Dever.source == 'wx_applet') {
						uni.authorize({
						    scope: 'scope.userInfo',
						    success() {
						        uni.login({
						          provider: 'weixin',
						          success: function (loginRes) {
						            console.log(loginRes.authResult);
						            // 获取用户信息
						            uni.getUserInfo({
						              provider: 'weixin',
									  lang : 'zh_CN',
						              success: function (infoRes) {
						                console.log('用户昵称为：' + infoRes.userInfo.nickName);
						              }
						            });
						          }
						        });
						    }
						})
					} else {
						self.Dever.alert('暂时不支持app登录');
					}
				});
			},
			index : function() {
				this.Dever.location('index/index');
			},
			tixian : function() {
				this.Dever.alert('敬请期待');
			},
		    update : function() {
				if(this.isRotate){
					//判断是否加载中，避免重复点击请求
					return false;
				}
				if (!this.fetch.name) {
					this.Dever.alert('昵称不能为空');
				    return false;
				}
				
				this.isRotate = true;
				var self = this;
				this.Dever.post('package/passport/?l=app.update', {noloading:1, username: this.fetch.name, info: this.fetch.info, gender: this.fetch.sex}, function(t) {
					self.isRotate = false;
					self.Dever.alert('已保存资料，仅影响未修改过信息的合小记');
				}, function(t) {
					self.Dever.alert(t.msg);
					self.isRotate = false;
				});
		    },
			avatarUpload : function(e) {
				var self = this;
				this.openAvatar = false;
				
				this.Dever.uploadFile(e.path, 'avatar', 1, function(type, file) {
					self.fetch.avatar = file;
					self.Dever.post('package/passport/?l=app.update', {avatar:file, noloading:1});
				});
			},
			avatarSet : function(index) {
				this.openAvatar = true;
				this.$refs.avatar.fChooseImg(index,{
					selWidth: "300rpx", selHeight: "300rpx",
					expWidth: '260rpx', expHeight: '260rpx',
					avatarSrc : this.fetch.avatar,
					bgImage : this.fetch.avatar,
					noTab : true,
					
				});
			}
		}
	}
</script>

<style>
	@import url("@/lib/watch-login/css/icon.css");
	@import url("@/lib/watch-login/css/main.css");
	.other_agree {
		display: flex;
		flex-direction: row;
		justify-content: center;
		align-items: center;
		margin-top: 50rpx;
		text-align: center;
	}
	.other_login {
		margin-top: 80rpx;
	}
</style>