<template>
	<view class="index">
		<!-- 顶部标题栏 -->
		<view class="header">
			<text class="title">计分器</text>
			<view class="header-icon">
				<!-- 这里可以放设置或其他功能图标 -->
			</view>
		</view>

		<!-- 未结束对局提示 -->
		<view class="notice">
			<view class="notice-icon">
				<uni-icons type="sound" size="16" color="#fa5d5d"></uni-icons>
			</view>
			<view class="notice-text">
				您还有未结束的对局，
				<text class="notice-link">点击前往</text>
			</view>
		</view>

		<!-- 功能列表 -->
		<view class="function-list">
			<!-- 多人模式 -->
			<view class="function-item" @click="openRoomPopup">
				<view class="function-icon">
					<image src="https://wx2.sinaimg.cn/mw690/6baef0d8gy1i1crsn044pj23b04eou12.jpg" mode="aspectFit">
					</image>
				</view>
				<view class="function-info">
					<view class="function-name">多人模式</view>
					<view class="function-desc">所有玩家自己计分</view>
				</view>
			</view>
			<!-- 单人模式 -->
			<view class="function-item" @click="navToSingleMode">
				<view class="function-icon">
					<image src="https://wx2.sinaimg.cn/mw690/6baef0d8gy1i1crsn044pj23b04eou12.jpg" mode="aspectFit">
					</image>
				</view>
				<view class="function-info">
					<view class="function-name">单人模式</view>
					<view class="function-desc">房主给所有玩家计分</view>
				</view>
			</view>

			<!-- 比赛计分器 -->
			<view class="function-item">
				<view class="function-icon">
					<image src="https://wx2.sinaimg.cn/mw690/6baef0d8gy1i1crsn044pj23b04eou12.jpg" mode="aspectFit">
					</image>
				</view>
				<view class="function-info">
					<view class="function-name">比赛计分器</view>
					<view class="function-desc">适合篮球、排球等运动类比赛计分工具</view>
				</view>
			</view>

			<!-- 多人记账 -->
			<view class="function-item">
				<view class="function-icon">
					<image src="https://wx2.sinaimg.cn/mw690/6baef0d8gy1i1crsn044pj23b04eou12.jpg" mode="aspectFit">
					</image>
				</view>
				<view class="function-info">
					<view class="function-name">多人记账</view>
					<view class="function-desc">免费的日常生活多人记账工具</view>
				</view>
			</view>


		</view>

		<!-- 遮罩层 -->
		<view v-if="showPopup" class="popup-mask" @click="closePopup"></view>

		<!-- 房间管理弹窗 - 居中显示 -->
		<view v-if="showPopup" class="popup-wrapper">
			<view class="popup-content">
				<!-- 标题和关闭按钮 -->
				<view class="popup-header">
					<text class="popup-title">房间管理</text>
					<text class="close-btn" @click="closePopup">×</text>
				</view>

				<!-- 模式选择标签 -->
				<view class="mode-tabs">
					<view class="tab-item" :class="{ active: currentMode === 'join' }" @click="switchMode('join')">
						加入房间
					</view>
					<view class="tab-item" :class="{ active: currentMode === 'create' }" @click="switchMode('create')">
						创建房间
					</view>
				</view>

				<!-- 加入房间表单 -->
				<view v-if="currentMode === 'join'" class="form-container">
					<view class="form-item">
						<text class="label">房间号</text>
						<input v-model="joinForm.roomId" class="input" placeholder="请输入6位房间号" maxlength="6"
							type="number" />
					</view>
					<button class="submit-btn join-btn" @click="handleJoinRoom">加入房间</button>
				</view>

				<!-- 创建房间表单 -->
				<view v-if="currentMode === 'create'" class="form-container">


					<view class="form-item">
						<text class="label">房间名称</text>
						<input v-model="createForm.roomName" class="input" placeholder="请输入房间名称（可选）" maxlength="20" />
					</view>

					<button class="submit-btn create-btn" @click="handleCreateRoom">创建房间</button>
				</view>
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		ref,
		onMounted,
		getCurrentInstance
	} from 'vue'
	import {
		GET,
		POST
	} from '../../utils/request';

	const proxy = getCurrentInstance().proxy;
	const userInfo = ref({})
	const getUserInfo = async () => {
		userInfo.value = proxy.$StaticValue.getUserInfo();
	}
	const showPopup = ref(false)

	// 修改打开弹窗方法
	const openRoomPopup = () => {
		console.log("打开弹窗")
		showPopup.value = true
		// 重置表单
		joinForm.value.roomId = ''

	}

	// 修改关闭弹窗方法
	const closePopup = () => {
		showPopup.value = false
	}

	// 当前模式：join-加入房间，create-创建房间
	const currentMode = ref('join')

	// 表单数据
	const joinForm = ref({
		roomId: ''
	})

	const createForm = ref({
		roomName: ''
	})




	// 切换模式
	const switchMode = (mode) => {
		currentMode.value = mode

	}

	// 加入房间
	const handleJoinRoom = () => {
		if (!joinForm.value.roomId) {
			uni.showToast({
				title: '请输入房间号',
				icon: 'none'
			})
			return
		}

		uni.showLoading({
			title: '加入中...'
		})

		var RoomUserInfo = {

			userId: userInfo.value.userId,
			roomId: joinForm.value.roomId,
			score: 0,
			createUser: userInfo.value.userId,

			playerType: 'user',
			nickName: userInfo.value.nickName,
			avatars: userInfo.value.avatars
		}

		POST('/system/score/room/user', RoomUserInfo).then((res) => {
			console.log("561449", res)
			console.log("15648", res.msg)

			if (res.msg == '0') {
				uni.hideLoading();
				uni.showToast({
					title: '加入房间失败，请重试',
					icon: 'error' // 或 'none'，根据需求调整
				});
				return;
			}

			setTimeout(() => {
				uni.hideLoading()
				uni.showToast({
					title: '加入房间成功',
					icon: 'success'
				})
				closePopup()

				// 跳转到房间页面
				uni.navigateTo({
					url: `/pages/more/more?roomUserInfo=${encodeURIComponent(JSON.stringify(RoomUserInfo))}`
				})
			}, 1000)

		}).catch((err) => {

			console.error('加入房间失败:', err); // 打印错误信息，便于调试
		});


	}

	// 创建房间
	const handleCreateRoom = () => {

		uni.showLoading({
			title: '创建中...'
		})

		var RoomUserInfo = {
			roomId: 0,
			roomName: createForm.value.roomName,
			createUser: userInfo.value.userId,
			userId: userInfo.value.userId,
			odds: 1,
			roomStatus: 0,
			bossId: userInfo.value.userId,
			playerType: 'business',
			nickName: userInfo.value.nickName,
			avatars: userInfo.value.avatars
		}

		POST('/system/room', RoomUserInfo).then((res) => {
			console.log("999999999999999999999", res.msg)

			RoomUserInfo.roomId = +res.msg;
			console.log("5484454545555", RoomUserInfo)
			setTimeout(() => {
				uni.hideLoading()
				uni.showToast({
					title: '房间创建成功',
					icon: 'success'
				})
				closePopup()

				// 跳转到房间页面
				uni.navigateTo({
					url: `/pages/more/more?roomUserInfo=${encodeURIComponent(JSON.stringify(RoomUserInfo))}`
				})
			}, 1000)

		})



	}

	// 导航函数
	const navToSingleMode = () => {
		uni.navigateTo({
			url: '/pages/single/single'
		})
	}


	const goToUnfinishedGame = () => {
		uni.navigateTo({
			url: '/pages/unfinished/unfinished'
		})
	}

	// 初始化
	onMounted(() => {
		getUserInfo()

	})
