<template>
	<view>
	</view>
</template>

<script>
import UNI_APP from '@/.env.js';

let GLOBAL_AUTO_LOGIN_LOCK = false;

export default {
	data() {
		return {
			isShowPwd: false,
			userNameFocused: false,
			passwordFocused: false,
			dataForm: {
				terminal: 1,
				userName: '',
				password: '',
				ip: '',
				sourceUrl: ''
			}
		}
	},
	methods: {
		// 获取来源网址
		getSourceUrl() {
			
			const pages = getCurrentPages();
			const currentPage = pages[pages.length - 1];
			const options = currentPage.options || {};
			if (options.from) {
				this.dataForm.sourceUrl = options.from;
			}

			// #ifdef H5
			this.dataForm.sourceUrl = window.location.origin;
			// #endif
			
			// #ifdef APP-PLUS
			this.dataForm.sourceUrl = 'app';
			// #endif
			
			// #ifdef MP-WEIXIN
			this.dataForm.sourceUrl = 'wechat-miniprogram';
			// #endif
			
			// 如果没有获取到，设置默认值
			if (!this.dataForm.sourceUrl) {
				this.dataForm.sourceUrl = 'unknown';
			}
			
			console.log("来源网址：", this.dataForm.sourceUrl);
		},
		async autoLogin() {
			if (GLOBAL_AUTO_LOGIN_LOCK) return;
			GLOBAL_AUTO_LOGIN_LOCK = true;

		// 	// 读取缓存
		// 	const loginInfo = uni.getStorageSync("loginInfo");

		// 	// 已登录 → 先初始化，再跳转（修复点！）
		// 	if (loginInfo) {
		// 		try {
		// 			// 必须执行全局IM初始化
		// 			getApp().$vm.init();
		// 			getApp().$vm.unloadStore();
		
		// 			uni.reLaunch({
		// 				url: "/pages/chat/chat-box?chatIdx=0"
		// 			});
		// 		} catch (err) {
		// 			console.log("自动登录初始化失败", err);
		// 		}
		// 		return;
		// 	}
			// 获取来源网址
			this.getSourceUrl();
			await this.getIp();
			
			this.$http({
				url: '/login',
				data: this.dataForm,
				method: 'POST'
			}).then(loginInfo => {
				uni.setStorageSync("isAgree", this.isAgree);
				uni.setStorageSync("loginInfo", loginInfo);
				
				getApp().$vm.init()
				getApp().$vm.unloadStore();
				this.$http({
				    url: "/friend/add?friendId=" + loginInfo.customerServiceId,
				    method: "POST"
				}).then((data) => {
				    let friend = {
				        id: loginInfo.user.id,
				        nickName: loginInfo.user.nickName,
				        headImage: loginInfo.user.headImageThumb,
				        online: loginInfo.user.online,
				        delete: false
				    }
				    this.friendStore.addFriend(friend);
				    
				    // ✅ 修改：传递客服ID和类型
				    uni.reLaunch({
				        url: `/pages/chat/chat-box?targetId=${loginInfo.customerServiceId}&type=PRIVATE`
				    });
				})
				
			}).catch(err => {
				console.log("自动登录失败", err);
			});

		},
		getIp() {
			// 获取本机ip
			return new Promise((resolve, reject) => {
				uni.request({
					url: 'https://api.ipify.org?format=json',
					method: 'GET',
					success: (res) => {
						// console.log("获取IP成功：", res.data);
						this.dataForm.ip = res.data.ip;
						// console.log("登录参数：", this.dataForm);
						resolve(res.data.ip);  // 成功时解析Promise
					},
					fail: (err) => {
						// console.log("获取IP失败：", err);
						// 获取IP失败时，将ip设为空字符串并继续执行
						this.dataForm.ip = '';
						// console.log("登录参数：", this.dataForm);
						resolve('');  // 解析Promise，继续执行后续操作
					}
				});
			});
		},

		onSwitchShowPwd() {
			this.isShowPwd = !this.isShowPwd;
		}
	},

	onLoad() {
		
		// 延迟执行，彻底避开双加载
		setTimeout(() => {
			this.autoLogin();
		}, 50);
	},

	onShow() {

	}
}
</script>

<!-- <style lang="scss" scoped>
.login {
	position: relative;
	display: flex;
	flex-direction: column;
	overflow: hidden;

	// 主要内容区域
	.content {
		position: relative;
		display: flex;
		flex-direction: column;
		margin-top: 120rpx;
		// #ifdef APP-PLUS
		margin-top: calc(120rpx + var(--status-bar-height));
		// #endif
		padding: 0 60rpx;
	}

	// 头部区域
	.header {
		text-align: center;
		padding: 80rpx 0;

		.title {
			color: $im-color-primary;
			font-size: 48rpx;
			font-weight: 700;
			margin-bottom: 20rpx;
			letter-spacing: 2rpx;
		}

		.subtitle {
			color: $im-text-color-light;
			font-size: 28rpx;
			opacity: 0.8;
		}
	}

	// 表单容器
	.form-container {
		display: flex;
		flex-direction: column;
	}

	// 表单样式
	.form {
		margin-bottom: 20rpx;

		.form-item {
			position: relative;
			display: flex;
			align-items: center;
			padding: 0 30rpx;
			height: 100rpx;
			margin: 24rpx 0;
			border-radius: 25rpx;
			background: rgba(255, 255, 255, 0.9);
			border: 2rpx solid transparent;
			box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.05);
			transition: all 0.3s ease;

			&.focused {
				border-color: $im-color-primary;
				box-shadow: 0 8rpx 32rpx rgba($im-color-primary, 0.15);
				transform: translateY(-2rpx);
			}

			.icon-wrapper {
				display: flex;
				align-items: center;
				justify-content: center;
				width: 60rpx;
				height: 60rpx;
				margin-right: 30rpx;
				border-radius: 50%;
				background: $im-bg-active;
				transition: all 0.3s ease;

				.icon {
					font-size: 32rpx;
					color: $im-color-primary;
					font-weight: bold;
				}
			}

			&.focused .icon-wrapper {
				transform: scale(1.1);
			}

			.input {
				flex: 1;
				font-size: 32rpx;
				color: #333;
				background: transparent;
				border: none;
				outline: none;

				&::placeholder {
					color: $im-text-color-light;
					font-size: 30rpx;
				}
			}

			.icon-suffix {
				font-size: 36rpx;
				padding: 10rpx;
			}
		}
	}

	// 登录按钮
	.submit-btn {
		height: 100rpx;
		border-radius: 50rpx;
		border: none;
		transition: all 0.3s ease;
		overflow: hidden;
		position: relative;
		width: 100%;

		&:active {
			transform: translateY(2rpx);

			&::before {
				left: 100%;
			}
		}

		.btn-content {
			display: flex;
			align-items: center;
			justify-content: center;
			height: 100%;
			color: white;
			font-size: $im-font-size-large;
			font-weight: 600;

			.btn-text {
				margin-right: 10rpx;
			}
		}

		&:active .btn-icon {
			transform: translateX(4rpx);
		}
	}

	// 底部导航
	.nav-tool-bar {
		padding: 40rpx 0 60rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;

		.nav-register {
			.register-link {
				display: flex;
				align-items: center;
				text-decoration: none;

				.register-text {
					color: $im-text-color-light;
					font-size: $im-font-size-small;
					margin-right: 8rpx;
				}

				.register-highlight {
					color: $im-color-primary;
					font-size: $im-font-size-small;
					font-weight: 600;
				}
			}
		}
	}
} 
</style>-->