<template>
	<view class="main">
		<view :style="{height:iSMT+'px'}"></view>
		<view class="top">
			<view class="top-list">
				<text>自动选择</text>
				<radio value="0" class="radio-btn"  activeBackgroundColor="red"
				 :checked="selectedIndex === 0"  @click="selectFont('auto')" />
			</view>
			<view class="top-list">
				<text>新加坡服务器</text>
				<radio value="1" class="radio-btn"  activeBackgroundColor="red"
				 :checked="selectedIndex === 1"  @click="selectFont('singapore')" />
			</view>
			<view class="top-list">
				<text>香港服务器</text>
				<radio value="2" class="radio-btn"  activeBackgroundColor="red"
				 :checked="selectedIndex === 2"  @click="selectFont('hongkong')" />
			</view>
		</view>
		</view>
</template>

<script setup>
	import {
		ref,
		onMounted
	} from 'vue'
	import {
		getSetting,
		updateSetting
	} from "@/api/setting/general"
	
	const iSMT = ref(0)
	const selectedIndex = ref(0)
	const servertypeMap = {
		'auto': 0,
		'singapore': 1,
		'hongkong': 2
	}
	
	const getServer = async () => {
		try {
			const res = await getSetting()
			if (res.code === 200) {
				const serverSelection = res.data.serverSelection
				selectedIndex.value = servertypeMap[serverSelection] ?? 0;
			}
		} catch (err) {
			console.error("获取服务器设置失败：", err);
		}
	}
	
	const selectFont = async (servertype) => {
		try {
			selectedIndex.value = servertypeMap[servertype]
			console.log('服务器类型：', servertype, '，looklook索引：', selectedIndex.value)
	
			const updateRes = await updateSetting({
				serverSelection: servertype
			})
			if (updateRes.code === 200) {
				uni.showToast({
					title: '服务器大小设置成功',
					icon: 'none'
				})
			}
		} catch (err) {
			console.error("更新服务器设置失败：", err);
			uni.showToast({
				title: '设置失败，请重试',
				icon: 'none'
			})
		}
	}

	onMounted(() => {
		// 状态栏高度
		iSMT.value = uni.getSystemInfoSync().statusBarHeight;
		console.log('看看高度', iSMT.value)
		getServer()
	})
</script>

<style>
	.top {
		margin-top: 1.5vh;
		height: 21vh;
		background-color: white;
	}

	.top-list {
		width: 630rpx;
		height: 7vh;
		display: flex;
		align-items: center;
		justify-content: center;
		margin: 0 40rpx;
		padding: 0 10rpx;
		border-bottom: 1rpx solid #eee;
	}

	.top-list:last-child {
		border-bottom: none;
	}

	.switch-btn {
		width: 100rpx;
		transform: scale(0.6);
		transform-origin: center right;
	}

	.public {
		width: 450rpx;
		margin-left: auto;
		font-size: 10px;
		color: rgb(203, 203, 203);
	}

	.arrow {
		margin-left: auto;
	}
	
	.radio-btn {
		margin-left: auto;
		transform: scale(0.6);
	}
</style>