import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useAppStore = defineStore('app', () => {
  // 侧边栏状态
  const sidebarCollapsed = ref(false)
  
  // 主题设置
  const theme = ref(localStorage.getItem('theme') || 'light')
  
  // 语言设置
  const language = ref(localStorage.getItem('language') || 'zh-CN')
  
  // 加载状态
  const loading = ref(false)
  
  // 面包屑导航
  const breadcrumbs = ref([])
  
  // 切换侧边栏
  const toggleSidebar = () => {
    sidebarCollapsed.value = !sidebarCollapsed.value
  }
  
  // 设置主题
  const setTheme = (newTheme) => {
    theme.value = newTheme
    localStorage.setItem('theme', newTheme)
    document.documentElement.setAttribute('data-theme', newTheme)
  }
  
  // 设置语言
  const setLanguage = (newLanguage) => {
    language.value = newLanguage
    localStorage.setItem('language', newLanguage)
  }
  
  // 设置加载状态
  const setLoading = (status) => {
    loading.value = status
  }
  
  // 设置面包屑
  const setBreadcrumbs = (crumbs) => {
    breadcrumbs.value = crumbs
  }
  
  // 初始化主题
  const initTheme = () => {
    document.documentElement.setAttribute('data-theme', theme.value)
  }
  
  return {
    // 状态
    sidebarCollapsed,
    theme,
    language,
    loading,
    breadcrumbs,
    
    // 方法
    toggleSidebar,
    setTheme,
    setLanguage,
    setLoading,
    setBreadcrumbs,
    initTheme
  }
})