<script setup lang="ts">
import { onLaunch, onShow } from "@dcloudio/uni-app";
import { useAuthStore } from "./stores/auth";
import { useDictStore } from "./stores/dict";
import { useNotificationStore } from "./stores/notification";
import { safeHideTabBar } from "./utils";

onLaunch(() => {
  console.log("App Launch");
  const authStore = useAuthStore();
  authStore.checkLogin();

  const dictStore = useDictStore();
  dictStore.loadCommonDicts();

  // 若已登录，建立 WebSocket 连接
  if (authStore.isLoggedIn && authStore.token) {
    const notificationStore = useNotificationStore();
    notificationStore.connectSocket(authStore.token);
    notificationStore.fetchUnreadCount();
  }

  // #ifdef H5 || APP-PLUS
  // App-PLUS 原生 tabBar 仍会显示，需要强制隐藏；H5 用 CSS 兜底
  safeHideTabBar({ animation: false, repeat: 12 })
  // #endif
});

onShow(() => {
  console.log("App Show");
  const authStore = useAuthStore();
  if (authStore.isLoggedIn && authStore.token) {
    const notificationStore = useNotificationStore();
    notificationStore.ensureConnected(authStore.token);
    notificationStore.fetchUnreadCount();
  }
  // #ifdef H5 || APP-PLUS
  safeHideTabBar({ animation: false, repeat: 10 })
  // #endif
});
</script>

<style>
@import "@/styles/uni-icons.css";

/* #ifdef H5 || APP-PLUS */
/* 隐藏 H5/App 原生 tabBar，统一使用自定义 AppTabBar（按角色显示不同 tab） */
.uni-tabbar {
  display: none !important;
}
/* #endif */
</style>
