<template>
	<view class="settings-container">
		<!-- 头像设置区域 -->
		<view class="settings-card">
			<view class="card-header">
				<text class="card-title">头像设置</text>
			</view>
			<view class="avatar-section">
				<view class="avatar-wrapper">
					<view class="avatar-circle">
						<img :src="Service.GetMateUrlByImg(userInfo.headImg)" alt="" style="width: 100%; height: 100%;" />
					</view>
					<view class="avatar-edit" @click="changeAvatar">
						<u-icon name="camera" size="20" color="#fff"></u-icon>
					</view>
				</view>
				<text class="avatar-tip">点击更换头像</text>
			</view>
		</view>

		<!-- 基本信息设置 -->
		<view class="settings-card">
			<view class="card-header">
				<text class="card-title">基本信息</text>
			</view>
			<view class="form-section">
				<view class="form-group">
					<text class="form-label">用户姓名</text>
					<view class="input-wrapper">
						<input
							class="form-input"
							v-model="userInfo.name"
							placeholder="请输入用户姓名"
							type="text"
							maxlength="20"
						/>
					</view>
				</view>

				<view class="form-divider"></view>

				<view class="form-group">
					<text class="form-label">手机号码</text>
					<view class="input-wrapper">
						<input
							class="form-input"
							v-model="userInfo.phone"
							placeholder="请输入手机号码"
							type="number"
							maxlength="11"
						/>
					</view>
				</view>
			</view>
		</view>

		<!-- 保存按钮 -->
		<view class="save-section">
			<button class="save-btn" @click="saveUserInfo">
				<text class="btn-text">保存修改</text>
			</button>
		</view>
	</view>
</template>

<script setup lang="ts">
	import {  onShow, onLoad } from "@dcloudio/uni-app"
	import { Service } from '@/Service/Service'
	import { ref } from 'vue'
	import { userService } from '@/Service/swimming/userService'
	
	const userInfo = ref<any>({
		headImg:'',
		name: '',
		phone: ''
	})

	onLoad(() => {
		loadUserInfo()
	})

	onShow(() => {

	})
	
	
	const loadUserInfo = () => {
		userService.GetUserInfo().then((content) => {
			if (content.code == 0) {
				userInfo.value=content.data.userInfo
			} else {
				Service.Msg(content.msg)
			}
		})
	}

	// 更换头像
	const changeAvatar = () => {
		uni.chooseImage({
			count: 1,
			sizeType: ['compressed'],
			sourceType: ['album', 'camera'],
			success: (res) => {
				let path = res.tempFiles[0].path
				Service.uploadH5(path, 'Avatar', data => {
					userInfo.value.headImg = data
				})
			}
		})
	}

// 保存用户信息
	const saveUserInfo = () => {
		if (!userInfo.value.name.trim()) {
			Service.Msg('请输入用户姓名')
			return
		}

		if (!userInfo.value.phone.trim()) {
			Service.Msg('请输入手机号码')
			return
		}

		// 简单的手机号验证
		const phoneReg = /^1[3-9]\d{9}$/
		if (!phoneReg.test(userInfo.value.phone)) {
			Service.Msg('请输入正确的手机号码')
			return
		}

		// 调用UpdateUserInfo接口
		userService.UpdateUserInfo(userInfo.value.name, userInfo.value.phone, userInfo.value.headImg).then((content) => {
			if (content.code == 0) {
				Service.Msg('保存成功', 'success')
				setTimeout(() => {
					Service.GoPageBack()
				}, 1000)
			} else {
				Service.Msg(content.msg)
			}
		})
	}
</script>

<style lang="scss" scoped>
	page {
		background-color: #f5f5f5;
	}

	.settings-container {
		padding: 20rpx;
		padding-bottom: 180rpx;
	}

	/* 页面标题区域 */
	.header-section {
		margin-bottom: 24rpx;

		.header-title {
			.title {
				font-size: 36rpx;
				font-weight: 700;
				color: #333;
				display: block;
				margin-bottom: 8rpx;
			}

			.subtitle {
				font-size: 24rpx;
				color: #999;
			}
		}
	}

	/* 设置卡片 */
	.settings-card {
		background-color: #fff;
		border-radius: 24rpx;
		padding: 0;
		margin-bottom: 20rpx;
		box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.06);
		overflow: hidden;

		.card-header {
			padding: 24rpx 24rpx 16rpx;
			border-bottom: 1rpx solid #f0f0f0;

			.card-title {
				font-size: 28rpx;
				font-weight: 600;
				color: #333;
			}
		}
	}

	/* 头像设置区域 */
	.avatar-section {
		padding: 40rpx 24rpx;
		display: flex;
		flex-direction: column;
		align-items: center;

		.avatar-wrapper {
			position: relative;
			margin-bottom: 20rpx;

			.avatar-circle {
				width: 160rpx;
				height: 160rpx;
				background: linear-gradient(135deg, #1890ff 0%, #36cfc9 50%, #096dd9 100%);
				border-radius: 50%;
				display: flex;
				align-items: center;
				justify-content: center;
				box-shadow: 0 8rpx 24rpx rgba(24, 144, 255, 0.3);
				overflow: hidden;
			}

			.avatar-edit {
				position: absolute;
				bottom: 0;
				right: 0;
				width: 56rpx;
				height: 56rpx;
				background: linear-gradient(135deg, #52c41a 0%, #389e0d 100%);
				border-radius: 50%;
				display: flex;
				align-items: center;
				justify-content: center;
				border: 4rpx solid #fff;
				box-shadow: 0 4rpx 12rpx rgba(82, 196, 26, 0.4);
				transition: all 0.3s ease;

				&:active {
					transform: scale(0.9);
				}
			}
		}

		.avatar-tip {
			font-size: 24rpx;
			color: #999;
		}
	}

	/* 表单区域 */
	.form-section {
		padding: 20rpx 24rpx;

		.form-group {
			padding: 20rpx 0;

			.form-label {
				display: block;
				font-size: 28rpx;
				font-weight: 600;
				color: #333;
				margin-bottom: 16rpx;
			}

			.input-wrapper {
				background-color: #f5f5f5;
				border-radius: 16rpx;
				padding: 0 24rpx;
				transition: all 0.2s ease;

				&:focus-within {
					background-color: #fff;
					box-shadow: 0 0 0 4rpx rgba(24, 144, 255, 0.15);
				}

				.form-input {
					width: 100%;
					height: 88rpx;
					font-size: 28rpx;
					color: #333;
				}
			}
		}

		.form-divider {
			height: 1rpx;
			background-color: #f0f0f0;
			margin: 0;
		}
	}

	/* 保存按钮区域 */
	.save-section {
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		background-color: #fff;
		padding: 20rpx 30rpx;
		padding-bottom: calc(20rpx + env(safe-area-inset-bottom));
		border-top: 1rpx solid #f0f0f0;
		box-shadow: 0 -4rpx 16rpx rgba(0, 0, 0, 0.08);
		z-index: 100;

		.save-btn {
			width: 100%;
			height: 96rpx;
			background: linear-gradient(135deg, #1890ff 0%, #096dd9 100%);
			border-radius: 20rpx;
			border: none;
			display: flex;
			align-items: center;
			justify-content: center;
			gap: 12rpx;
			box-shadow: 0 8rpx 24rpx rgba(24, 144, 255, 0.4);
			transition: all 0.3s ease;

			&:active {
				transform: scale(0.97);
				box-shadow: 0 4rpx 12rpx rgba(24, 144, 255, 0.3);
			}

			.btn-text {
				font-size: 32rpx;
				color: #fff;
				font-weight: 700;
			}
		}
	}
</style>
