<template>
	<view class="main">
		<view :style="{height:iSMT+'px'}"></view>
		<view class="theme">
			<view class="left">
				<image class="img-theme" src="/static/my/whiteTheme.png" mode="widthFix" />
				<radio value="0" class="radio-btn" activeBackgroundColor="red" :checked="selectedIndex === 0"
					@click="updateTheme('light')" />
			</view>
			<view class="left">
				<image class="img-theme" src="/static/my/blackTheme.png" mode="widthFix" />
				<radio value="1" class="radio-btn" activeBackgroundColor="red" :checked="selectedIndex === 1"
					@click="updateTheme('dark')" />
			</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 themeTypeMap = {
		'light': 0,
		'dark': 1
	}

	const getTheme = async () => {
		try {
			const res = await getSetting()
			if (res.code === 200) {
				const theme = res.data.theme
				selectedIndex.value = themeTypeMap[theme] ?? 0
			}
		} catch (err) {
			console.error("获取主题设置失败：", err);
		}
	}

	const updateTheme = async (themeType) => {
		try {
			selectedIndex.value = themeTypeMap[themeType]
			console.log('主题：', themeType, '，looklook索引：', selectedIndex.value)

			const updateRes = await updateSetting({
				theme: themeType
			})
			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)
		getTheme()
	})
</script>

<style>
	.theme {
		margin-top: 1.5vh;
		height: 34vh;
		background-color: white;
		display: flex;
		justify-content: space-around;
	}

	.img-theme {
		width: 316rpx;
		height: 362rpx;
	}

	.left {
		width: 350rpx;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
	}

	.radio-btn {
		margin-top: 36rpx;
		transform: scale(0.8);
	}
</style>