<template>
	<view class="page">
		<view class="header">
			<text class="title">编辑个人信息</text>
			<text class="subtitle">完善资料便于我们为您提供更贴心的服务</text>
		</view>

		<view class="form-card">
			<view class="form-item">
				<text class="form-label">姓名</text>
				<input class="form-input" v-model="form.username" type="text" placeholder="请输入您的姓名"
					placeholder-class="form-placeholder" />
			</view>
			<view class="form-item">
				<text class="form-label">性别</text>
				<picker mode="selector" :range="genderOptions" range-key="label" :value="genderIndex"
					@change="handleGenderChange">
					<view class="form-picker">
						<text :class="form.sex ? '' : 'form-placeholder'">
							{{ genderText || '请选择性别' }}
						</text>
						<text class="picker-arrow">›</text>
					</view>
				</picker>
			</view>
			<view class="form-item">
				<text class="form-label">手机号</text>
				<input class="form-input" v-model="form.mobile" type="number" maxlength="11" placeholder="请输入常用手机号"
					placeholder-class="form-placeholder" />
			</view>
			<!-- <view class="form-item">
				<text class="form-label">邮箱</text>
				<input
					class="form-input"
					v-model="form.email"
					type="text"
					placeholder="请输入联系邮箱"
					placeholder-class="form-placeholder"
				/>
			</view> -->
			<!-- <view class="form-item">
				<text class="form-label">地址</text>
				<textarea
					class="form-textarea"
					v-model="form.address"
					placeholder="请输入联系地址"
					placeholder-class="form-placeholder"
					auto-height="true"
				/>
			</view> -->
		</view>

		<view class="tips-card">
			<text class="tips-title">信息使用说明</text>
			<text class="tips-text">
				姓名和联系方式仅用于订单通知及售后服务，我们会严格保护您的隐私信息。
			</text>
		</view>

		<view class="actions">
			<button class="btn cancel" @click="handleCancel">取消</button>
			<button class="btn submit" type="primary" @click="handleSubmit">保存信息</button>
		</view>
	</view>
</template>

