import { defineStore } from 'pinia';
import { SiteSettingForm } from '@/api/setting/siteSetting/types';

export const useSiteSettingStore = defineStore('siteSetting', {
  state: () => ({
    siteSettingInfo: {} as SiteSettingForm,
    // 添加一个标志位，用于判断是否需要从localStorage加载
    loadedFromStorage: false
  }),

  getters: {
    roundLogo: (state) => state.siteSettingInfo.roundLogo || '',
    generalBackstageName: (state) => state.siteSettingInfo.generalBackstageName || '',
    generalBackstageLogin: (state) => state.siteSettingInfo.generalBackstageLogin || ''
  },

  actions: {
    setSiteSettingInfo(data: SiteSettingForm) {
      this.siteSettingInfo = data;
      // 同时保存到localStorage
      localStorage.setItem('siteSettingInfo', JSON.stringify(data));
      this.loadedFromStorage = true;
    },

    // 从localStorage加载数据
    loadFromStorage() {
      try {
        const localData = localStorage.getItem('siteSettingInfo');
        if (localData) {
          this.siteSettingInfo = JSON.parse(localData);
          this.loadedFromStorage = true;
        }
      } catch (error) {
        console.error('加载站点设置失败:', error);
      }
    }
  }
});