</script>

<style scoped lang="less">
	.index {
		min-height: 100vh;
		background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
		padding: 20rpx;
		position: relative;
	}

	/* 遮罩层 */
	.popup-mask {
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		background: rgba(0, 0, 0, 0.5);
		z-index: 999;
	}

	/* 弹窗包装器 - 居中定位 */
	.popup-wrapper {
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		display: flex;
		align-items: center;
		justify-content: center;
		z-index: 1000;
		padding: 40rpx;
		box-sizing: border-box;
	}

	/* 弹窗内容 */
	.popup-content {
		background: #fff;
		border-radius: 20rpx;
		padding: 40rpx 30rpx;
		width: 100%;
		max-width: 600rpx;
		max-height: 80vh;
		overflow-y: auto;
		box-shadow: 0 10rpx 30rpx rgba(0, 0, 0, 0.3);
		animation: popup-show 0.3s ease-out;
	}

	/* 弹窗动画 */
	@keyframes popup-show {
		from {
			opacity: 0;
			transform: scale(0.8) translateY(-50rpx);
		}

		to {
			opacity: 1;
			transform: scale(1) translateY(0);
		}
	}

	/* 顶部标题栏 */
	.header {
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 30rpx 20rpx;

		.title {
			font-size: 36rpx;
			font-weight: bold;
			color: #fff;
		}

		.header-icon {
			width: 40rpx;
			height: 40rpx;
		}
	}

	/* 未结束对局提示 */
	.notice {
		display: flex;
		align-items: center;
		background: rgba(255, 255, 255, 0.9);
		padding: 20rpx 25rpx;
		border-radius: 15rpx;
		margin: 20rpx 0;
		backdrop-filter: blur(10px);

		.notice-icon {
			margin-right: 15rpx;
		}

		.notice-text {
			font-size: 28rpx;
			color: #666;
		}

		.notice-link {
			color: #fa5d5d;
			text-decoration: underline;
			margin-left: 5rpx;
		}
	}

	/* 功能列表 */
	.function-list {
		margin-top: 40rpx;
	}

	.function-item {
		display: flex;
		align-items: center;
		background: #fff;
		padding: 30rpx 25rpx;
		border-radius: 20rpx;
		margin-bottom: 25rpx;
		box-shadow: 0 4rpx 15rpx rgba(0, 0, 0, 0.1);
		transition: all 0.3s ease;
		cursor: pointer;

		&:active {
			transform: scale(0.98);
			box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.15);
		}

		.function-icon {
			width: 80rpx;
			height: 80rpx;
			margin-right: 25rpx;

			image {
				width: 100%;
				height: 100%;
				border-radius: 15rpx;
			}
		}

		.function-info {
			flex: 1;

			.function-name {
				font-size: 32rpx;
				font-weight: 600;
				color: #333;
				margin-bottom: 8rpx;
			}

			.function-desc {
				font-size: 26rpx;
				color: #999;
				line-height: 1.4;
			}
		}
	}

	.popup-header {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 30rpx;

		.popup-title {
			font-size: 32rpx;
			font-weight: bold;
			color: #333;
		}

		.close-btn {
			font-size: 40rpx;
			color: #999;
			padding: 10rpx 20rpx;
			cursor: pointer;
		}
	}

	.mode-tabs {
		display: flex;
		background: #f5f5f5;
		border-radius: 10rpx;
		padding: 4rpx;
		margin-bottom: 30rpx;

		.tab-item {
			flex: 1;
			text-align: center;
			padding: 20rpx;
			border-radius: 8rpx;
			font-size: 28rpx;
			color: #666;
			transition: all 0.3s;
			cursor: pointer;

			&.active {
				background: #fff;
				color: #007AFF;
				font-weight: bold;
				box-shadow: 0 2rpx 6rpx rgba(0, 122, 255, 0.2);
			}
		}
	}

	.form-container {
		.form-item {
			margin-bottom: 30rpx;

			.label {
				display: block;
				font-size: 28rpx;
				color: #333;
				margin-bottom: 15rpx;
				font-weight: 500;
			}

			.input {
				width: 90%;
				padding: 20rpx;
				border: 2rpx solid #e0e0e0;
				border-radius: 10rpx;
				font-size: 28rpx;
				background: #fafafa;

				&:focus {
					border-color: #007AFF;
					background: #fff;
				}
			}

			.room-id-container {
				display: flex;
				gap: 20rpx;
				align-items: center;

				.input {
					flex: 1;
				}

				.generate-btn {
					background: #f0f0f0;
					color: #666;
					padding: 20rpx 30rpx;
					border-radius: 8rpx;
					font-size: 24rpx;
					white-space: nowrap;
					border: none;
					cursor: pointer;
				}
			}
		}

		.submit-btn {
			width: 100%;
			padding: 25rpx;
			border-radius: 10rpx;
			font-size: 32rpx;
			font-weight: bold;
			border: none;
			margin-top: 10rpx;
			cursor: pointer;

			&.join-btn {
				background: #007AFF;
				color: #fff;
			}

			&.create-btn {
				background: #34C759;
				color: #fff;
			}

			&:active {
				opacity: 0.8;
			}
		}
	}
</style>