<template>
	<gui-page :customHeader="true">
		<view slot="gHeader">
			<view class="gui-flex gui-nowrap gui-rows gui-align-items-center gui-padding">
				<!-- 使用组件实现返回按钮及返回首页按钮 -->
				<gui-header-leading :onlyBack="true"></gui-header-leading>
				<!-- 导航文本此处也可以是其他自定义内容 -->
				<text class="gui-h4 gui-ellipsis gui-text-center gui-header-content gui-primary-color">个人资料</text>
				<!-- 如果右侧有其他内容可以利用条件编译和定位来实现-->
			</view>
		</view>
		<view slot="gBody" class="container">
			<view class="brand">
				<image :src="fetch.avatar" @click="avatarSet(0)" class="avatar"></image>
				<avatar @upload="avatarUpload" ref="avatar"></avatar>
				<view class="username">{{fetch.name}}</view>
			</view>
			<view class="user-form">
				<list-cell line-right padding="30rpx">
					<view class="form-item">
						<view class="label">昵称</view>
						<input type="text" v-model="fetch.name"/>
					</view>
				</list-cell>
				<list-cell line-right padding="30rpx">
					<view class="form-item">
						<view class="label">头衔</view>
						<input type="text" v-model="fetch.title"/>
					</view>
				</list-cell>
				<list-cell line-right padding="30rpx">
					<view class="form-item">
						<view class="label">签名</view>
						<input type="text" v-model="fetch.info"/>
					</view>
				</list-cell>
				<list-cell line-right padding="30rpx" style="display: none;">
					<view class="form-item">
						<view class="label">手机号码</view>
						<input type="text" v-model="fetch.mobile" />
						<button type="default" plain class="font-size-sm" @tap="showTelphoneModal">更换手机</button>
					</view>
				</list-cell>
				<list-cell line-right padding="30rpx">
					<view class="form-item">
						<view class="label">性别</view>
						<view class="radio" @tap="fetch.sex = 1">
							<image :src="fetch.sex == 1 ? '/static/images/common/round-black-selected.png' : '/static/images/common/gouxuankuang.png'"></image>
							<view>男</view>
						</view>
						<view class="radio" @tap="fetch.sex = 2">
							<image :src="fetch.sex == 2 ? '/static/images/common/round-black-selected.png' : '/static/images/common/gouxuankuang.png'"></image>
							<view>女</view>
						</view>
						<view class="radio" @tap="fetch.sex = 3">
							<image :src="fetch.sex == 3 ? '/static/images/common/round-black-selected.png' : '/static/images/common/gouxuankuang.png'"></image>
							<view>不显示</view>
						</view>
					</view>
				</list-cell>
				<list-cell line-right padding="30rpx" last>
					<view class="form-item">
						<view class="label">生日</view>
						 <picker class="flex-fill" mode="date" :value="fetch.birthday" @change="handleBirthdayChange">
							<view v-if="fetch.birthday">{{ fetch.birthday }}</view>
							<view v-if="!fetch.birthday">请选择生日</view>
						</picker>
					</view>
				</list-cell>
			</view>
			
			<view class="save-btn">
				<wButton 
					class="wbutton"
					text="修改资料"
					:rotate="isRotate" 
					@click.native="update()"
				></wButton>
			</view>
			
			<modal :show="telphoneModalVisible" custom padding="40rpx 30rpx">
				<view class="telphone-modal">
					<view class="header">
						<view>温馨提示</view>
						<image src="/static/images/common/closex.png" class="close-icon" @tap="closeTelphoneModal"></image>
					</view>
					<view class="tips">
						请在绑定前确定新手机号未绑定合小记。
					</view>
					<view class="telphone-form">
						<list-cell padding="30rpx 0">
							<view class="telphone-input">
								<view class="prefix">+86</view>
								<input type="text" v-model="telphoneForm.telphone" placeholder="请填写有效手机号码" placeholder-class="font-size-base text-color-assist"/>
							</view>
						</list-cell>
						<list-cell padding="30rpx 0">
							<view class="verfiycode-input">
								<input type="text" v-model="telphoneForm.verifyCode" placeholder="6位数字验证码" placeholder-class="font-size-base text-color-assist">
								<view class="countdown" :class="{'text-color-assist': !countdown, 'text-color-base': countdown}" @tap="getVerifyCode">
									{{ !countdown ? '获取验证码' : countdown + '秒可获取' }}
								</view>
							</view>
						</list-cell>
					</view>
					<view class="footer">
						<button type="primary" @tap="closeTelphoneModal">确认</button>
					</view>
				</view>
			</modal>
		</view>
	</gui-page>