<script>
import request from '@/api/request.js';
export default {
	data() {
		return {
			form: {
				username: '',
				sex: null,
				mobile: '',
				// email: '',
				// address: ''
			},
			genderOptions: [
				{ label: '男', value: 1 },
				{ label: '女', value: 2 }
			]
		};
	},
	computed: {
		genderIndex() {
			if (!this.form.sex) return 0;
			const option = this.genderOptions.find(item => item.value === this.form.sex);
			return option ? this.genderOptions.indexOf(option) : 0;
		},
		genderText() {
			if (!this.form.sex) return '';
			const option = this.genderOptions.find(item => item.value === this.form.sex);
			return option ? option.label : '';
		}
	},
	onLoad() {
		
		this.loadProfile();
	},
	methods: {
		loadProfile() {
			try {
				// const is_personal_information_complete = uni.getStorageSync('is_personal_information_complete');
				// if (is_personal_information_complete) {
					request('xcx/get_user_info', 'GET').then(res => {
						this.form = {
							...this.form,
							...res
						};
					});
				// }
			} catch (error) {
				console.warn('加载个人信息失败', error);
			}
		},
		handleGenderChange(e) {
			const index = e.detail.value;
			this.form.sex = this.genderOptions[index].value;
		},
		validatePhone(phone) {
			const phoneReg = /^(?:(?:\+|00)86)?1[3-9]\d{9}$/;
			return phoneReg.test(phone);
		},
		validateEmail(email) {
			const emailReg =
				/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@(([^<>()[\]\.,;:\s@"]+\.)+[^<>()[\]\.,;:\s@"]{2,})$/i;
			return emailReg.test(email);
		},
		handleCancel() {

			const is_personal_information_complete = uni.getStorageSync('is_personal_information_complete');
			if (is_personal_information_complete) {
				wx.navigateBack();
			} else {
				uni.setStorageSync('is_personal_information_complete', true);
				wx.navigateTo({
					url: `/pages/my/editInfo/index`,
				});
			}
		},
		handleSubmit() {
			if (!this.form.username.trim()) {
				return this.showToast('请填写姓名');
			}
			if (!this.form.sex || (this.form.sex !== 1 && this.form.sex !== 2)) {
				return this.showToast('请选择性别');
			}
			if (!this.form.mobile.trim() || !this.validatePhone(this.form.mobile.trim())) {
				return this.showToast('请填写有效的手机号');
			}
			// if (this.form.email.trim() && !this.validateEmail(this.form.email.trim())) {
			// 	return this.showToast('请填写有效的邮箱');
			// }
			// if (!this.form.address.trim()) {
			// 	return this.showToast('请填写联系地址');
			// }

			try {
				request('xcx/set_user_info', 'POST', this.form).then(res => {
					this.showToast('保存成功', 'success');
					wx.navigateBack();
					// const is_personal_information_complete = uni.getStorageSync('is_personal_information_complete');
					// if (is_personal_information_complete) {
					// 	wx.navigateBack();
					// } else {
					// 	uni.setStorageSync('is_personal_information_complete', true);
					// 	wx.navigateTo({
					// 		url: `/pages/my/editInfo/index`,
					// 	});
					// }
				});

			} catch (error) {
				this.showToast('信息保存失败，请稍后重试');
				console.error('保存个人信息失败', error);
			}
		},
		showToast(title, icon = 'none') {
			uni.showToast({
				title,
				icon,
				duration: 2000
			});
		}
	}
};
</script>

<style scoped>
.page {
	background: #f7f8fa;
	padding: 40rpx 32rpx 80rpx;
	display: flex;
	flex-direction: column;
	gap: 32rpx;
}

.header {
	display: flex;
	flex-direction: column;
	gap: 12rpx;
}

.title {
	font-size: 36rpx;
	color: #1a1a1a;
	font-weight: 600;
}

.subtitle {
	font-size: 26rpx;
	color: #8c8c8c;
	line-height: 1.6;
}

.form-card {
	background: #ffffff;
	border-radius: 24rpx;
	padding: 32rpx;
	display: flex;
	flex-direction: column;
	gap: 32rpx;
	box-shadow: 0 12rpx 30rpx rgba(16, 30, 54, 0.06);
}

.form-item {
	display: flex;
	flex-direction: column;
	gap: 20rpx;
}

.form-label {
	font-size: 28rpx;
	color: #1a1a1a;
	font-weight: 500;
}

.form-input {
	height: 80rpx;
	line-height: 80rpx;
}

.form-input,
.form-textarea {
	width: 100%;
	background: #f5f7fb;
	border-radius: 16rpx;
	padding: 0 28rpx;
	font-size: 28rpx;
	color: #1a1a1a;
	box-sizing: border-box;
}

.form-picker {
	width: 100%;
	height: 80rpx;
	background: #f5f7fb;
	border-radius: 16rpx;
	padding: 24rpx 28rpx;
	font-size: 28rpx;
	color: #1a1a1a;
	box-sizing: border-box;
	display: flex;
	align-items: center;
	justify-content: space-between;
}

.picker-arrow {
	font-size: 32rpx;
	color: #b0b3ba;
}

.form-textarea {
	min-height: 160rpx;
	line-height: 1.6;
}

.form-placeholder {
	color: #b0b3ba;
	font-size: 28rpx;
}

.tips-card {
	background: #fff8f2;
	border-radius: 24rpx;
	padding: 28rpx 32rpx;
	display: flex;
	flex-direction: column;
	gap: 12rpx;
}

.tips-title {
	font-size: 28rpx;
	color: #ff7a00;
	font-weight: 600;
}

.tips-text {
	font-size: 26rpx;
	color: #a66b3a;
	line-height: 1.6;
}

.actions {
	display: flex;
	gap: 24rpx;
	padding: 0 4rpx;
}

.btn {
	flex: 1;
	/* height: 92rpx; */
	border-radius: 46rpx;
	font-size: 30rpx;
	font-weight: 500;
	display: flex;
	align-items: center;
	justify-content: center;
}

.cancel {
	color: #1a1a1a;
	background: #e9ecf1;
}

.submit {
	color: #ffffff;
	background: linear-gradient(135deg, #8b40ff 0%, #ff3c8d 100%);
	border: none;
}
</style>
