<template>
  <el-config-provider :locale="locale" :size="assemblySize" :button="buttonConfig">
    <router-view></router-view>
  </el-config-provider>
</template>

<script setup lang="ts">
import {onMounted, reactive, computed, ref} from "vue";
import { useI18n } from "vue-i18n";
import { getBrowserLang } from "@/utils";
import { useTheme } from "@/hooks/useTheme";
import {ElConfigProvider, ElNotification} from "element-plus";
import { LanguageType } from "./stores/interface";
import { useGlobalStore } from "@/stores/modules/global";
import en from "element-plus/es/locale/lang/en";
import zhCn from "element-plus/es/locale/lang/zh-cn";
import {Login, Platform} from "@/api/interface";
import {loginApi} from "@/api/modules/login";
import {initDynamicRouter} from "@/routers/modules/dynamicRouter";
import {HOME_URL} from "@/config";
import {useUserStore} from "@/stores/modules/user";
import {getPlatformList} from "@/api/modules/allData";

const globalStore = useGlobalStore();

// init theme
const { initTheme } = useTheme();
initTheme();

// init language
const i18n = useI18n();
onMounted(() => {
  const language = globalStore.language ?? getBrowserLang();
  i18n.locale.value = language;
  globalStore.setGlobalState("language", language as LanguageType);
});

// element language
const locale = computed(() => {
  if (globalStore.language == "zh") return zhCn;
  if (globalStore.language == "en") return en;
  return getBrowserLang() == "zh" ? zhCn : en;
});

// element assemblySize
const assemblySize = computed(() => globalStore.assemblySize);

// element button config
const buttonConfig = reactive({ autoInsertSpace: false });



const loginForm = reactive<Login.ReqLoginForm>({
  rememberMe: false,
  username: "",
  password: ""
});

const userStore = useUserStore();
const loginStationsList = async () => {
  loginForm.username = localStorage.getItem('username2')
  loginForm.password = localStorage.getItem('password2')
  if(!loginForm.username||!loginForm.password){
    return
  }
  try {
    const {data} = await loginApi({...loginForm, password: loginForm.password});
    if (!data.user) {
      return;
    }

    userStore.setToken(Date.now().toString());
    userStore.setUserInfo(data.user)
    userStore.setAsInfo(data.asInfo)
    getStationsList();
  } finally {
  }
};
// 站号列表更新
const getStationsList = async () => {
  const userStore = useUserStore();
  const user = computed(() => userStore.userInfo);
  const Info = computed(() => userStore.asInfo);

  // 查询自动站列表
  const queryas = ref<Platform>({ pageSize: 20000, pageNum: 1 });
  const { data } = await getPlatformList(queryas.value);
  const array2 = data.list;

  if (user.value.role_id === '管理员') {
    userStore.setStations(array2);
  } else {
    const codesToMatch = Info.value.map(item => item.as_code);
    const filteredArray = array2.filter(item => codesToMatch.includes(item.as_code));
    userStore.setStations(filteredArray);
  }
};

onMounted(() => {
  loginStationsList()
})

</script>
<style>
body {
  font-family: 'PingFang-Medium', sans-serif;
}
</style>
