<template>
  <a-config-provider :locale="zhCN">
    <BasicLayout
      v-if="showLayout"
      :userName="authStore.user?.name"
      @logout="handleLogout"
    >
      <router-view />
    </BasicLayout>
    <router-view v-else />
  </a-config-provider>
</template>

<script setup lang="ts">
import { computed, onMounted } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import { message } from 'ant-design-vue';
import zhCN from 'ant-design-vue/es/locale/zh_CN';
import { DashboardOutlined, ShopOutlined, UserOutlined } from '@ant-design/icons-vue';
import BasicLayout from '@/layouts/BasicLayout.vue';
import { useAuthStore } from '@/stores/auth';
import { useLayoutStore } from '@/stores/layout';

const router = useRouter();
const route = useRoute();
const authStore = useAuthStore();
const layoutStore = useLayoutStore();

// 初始化布局配置
onMounted(() => {
  layoutStore.init({
    menuItems: [
      { key: 'dashboard', label: '主页看板', icon: DashboardOutlined, path: '/' },
      { key: 'factories', label: '工厂管理', icon: ShopOutlined, path: '/factories' },
      { key: 'users', label: '系统用户', icon: UserOutlined, path: '/users' }
    ]
  });
});

// 根据路由 meta 判断是否显示布局
const showLayout = computed(() => {
  return route.meta.showLayout !== false;
});

// 登出处理
async function handleLogout() {
  await authStore.logout();
  message.success('已退出登录');
  router.push('/login');
}
</script>

<style>
#app {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial,
    'Noto Sans', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol',
    'Noto Color Emoji';
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}
</style>