<template>
	<view>
		<navBar leftText="信息完善"></navBar>
		<image class="topBg absolute" src="/static/image/index/conceptTopBg.png" mode="widthFix"></image>
		<view class="avatarC fixed" :style="'top:'+avatarTop+'px;'">
			<image class="avatar" :src="avatar?avatar:avatarUrl" mode="aspectFill"></image>
			<image class="icon absolute" src="/static/icon/mine/basicInfo/edit.png" mode="widthFix"></image>
			<button class="absolute" open-type="chooseAvatar" @chooseavatar="chooseAvatar"></button>
		</view>
		<view class="basicInfoC fixed" :style="'top:'+contentTop+'px;'">
			<view class="title">基本信息</view>
			<view class="section">昵称</view>
			<view class="inputC">
				<input type="nickname" v-model="nickname" />
			</view>
			<view class="section">手机号</view>
			<view class="selectC flex relative">
				{{mobile}}
				<button class="absolute" open-type="getPhoneNumber" @getphonenumber="getPhoneNumData"></button>
			</view>
			<view class="section">性别</view>
			<picker mode="selector" :range="sexList" @change="sexChange">
				<view class="selectC flex">
					<view class="flex1">{{sex}}</view>
					<image class="arrow" src="/static/icon/mine/basicInfo/downArrow.png" mode="widthFix"></image>
				</view>
			</picker>
			<view class="section">个人简介</view>
			<view class="textareaC">
				<textarea v-model="profile" placeholder="简单介绍一下自己吧" placeholder-style="color:#AAA"></textarea>
			</view>
		</view>
		<view class="next fixed" @click="clickNext()">下一步</view>
	</view>
</template>

<script>
	import { inject } from 'vue';
	import { updateBasicInfo, userInfo, wxBindPhone } from '@/request/api';
	import { uploadImg } from '@/utils/util';

	export default {
		data() {
			return {
				avatarTop:'',
				contentTop:'',
				avatar:'',		//选择头像临时地址
				avatarUrl:'',	//已上传的链接
				nickname:'',   //昵称
				mobile:'',    //手机号
				sexList:['男','女'],
				sex:'',
				profile:'',			//个人简介
			}
		},
		onLoad() {
			this.avatarTop = inject('navHeight') + 60/750*inject('windowWidth')
			this.contentTop = this.avatarTop + 75/750*inject('windowWidth')
			this.getUserInfoData()
		},
		methods: {
			/**
			 * 点击选择头像
			 * @param {Object} e
			 */
			chooseAvatar(e)
			{
				console.log(e)
				this.avatar = e.detail.avatarUrl
			},
			/**
			 * 点击选择性别
			 * @param {Object} e
			 */
			sexChange(e)
			{
				console.log(e)
				let value = e.detail.value
				this.sex = this.sexList[value]
			},
			/**
			 * 获取手机号
			 * @param {Object} e
			 */
			getPhoneNumData(e)
			{
				console.log(e)
				if (e.detail.errMsg == 'getPhoneNumber:ok') {
					let param = {code:e.detail.code}
					wxBindPhone(param).then(res=>{
						if (res.code==200) {
							uni.showToast({
								title:res.message,
							})
							this.mobile = res.data.phone
						}else
							uni.showToast({
								title:res.message,
								icon:'none'
							})
					}).catch(error=>{
						
					})		
				}
			},
			/**
			 * 点击下一步
			 */
			clickNext()
			{
				if(!this.avatar&&!this.avatarUrl)
				{
					uni.showToast({
						title:'请选择头像',
						icon:'none'
					})
					return
				}
				if(!this.nickname)
				{
					uni.showToast({
						title:'请输入昵称',
						icon:'none'
					})
					return
				}
				if(!this.sex)
				{
					uni.showToast({
						title:'请选择性别',
						icon:'none'
					})
					return
				}
				if(!this.profile)
				{
					uni.showToast({
						title:'请输入个人简介',
						icon:'none'
					})
					return
				}
				if(this.avatar)
				{
					//如果选择了新头像
					let param = {avatar:this.avatar,nickname:this.nickname,gender:this.sex=='男'?'male':'female',phone:this.mobile,
					bio:this.profile,isFile:1}
					updateBasicInfo(param).then(res=>{
						uni.navigateTo({
							url:'/pages/mine/investPreference/investPreference'
						})
					}).catch(error=>{
						
					})
				}else 
				{
					//没有选择新头像
					let param = {nickname:this.nickname,gender:this.sex=='男'?'male':'female',phone:this.mobile,
					bio:this.profile}
					updateBasicInfo(param).then(res=>{
						uni.navigateTo({
							url:'/pages/mine/investPreference/investPreference'
						})
					}).catch(error=>{
						
					})
				}
			},
			/**
			 * 获取用户信息数据
			 */
			getUserInfoData()
			{
				userInfo().then(res=>{
					if(res.code==200)
					{
						this.avatarUrl = res.data.basic_info.avatar_url
						this.nickname = res.data.basic_info.nickname
						this.mobile = res.data.basic_info.phone
						this.sex = res.data.basic_info.gender=='male'?'男':'女'
						this.profile = res.data.basic_info.bio
					}else 
						wx.showToast({
							title:res.message,
							icon:'none'
						})
				}).catch(error=>{
					
				})
			}
		}
	}
