<template>
	<view class="setting-page">
		<view class="group">
			<view class="item" @click="showLanguageSelector">
				<text class="label">{{ $t('settings.language') }}</text>
				<view class="right">
					<text class="value">{{ currentLanguageText }}</text>
					<uv-icon name="arrow-right" size="16" color="#c8c8c8"></uv-icon>
				</view>
			</view>
		</view>
		<view class="group">
			<view class="item" @click="navigateTo('/subPackages/other/legal/agreement')">
				<text class="label">{{ $t('user.userAgreement') }}</text>
				<uv-icon name="arrow-right" size="16" color="#c8c8c8"></uv-icon>
			</view>
			<view class="item" @click="navigateTo('/subPackages/other/legal/privacy')">
				<text class="label">{{ $t('user.privacyPolicy') }}</text>
				<uv-icon name="arrow-right" size="16" color="#c8c8c8"></uv-icon>
			</view>
			<view class="item" @click="navigateTo('/subPackages/other/legal/terms')">
				<text class="label">{{ $t('legal.termsAndConditions') }}</text>
				<uv-icon name="arrow-right" size="16" color="#c8c8c8"></uv-icon>
			</view>
		</view>
		<view class="group">
			<view class="item" @click="handleLogout">
				<text class="label">{{ $t('user.logout') }}</text>
				<uv-icon name="arrow-right" size="16" color="#c8c8c8"></uv-icon>
			</view>
		</view>
	</view>
</template>

<script setup>
import { ref, computed, onMounted, getCurrentInstance } from 'vue'
import { userLogout } from '@/config/api/user.js'
import { useI18n } from '@/utils/i18n.js'

const { t } = useI18n()

// 获取全局 i18n 实例
const instance = getCurrentInstance()
const globalI18n = instance?.appContext?.config?.globalProperties?.$i18n

// 设置页面标题
onMounted(() => {
	uni.setNavigationBarTitle({
		title: t('settings.title')
	})
})

// 当前语言
const currentLanguage = ref(uni.getStorageSync('language') || 'zh-CN')

// 当前语言文本显示
const currentLanguageText = computed(() => {
	if (currentLanguage.value === 'zh-CN') {
		return t('settings.chinese')
	} else if (currentLanguage.value === 'id-ID') {
		return t('settings.indonesian')
	} else {
		return t('settings.english')
	}
})

const navigateTo = (url) => {
	uni.navigateTo({ url })
}

// 显示语言选择器
const showLanguageSelector = () => {
	const languages = [
		{ code: 'zh-CN', label: t('settings.chinese') },
		{ code: 'en-US', label: t('settings.english') },
		{ code: 'id-ID', label: t('settings.indonesian') }
	]
	
	uni.showActionSheet({
		itemList: languages.map(lang => lang.label),
		success: (res) => {
			const selectedLang = languages[res.tapIndex].code
			if (selectedLang !== currentLanguage.value) {
				// 1. 保存到缓存
				uni.setStorageSync('language', selectedLang)
				
				// 2. 立即更新 i18n 实例（重要！）
				if (globalI18n) {
					globalI18n.locale = selectedLang
				}
				
				// 3. 更新当前语言状态
				currentLanguage.value = selectedLang
				
				// 4. 提示用户
				uni.showToast({
					title: t('settings.languageSwitched'),
					icon: 'none',
					duration: 800
				})
				
				// 5. 延迟后重新加载应用（确保 i18n 更新已生效）
				setTimeout(() => {
					// 使用 reLaunch 完全重启应用
					uni.reLaunch({
						url: '/pages/index/index'
					})
				}, 800)
			}
		}
	})
}

const handleLogout = async () => {
	uni.showModal({
		title: t('common.tips'),
		content: t('user.confirmLogout'),
		success: async (res) => {
			if (res.confirm) {
				const response = await userLogout();
				if (response.code == 200) {
					uni.showToast({ title: t('user.logoutSuccess'), icon: 'none' })
					setTimeout(() => {
						uni.removeStorageSync('token')
						uni.removeStorageSync('userInfo')
						uni.reLaunch({ url: '/subPackages/user/login/index' })
					}, 1200)
				}
			}
		}
	})
}
</script>

<style lang="scss" scoped>
	.setting-page {
		min-height: 100vh;
		background-color: #f6f6f6;
		padding-bottom: env(safe-area-inset-bottom);
	}

	.group {
		margin-top: 20rpx;
		background-color: #ffffff;
		border-top: 1rpx solid #f0f0f0;
		border-bottom: 1rpx solid #f0f0f0;
	}

	.item {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 28rpx 30rpx;
		border-top: 1rpx solid #f5f5f5;
	}

	.item:first-child {
		border-top: none;
	}

	.label {
		font-size: 30rpx;
		color: #333333;
	}

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

	.value {
		font-size: 28rpx;
		color: #999999;
	}
</style>