<template>
  <el-dropdown
    class="international"
    trigger="click"
    @command="handleSelectLanguage"
  >
    <div>
      <el-tooltip content="国际化" :effect="effect">
        <svg-icon icon="language"></svg-icon>
      </el-tooltip>
    </div>
    <template #dropdown>
      <el-dropdown-menu>
        <el-dropdown-item :disabled="language === 'zh'" command="zh"
          >中文</el-dropdown-item
        >
        <el-dropdown-item :disabled="language === 'en'" command="en"
          >英文</el-dropdown-item
        >
      </el-dropdown-menu>
    </template>
  </el-dropdown>
</template>

<script setup>
import { computed, defineProps } from 'vue'
import { useStore } from 'vuex'
import { useI18n } from 'vue-i18n'
import { ElMessage } from 'element-plus'

defineProps({
  effect: {
    type: String,
    default: 'dark',
    validator: function (value) {
      return ['dark', 'light'].indexOf(value) !== -1
    }
  }
})

const store = useStore()
const language = computed(() => store.getters.language)

const i18n = useI18n()
const handleSelectLanguage = (lang) => {
  //切换i18n的locale
  i18n.locale.value = lang
  //修改vuex中language
  store.commit('app/setLanguage', lang)
  //提示
  ElMessage.success('更新成功')
  //刷新下，不然element-plus无法更新语言
  location.reload()
}
</script>

<style></style>
