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

export const useSystemSettingsStore = defineStore('systemSettings', () => {
  // 品牌设置
  const brandName = ref('XYZW 控制台')
  const brandLogo = ref('/icons/xiaoyugan.png')
  
  // 从localStorage加载设置
  const loadSettings = () => {
    try {
      const savedBrandName = localStorage.getItem('system_brand_name')
      const savedBrandLogo = localStorage.getItem('system_brand_logo')
      
      if (savedBrandName) {
        brandName.value = savedBrandName
      }
      if (savedBrandLogo) {
        brandLogo.value = savedBrandLogo
      }
    } catch (error) {
      console.error('加载系统设置失败:', error)
    }
  }
  
  // 保存设置到localStorage
  const saveSettings = () => {
    try {
      localStorage.setItem('system_brand_name', brandName.value)
      localStorage.setItem('system_brand_logo', brandLogo.value)
    } catch (error) {
      console.error('保存系统设置失败:', error)
    }
  }
  
  // 更新品牌名称
  const updateBrandName = (name) => {
    brandName.value = name
    saveSettings()
  }
  
  // 更新品牌Logo
  const updateBrandLogo = (logo) => {
    brandLogo.value = logo
    saveSettings()
  }
  
  // 重置为默认设置
  const resetToDefault = () => {
    brandName.value = 'XYZW 控制台'
    brandLogo.value = '/icons/xiaoyugan.png'
    saveSettings()
  }
  
  // 初始化时加载设置
  loadSettings()
  
  return {
    brandName,
    brandLogo,
    updateBrandName,
    updateBrandLogo,
    resetToDefault,
    loadSettings
  }
})

