<template>
	<view class="login-page">
		<view class="content">
			<view class="header">
				<view class="main-title">登录</view>
				<view class="sub-title">请输入标识进行登录</view>
			</view>

			<view class="form-section">
				<view class="input-wrap" :class="{ focus: inputFocus }">
					<input
						v-model="identifier"
						class="input"
						placeholder="请输入标识"
						placeholder-class="placeholder"
						@focus="inputFocus = true"
						@blur="inputFocus = false"
						@input="showError = false"
					/>
				</view>
				<text v-if="showError" class="error">请输入标识</text>
			</view>

			<view class="btn-section">
				<button
					class="btn-login"
					:class="{ disabled: !identifier || loading || !agreed }"
					:disabled="!identifier || loading || !agreed"
					@tap="handleLogin"
				>
					<view v-if="loading" class="loading">
						<view class="dot"/>
						<view class="dot"/>
						<view class="dot"/>
					</view>
					<text v-else>登录</text>
				</button>
			</view>

			<view class="agreement">
				<view class="agreement-text">
					<text class="text">登录即表示同意</text>
					<text class="link">《用户协议》</text>
					<text class="text">和</text>
					<text class="link">《隐私政策》</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script setup>
	import { ref, onMounted } from 'vue';
	import { authApi } from '@/api/index.js';
	import { useUserStore } from '@/store/index.js';
	import { APP_ID, USE_WECHAT_AUTH } from '@/utils/env.js';

	const userStore = useUserStore();

	const identifier = ref('TEST5G');
	const loading = ref(false);
	const agreed = ref(true);
	const inputFocus = ref(false);
	const showError = ref(false);

	const getCode = () => {
		const params = new URLSearchParams(window.location.search);
		return params.get('code');
	};

	const clearCode = () => {
		window.history.replaceState({}, '', window.location.pathname);
	};

	const redirectToWxAuth = (assetToken) => {
		sessionStorage.setItem('assetToken', assetToken);
		const redirectUri = encodeURIComponent('https://cmp-c.boss160.cn/');
		const url = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${APP_ID}&redirect_uri=${redirectUri}&response_type=code&scope=snsapi_userinfo&state=wechat_auth#wechat_redirect`;
		window.location.href = url;
	};

	const handleLogin = () => {
		if (!identifier.value) {
			showError.value = true;
			return;
		}
		doLogin();
	};

	const doLogin = async () => {
		loading.value = true;
		try {
			const verifyData = await authApi.verifyAsset(identifier.value);
			userStore.setAssetToken(verifyData.asset_token);
			userStore.setIdentifier(identifier.value);

			if (!USE_WECHAT_AUTH) {
				const loginData = await authApi.wechatLogin(verifyData.asset_token, 'mock_code');
				userStore.setToken(loginData.token);
				uni.setStorageSync('identifier', userStore.state.identifier);
				uni.redirectTo({ url: '/pages/index/index' });
				return;
			}

			redirectToWxAuth(verifyData.asset_token);
		} catch (e) {
			console.error('登录失败', e);
			loading.value = false;
		}
	};

	const handleWechatCallback = async () => {
		if (!USE_WECHAT_AUTH) return;

		const code = getCode();
		const assetToken = sessionStorage.getItem('assetToken');

		if (!code || !assetToken) return;

		loading.value = true;
		clearCode();

		try {
			const loginData = await authApi.wechatLogin(assetToken, code);
			userStore.setToken(loginData.token);

			uni.setStorageSync('identifier', userStore.state.identifier);
			uni.redirectTo({ url: '/pages/index/index' });
		} catch (e) {
			console.error('微信登录失败', e);
			loading.value = false;
		}
	};

	onMounted(() => {
		handleWechatCallback();
	});
</script>

<style lang="scss" scoped>
	.login-page {
		display: flex;
		flex-direction: column;
		background: #FFFFFF;
		min-height: 100vh;
		padding: 60rpx 40rpx;
	}

	.content {
		width: 100%;
	}

	.header {
		margin-bottom: 80rpx;
	}

	.main-title {
		font-size: 56rpx;
		font-weight: 700;
		color: #1A1A1A;
		line-height: 1.3;
		margin-bottom: 24rpx;
		letter-spacing: 0.5rpx;
	}

	.sub-title {
		font-size: 28rpx;
		color: #9CA3AF;
		line-height: 1.5;
		letter-spacing: 0.5rpx;
	}

	.form-section {
		margin-bottom: 60rpx;
	}

	.input-wrap {
		display: flex;
		align-items: center;
		background: #f7f7f7;
		border-radius: 20rpx;
		padding: 32rpx 40rpx;
		transition: all 0.3s;
	}

	.input-wrap.focus {
		background: #f7f7f7;
	}

	.input {
		flex: 1;
		font-size: 32rpx;
		color: #1A1A1A;
		background: transparent;
	}

	.placeholder {
		color: #C4C4C4;
	}

	.error {
		font-size: 24rpx;
		color: #EF4444;
		margin-top: 16rpx;
		padding-left: 8rpx;
	}

	.btn-section {
		margin-bottom: 60rpx;
	}

	.btn-login {
		width: 100%;
		height: 108rpx;
		background: $uni-color-primary;
		border: none;
		border-radius: 20rpx;
		font-size: 36rpx;
		font-weight: 600;
		color: #FFFFFF;
		display: flex;
		align-items: center;
		justify-content: center;
		transition: all 0.3s;
	}

	.btn-login.disabled {
		background: #E5E7EB;
		color: #9CA3AF;
	}

	.btn-login:active:not(.disabled) {
		opacity: 0.8;
	}

	.loading {
		display: flex;
		align-items: center;
		gap: 8rpx;
	}

	.dot {
		width: 10rpx;
		height: 10rpx;
		background: #FFFFFF;
		border-radius: 50%;
		animation: load 1.4s infinite ease-in-out both;
	}

	.dot:nth-child(1) { animation-delay: -0.32s; }
	.dot:nth-child(2) { animation-delay: -0.16s; }

	@keyframes load {
		0%, 80%, 100% {
			transform: scale(0.5);
			opacity: 0.3;
		}
		40% {
			transform: scale(1);
			opacity: 1;
		}
	}

	.agreement {
		display: flex;
		align-items: center;
		justify-content: center;
		padding: 0 8rpx;
	}

	.agreement-text {
		font-size: 24rpx;
		line-height: 1.6;
		display: flex;
		flex-wrap: wrap;
		align-items: center;
		justify-content: center;
		gap: 4rpx;
	}

	.text {
		color: #9CA3AF;
		white-space: nowrap;
	}

	.link {
		color: #2563EB;
		white-space: nowrap;
	}
</style>
