<template>
	<view class="page__view">
		<navbar />
		
		<view class="flex flex-column header">
			<image class="icon" :src="configList.app_logo" mode="widthFix"></image>
			<!-- <image class="icon" src="@/static/image/icon.png" mode="widthFix"></image> -->
			<view class="name">
				{{ configList.app_name || '' }}
			</view>
			<view class="title">
				申请获取你的头像、昵称
			</view>
		</view>

		<view class="form">
			<view class="form-item">
				<view class="label">
					头像
				</view>
				<view class="content">
					<button class="btn-avatar" :plain="true" :hairline="false" open-type="chooseAvatar" @chooseavatar="onChooseAvatar">
						
						<image class="avatar" v-if="userInfoForm.headImage" :src="userInfoForm.headImage" mode=""></image>

						<image class="avatar" v-else src="@/pages_order/static/auth/avatar-default.png" mode="scaleToFill"></image>
						
					</button>
				</view>
			</view>
			<view class="form-item">
				<view class="label">
					昵称
				</view>
				<view class="content">
					<input type="nickname" placeholder="请输入昵称" style="text-align: right;" id="nickName"
						placeholder-class="uni-placeholder"
						v-model="userInfoForm.nickName" 
					/>
				</view>
			</view>
			<view class="form-item">
				<view class="label">
					手机号
				</view>
				<view class="flex content" style="justify-content: flex-end; column-gap: 31rpx;">
					<input 
						v-model="userInfoForm.phone" 
						style="text-align: right;"
						disabled
					/>
					<button
						:plain="true" :hairline="false"
						class="btn-phone"
						open-type="getPhoneNumber" 
						@getphonenumber="getPhone"
					>
						获取手机号
					</button>
				</view>
			</view>
		</view>
		
		<view class="footer">
			<button class="btn" @click="submit">
				确定
			</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				userInfoForm: {
					headImage: '',
					nickName: '',
					phone: '',
				}
			};
		},
		onShow() {},
		onLoad() {
			this.userInfoForm.nickName = this.userInfo.nickName || ''
			this.userInfoForm.headImage = this.userInfo.headImage || ''
		},
		computed: {},
		methods: {
			onChooseAvatar(res) {
				this.$Oss.ossUpload(res.target.avatarUrl)
				.then(url => {
					this.userInfoForm.headImage = url
				})
			},
			getPhone(e){
				this.$api('bindPhone', {
					phoneCode : e.detail.code
				}, res => {
					if(res.code == 200){
						let phoneObj = JSON.parse(res.result)
						
						if(phoneObj.errmsg == 'ok'){
							this.userInfoForm.phone = phoneObj.phone_info.phoneNumber
						}else{
							uni.showModal({
								title: phoneObj.errmsg
							})
						}
					}
				})
			},
			submit() {
				let self = this

				uni.createSelectorQuery().in(this)
					.select("#nickName")
					.fields({
						properties: ["value"],
					})
					.exec((res) => {
						const nickName = res?.[0]?.value
						self.userInfoForm.nickName = nickName
						
						if (self.$utils.verificationAll(self.userInfoForm, {
								headImage: '请选择头像',
								nickName: '请填写昵称',
								phone: '请填写手机号',
							})) {
							return
						}
						
						self.$api('updateInfo', {
							headImage : self.userInfoForm.headImage,
							nickName : self.userInfoForm.nickName,
							phone : self.userInfoForm.phone,
						}, res => {
							if (res.code == 200) {
								uni.reLaunch({
									url:'/pages/index/index'
								})
							}
						})
					})
				
			},
		}
	}
</script>

<style lang="scss" scoped>

	.page__view {
		display: flex;
		justify-content: flex-start;
		align-items: center;
		height: 100vh;
		flex-direction: column;
		position: relative;
		
		.header{
			padding: 187rpx 0 106rpx 0;

			.icon {
				width: 100rpx;
				height: auto;
			}

			.name {
				margin: 20rpx 0 14rpx 0;
				font-size: 36rpx;
				font-family: PingFang SC, PingFang SC-Bold;
				font-weight: 700;
				text-align: center;
				color: #000000;
			}
			
			.title {
				font-size: 30rpx;
				font-family: PingFang SC, PingFang SC-Regular;
				font-weight: 400;
				color: #000000;
			}
		}

		.form {
			padding: 0 48rpx;
			box-sizing: border-box;
			width: 100%;

			.form-item {
				display: flex;
				align-items: center;
				justify-content: space-between;
				width: 100%;
				min-height: 119rpx;
				padding: 0 22rpx 0 13rpx;
				position: relative;
				border-top: 1rpx solid rgba($color: #C7C7C7, $alpha: 0.33);
				
				&:last-child {
					border-bottom: 1rpx solid rgba($color: #C7C7C7, $alpha: 0.33);
				}

				.label {
					white-space: nowrap;
					font-size: 32rpx;
					font-weight: 400;
					text-align: left;
					color: #000000;
				}

				.content {
					flex: 1;
					text-align: right;
				}
			}

			.btn-avatar {
				background: transparent;
				border: none;
				border-radius: none;
				box-shadow: none;
				padding: 0;
				margin: 0;
				font-size: 0;
				text-align: right;
			}

			.avatar {
				display: inline-block;
				width: 97rpx;
				height: 97rpx;
			}

			.btn-phone {
				display: inline-block;
				width: auto;
				margin: 0;
				padding: 23rpx 33rpx;
				font-size: 30rpx;
				line-height: 1.4;
				color: #FFFFFF;
				background: #014FA2;
				border: none;
				border-radius: 38rpx;
			}

		}

		.btn {
			text-align: center;
			margin-top: 155rpx;
				
			width: 80%;
			height: 100rpx;
			border-radius: 50rpx;
			background-image: linear-gradient(to right, #84A73F, #D8FF8F);
			color: #fff;
			
			display: flex;
			justify-content: center;
			align-items: center;
			border-radius: 50rpx;
			border: none;
		}
	}

	.footer {
		margin-top: 200rpx;
		width: 100%;
		padding: 0 78rpx;
		box-sizing: border-box;

		.btn {
			width: 100%;
			padding: 25rpx 0;
			box-sizing: border-box;
			font-size: 32rpx;
			color: #FFFFFF;
			background: #014FA2;
			border-radius: 41rpx;
		}
	}
</style>
