<template>
	<view class="default_page adffc" :style="{'height':h+'px', 'padding-top':mt+'px'}">
		<cus-header title='用户信息'></cus-header>
		<view class="info adffcac">
			<image class="info-avatar" :src="userInfo.headUrl||defaultAvatar" @click="changeAvatar"></image>
			<view class="info-text">上传头像</view>
			<view class="info-form">
				<view class="info-form-item adfac">
					<view class="info-form-item-left">姓名</view>
					<view class="info-form-item-right">
						<u-input border="none" v-model="userInfo.realName" placeholder="请输入姓名" style="color: #193D59;font-size: 30rpx;" placeholder-style="color:#99A9B5;font-size: 30rpx;"></u-input>
					</view>
				</view>
				<view class="info-form-item adfac">
					<view class="info-form-item-left">性别</view>
					<view class="info-form-item-right adfac">
						<view class="info-form-item-right-pre adfac" @click="changeGender(0)">
							<image :src="imgBase+'selected.png'" v-if="userInfo.gender==0"></image>
							<image :src="imgBase+'not_select.png'" v-else></image>
							<text>男</text>
						</view>
						<view class="info-form-item-right-pre adfac" @click="changeGender(1)" style="margin-left: 80rpx;">
							<image :src="imgBase+'selected.png'" v-if="userInfo.gender==1"></image>
							<image :src="imgBase+'not_select.png'" v-else></image>
							<text>女</text>
						</view>
					</view>
				</view>
				<view class="info-form-item adfac">
					<view class="info-form-item-left">手机</view>
					<view class="info-form-item-right">{{ jmPhone }}</view>
				</view>
			</view>
		</view>
		<view class="zt_btn" @click="handleSave">保存</view>
	</view>
</template>

<script>
	import { BaseApi } from '@/http/baseApi.js'
	export default {
		data(){
			return {
				defaultAvatar:this.$imgBase+'upload.png',
				userInfo:{
					id:'',
					headUrl:'',
					realName:'',
					gender:null
				},
				jmPhone:''
			}
		},
		onLoad() {
			if(uni.getStorageSync('userInfo')){
				let ui = JSON.parse(uni.getStorageSync('userInfo'));
				this.jmPhone = ui?.mobile?.replace(/(\d{3})\d{4}(\d{4})/, '$1 **** $2');
				this.userInfo.id = ui?.id;
				this.userInfo.headUrl = ui.headUrl;
				this.userInfo.realName = ui.realName;
				this.userInfo.gender = ui.gender;
			}
		},
		methods:{
			changeGender(gender){
				this.userInfo.gender = gender;
			},
			changeAvatar(){
				uni.showActionSheet({
					itemList: ['从手机相册选择', '获取微信头像'],
					success: (res) => {
					  if (res.tapIndex === 0) { // 从手机相册选择
						this.chooseLocalImage();
					  } else if (res.tapIndex === 1) { // 获取微信头像
						this.getWeChatProfile(false, true); // 只获取头像
					  }
					}
				});
			},
			uploadAvatar(url){
				uni.uploadFile({
					url: BaseApi + '/uploadFile',
					filePath: url,
					name: 'file',
					success: (res) => {
						try{
							let data = JSON.parse(res.data);
							if(data.code!==0) return this.$showToast(data.msg)
							this.userInfo.headUrl = data.data||'';
						}catch(e){
							//TODO handle the exception
						}
					},
					fail: (err) => {
						console.log(err,'err');
					}
				});
			},
			// 从本地选择图片并显示
			chooseLocalImage() {
			  uni.chooseImage({
				count: 1, // 只能选择一张
				sizeType: ['compressed'], // 压缩图片
				sourceType: ['album', 'camera'], // 可以从相册或相机选择
				success: (res) => {
				  const tempFilePaths = res.tempFilePaths;
				  if (tempFilePaths && tempFilePaths.length > 0) {
					this.uploadAvatar(tempFilePaths[0]);
				  }
				},
				fail: (err) => {
				  console.error('选择图片失败', err);
				}
			  });
			},
			// 获取微信用户头像和/或昵称
			// @param {boolean} getNameOnly 是否只获取昵称
			// @param {boolean} getAvatarOnly 是否只获取头像
			async getWeChatProfile(getNameOnly = false, getAvatarOnly = false) {
			  uni.getUserProfile({
				desc: '用于完善您的个人资料展示', // 声明获取用户个人信息后的用途，必填
				success: (res) => {
				  console.log('获取微信用户信息成功:', res.userInfo);
				  if (res.userInfo) {
					if (getAvatarOnly) { // 只获取头像
					  this.userInfo.headUrl = res.userInfo.avatarUrl;
					}
				  }
				},
				fail: (err) => {
				  console.error('获取微信用户信息失败:', err);
				}
			  });
			},
			handleSave(){
				if(!this.userInfo.headUrl) return this.$showToast('请上传头像');
				if(!this.userInfo.realName) return this.$showToast('请输入姓名');
				if(this.userInfo.gender==null) return this.$showToast('请选择性别');
				
				this.$api.put('/wx/updateUser',this.userInfo).then(({data:res})=>{
					if(res.code!==0) return this.$showToast(res.msg)
					this.$showToast('保存成功')
					let ui = JSON.parse(uni.getStorageSync('userInfo'));
					ui.headUrl = this.userInfo.headUrl;
					ui.realName = this.userInfo.realName;
					ui.gender = this.userInfo.gender;
					uni.setStorageSync('userInfo',JSON.stringify(ui));
					setTimeout(()=>{
						uni.reLaunch({
							url:'/pages/my'
						})
					},1500)
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.default_page{
		padding: 0 24rpx 306rpx;
		box-sizing: border-box;
		background: #F7F7F7;
		justify-content: space-between;
		
		.info{
			margin-top: 68rpx;
			flex: 1;
			&-avatar{
				width: 168rpx;
				height: 168rpx;
				border-radius: 50%;
			}
			&-text{
				font-family: PingFangSC, PingFang SC;
				font-weight: 400;
				font-size: 30rpx;
				color: #002846;
				line-height: 42rpx;
				margin-top: 24rpx;
				text-align: center;
			}
			&-form{
				width: 100%;
				margin-top: 68rpx;
				background: #FFFFFF;
				border-radius: 16rpx;
				padding: 0 24rpx;
				box-sizing: border-box;
				&-item{
					height: 98rpx;
					box-shadow: inset 0rpx -1rpx 0rpx 0rpx #EFEFEF;
					&-left{
						width: 140rpx;
						font-family: PingFang-SC, PingFang-SC;
						font-weight: bold;
						font-size: 30rpx;
						color: #002846;
						line-height: 42rpx;
					}
					&-right{
						width: calc(100% - 140rpx);
						font-family: PingFangSC, PingFang SC;
						font-weight: 400;
						font-size: 30rpx;
						color: #193D59;
						line-height: 32rpx;
						
						&-pre{
							image{
								width: 36rpx;
								height: 36rpx;
							}
							text{
								font-family: PingFang-SC, PingFang-SC;
								font-weight: bold;
								font-size: 30rpx;
								color: #193D59;
								line-height: 42rpx;
								margin-left: 20rpx;
							}
						}
					}
				}
			}
		}
		
		.zt_btn{
			width: calc(100% - 32rpx);
			margin-left: 16rpx;
		}
	}
</style>