<template>
  <view class="popup-mask" v-if="visible" @click="handleClose">
    <view class="popup-content" @click.stop>
      <text class="popup-title">{{ t('language.title') }}</text>
      <view class="lang-list">
        <view
          v-for="lang in languages"
          :key="lang.value"
          class="lang-item"
          :class="{ active: appStore.locale === lang.value }"
          @click="handleSelect(lang.value)"
        >
          <text class="lang-text">{{ lang.label }}</text>
          <text v-if="appStore.locale === lang.value" class="lang-check">✓</text>
        </view>
      </view>
    </view>
  </view>
</template>

<script setup>
import { useI18n } from 'vue-i18n'
import { useAppStore } from '../stores/app.js'

const { t } = useI18n()
const appStore = useAppStore()

defineProps({
  visible: {
    type: Boolean,
    default: false
  }
})

const emit = defineEmits(['close'])

// 语言选项列表
const languages = [
  { label: '简体中文', value: 'zh-CN' },
  { label: '繁體中文', value: 'zh-TW' },
  { label: 'English', value: 'en' }
]

function handleSelect(locale) {
  appStore.setLocale(locale)
  emit('close')
  // 切换语言后重新加载所有页面
  uni.reLaunch({ url: '/pages/index/index' })
}

function handleClose() {
  emit('close')
}
</script>

<style scoped>
.popup-mask {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 999;
}

.popup-content {
  width: 600rpx;
  background-color: #ffffff;
  border-radius: 24rpx;
  padding: 40rpx;
}

.popup-title {
  font-size: 34rpx;
  font-weight: 600;
  color: #333;
  text-align: center;
  display: block;
  margin-bottom: 30rpx;
}

.lang-list {
  margin-bottom: 10rpx;
}

.lang-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 28rpx 24rpx;
  border-bottom: 1rpx solid #f0f0f0;
}

.lang-item:last-child {
  border-bottom: none;
}

.lang-item.active {
  color: #007aff;
}

.lang-text {
  font-size: 30rpx;
  color: #333;
}

.lang-item.active .lang-text {
  color: #007aff;
}

.lang-check {
  font-size: 32rpx;
  color: #007aff;
}
</style>