</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";
	import listCell from '@/lib/list-cell/list-cell.vue'
	import modal from '@/lib/modal/modal.vue'
	
	export default {
		components: { 
			listCell,
			modal,
			wInput,
			wButton,
			avatar
		}, 
		data() {
			return {
				fetch : {
					name : '',
					avatar : '',
					sex : 3,
				},
				isRotate: false, //是否加载旋转
				openAvatar : false,
				telphoneModalVisible: false,
				telphoneForm: {
					telphone: '',
					verifyCode: ''
				},
				countdown: 0
			}
		},
		// 重新加载
		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, title:this.fetch.title, birthday:this.fetch.birthday}, 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,
					
				});
			},
			handleBirthdayChange({target: {value}}) {
				 this.fetch.birthday = value
			},
			showTelphoneModal() {
				this.telphoneModalVisible = true
			},
			closeTelphoneModal() {
				this.telphoneModalVisible = false
				this.telphoneForm = this.$options.data().telphoneForm
			},
			getVerifyCode() {
				if(this.countdown) return
				
				if(!this.telphoneForm.telphone) {
					uni.showToast({
						title: '请填写手机号码',
						icon: 'none'
					})
					return
				}
				
				this.countdown = 60
				let interval = setInterval(() => {
					if(!this.countdown) {
						clearInterval(interval)
						return
					}
					this.countdown -= 1
				}, 1000)
			}
		}
	}
</script>

<style lang="scss" scoped>
.brand {
	background-color: $bg-color-white;
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	padding: 40rpx 0;
	margin-bottom: 20rpx;
	
	.avatar {
		width: 200rpx;
		height: 200rpx;
		border-radius: 100%;
		margin-bottom: 30rpx;
	}
	
	.username {
		color: $font-size-medium;
		font-weight: bold;
	}
}

.user-form {
	.form-item {
		width: 100%;
		display: flex;
		align-items: center;
		
		.label {
			width: 160rpx;
		}
		
		input {
			flex: 1;
		}
		
		.radio {
			display: flex;
			margin-right: 50rpx;
			image {
				width: 40rpx;
				height: 40rpx;
				margin-right: 20rpx;
			}
		}
	}
}

.save-btn {
	padding: 0 30rpx;
	margin-top: 60rpx;
	
	button {
		width: 100%;
		font-size: $font-size-extra-lg;
	}
}

.telphone-modal {
	.header {
		display: flex;
		align-items: center;
		justify-content: center;
		position: relative;
		font-size: $font-size-extra-lg;
		font-weight: bold;
		margin-bottom: 30rpx;
		
		.close-icon {
			width: 60rpx;
			height: 60rpx;
			position: absolute;
			right: 0;
		}
	}
	
	.tips {
		color: $text-color-grey;
		line-height: 1.2rem;
		margin-bottom: 50rpx;
	}
	
	.telphone-form {
		font-size: $font-size-base;
		
		.telphone-input {
			width: 100%;
			display: flex;
			align-items: center;
			
			.prefix {
				color: $color-primary;
				display: flex;
				align-items: center;
				padding-right: 20rpx;
				position: relative;
				margin-right: 10rpx;
				
				&::after {
					content: " ";
					position: absolute;
					width: 4rpx;
					background-color: $border-color;
				}
			}
			
			input {
				flex: 1;
			}
		}
		
		.verfiycode-input {
			width: 100%;
			display: flex;
			align-items: center;
			justify-content: space-between;
			
			.countdown {
				font-size: $font-size-base;
			}
		}
	}
	
	.footer {
		margin-top: 30rpx;
		button {
			font-size: $font-size-lg;
		}
	}
}
</style>
