<template>
	<view class="page">
		<!-- 自定义导航栏 -->
		<view v-if="!fromLogin" class="custom-navbar">
			<view class="navbar-back" @tap="goBack">
				<text class="back-text">🔙</text>
			</view>
			<view class="navbar-title">绑定手机号</view>
			<view class="navbar-placeholder"></view>
		</view>

		<view class="container" :style="{ paddingTop: fromLogin ? '0' : '0' }">
			<view class="card flex-col-g20">
				<view class="flex-row-g20">
					<label for="">手机号:</label>
					<up-input placeholder="请输入绑定的手机号" border="surround" v-model="bind.phone" />
				</view>

				<view class="flex-row-g20">
					<label for="">验证码:</label>
					<up-input placeholder="验证码" v-model="bind.code">
						<template #suffix>
							<up-button @tap="getCode" :text="codeText" type="success" :disabled="cooldown > 0"></up-button>
						</template>
					</up-input>
				</view>

				<view class="btn">
					<up-button type="primary" @tap="bindPhone">绑定手机号</up-button>
				</view>

				<!-- 如果从登录页面跳转过来，显示退出按钮 -->
				<view class="btn" v-if="fromLogin">
					<up-button type="error" @tap="logout">退出</up-button>
				</view>
			</view>
		</view>
	</view>
</template>

<script setup>
	import { ref, reactive, onMounted } from 'vue';
	import { authApi } from '@/api/index.js';

	let bind = reactive({
		phone: '',
		code: ''
	});

	let cooldown = ref(0);
	let codeText = ref('获取验证码');
	let timer = null;
	let fromLogin = ref(false);

	onMounted(() => {
		// 获取页面参数，判断是否从登录页面跳转过来
		const pages = getCurrentPages();
		const currentPage = pages[pages.length - 1];
		const options = currentPage.options;
		fromLogin.value = options.fromLogin === 'true';
	});

	const getCode = async () => {
		if (!bind.phone) {
			uni.showToast({ title: '请输入手机号', icon: 'none' });
			return;
		}

		if (!/^1[3-9]\d{9}$/.test(bind.phone)) {
			uni.showToast({ title: '请输入正确的手机号', icon: 'none' });
			return;
		}

		try {
			const data = await authApi.sendCode(bind.phone, 'bind_phone');
			cooldown.value = data.cooldown_seconds || 60;
			codeText.value = `${cooldown.value}s`;

			timer = setInterval(() => {
				cooldown.value--;
				if (cooldown.value <= 0) {
					clearInterval(timer);
					codeText.value = '获取验证码';
				} else {
					codeText.value = `${cooldown.value}s`;
				}
			}, 1000);

			uni.showToast({ title: '验证码已发送', icon: 'success' });
		} catch (e) {
			console.error('发送验证码失败', e);
		}
	};

	const bindPhone = async () => {
		if (!bind.phone || !bind.code) {
			uni.showToast({ title: '手机号和验证码都不能为空', icon: 'none' });
			return;
		}

		try {
			await authApi.bindPhone(bind.phone, bind.code);
			uni.showToast({ title: '绑定成功', icon: 'success' });
			setTimeout(() => {
				if (fromLogin.value) {
					// 从登录页面过来的，绑定成功后跳转到首页
					uni.redirectTo({ url: '/pages/index/index' });
				} else {
					// 从其他页面过来的，返回上一页
					uni.navigateBack();
				}
			}, 1500);
		} catch (e) {
			console.error('绑定手机号失败', e);
		}
	};

	const logout = () => {
		uni.showModal({
			title: '提示',
			content: '确定要退出登录吗？',
			success: (res) => {
				if (res.confirm) {
					uni.clearStorageSync();
					uni.reLaunch({ url: '/pages/login/login' });
				}
			}
		});
	};

	const goBack = () => {
		uni.navigateBack();
	};
</script>

<style lang="scss" scoped>
	.page {
		min-height: 100vh;
		background: var(--bg-secondary);
	}

	.custom-navbar {
		display: flex;
		align-items: center;
		justify-content: space-between;
		height: 88rpx;
		margin-bottom: 24rpx;
		background: var(--bg-primary);
		border-bottom: 1rpx solid var(--border-light);
		padding: 0 24rpx;
		position: sticky;
		top: 0;
		z-index: 100;

		.navbar-back {
			display: flex;
			align-items: center;
			gap: 8rpx;
			padding: 8rpx;
			cursor: pointer;

			.back-icon {
				font-size: 36rpx;
				color: var(--text-primary);
			}

			.back-text {
				font-size: 28rpx;
				color: var(--text-primary);
			}
		}

		.navbar-title {
			font-size: 32rpx;
			font-weight: 600;
			color: var(--text-primary);
			position: absolute;
			left: 50%;
			transform: translateX(-50%);
		}

		.navbar-placeholder {
			width: 100rpx;
		}
	}

	.container {
		padding: var(--space-md);
	}
</style>