</script>

<style lang="less">
.topBg 
{
	top: 0;
	left: 0;
	width: 100%;
	height: auto;
}
.avatarC 
{
	left: 0;
	margin-left:calc((100% - 150rpx)/2);
	width: 150rpx;
	.avatar 
	{
		width: 100%;
		height: 150rpx;
		border-radius: 50%;
		border: solid 2rpx white;
	}
	.icon 
	{
		right: 20rpx;
		bottom: 0;
		width: 40rpx;
		height: auto;
	}
	button 
	{
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
	}
	z-index: 10;
}
.basicInfoC
{
	background-color: white;
	left: 0;
	right: 0;
	bottom: calc(73rpx + 17rpx + 80rpx);
	border-radius: 20rpx 20rpx 0 0;
	overflow-y: scroll;
	.title 
	{
		margin: 150rpx 0 0 60rpx;
		font-size: 36rpx;
		font-weight: bold;
	}
	.section
	{
		margin: 0 60rpx;
		line-height: 66rpx;
		font-size: 26rpx;
		font-weight: bold;
	}
	.inputC 
	{
		background-color: #FBFBFD;
		margin: 0 60rpx;
		padding: 0 28rpx;
		height: 78rpx;
		border: solid 2rpx #EFEFF2;
		border-radius: 10rpx;
		input 
		{
			height: 100%;
			font-size: 24rpx;
			font-weight: 500;
			color: #555;
		}
	}
	.selectC 
	{
		background-color: #FBFBFD;
		margin: 0 60rpx;
		padding: 0 25rpx 0 28rpx;
		height: 78rpx;
		border: solid 2rpx #EFEFF2;
		border-radius: 10rpx;
		font-size: 24rpx;
		font-weight: 500;
		color: #555;
		.arrow 
		{
			width: 16rpx;
			height: auto;
		}
		button 
		{
			top: 0;
			left: 0;
			width: 100%;
			height: 100%;
		}
	}
	.textareaC
	{
		background-color: #FBFBFD;
		margin: 0 60rpx;
		padding: 20rpx 0 0 28rpx;
		border: solid 2rpx #EFEFF2;
		border-radius: 2rpx;
		textarea 
		{
			height: 200rpx;
			font-size: 24rpx;
			font-weight: 500;
			color: #555;
		}
	}
}
.next 
{
	background-color: #F97316;
	left: 0;
	right: 0;
	bottom: 73rpx;
	margin: 0 25rpx;
	line-height: 80rpx;
	border-radius: 20rpx;
	font-size: 26rpx;
	font-weight: 500;
	color: white;
	text-align: center;
}
</style>
