import api from '@/request/api'

const state = {
    //全局系统配置
    systemconfig:[]
}

const mutations = {
    /**设置系统配置 */
    SET_CONFIG(state,config){
        state.systemconfig = config
    }
}

const actions = {
    /**
     * 初始化配置
     */
    async fetch_config({commit}){
        try{
            const res = await api.getAllConfig()
            commit('SET_CONFIG',res.data)
        }catch(e){
            console.error('初始化配置失败'.e)
        }
    }
}

const getters = {
    /**
     * 将后端返回的系统配置数组转换为对象格式
     * @returns {object}
     */
    configMap: (state) => {
    const map = {}
    state.systemconfig.forEach(item => {
      map[item.configName] = item.configBody
    })
    return map
  },
    systemname: (state,getters) => getters.configMap.SystemName || '青蓝',
    systemdesc: (state,getters) => getters.configMap.SystemDescription || '无',
    systemlogo: (state,getters) => getters.configMap.LogoLocation || 'https://img.picui.cn/free/2025/06/11/684900b15ecc4.jpg',
    systemphone: (state,getters) => getters.configMap.Phone || '13000000000',
    systememail: (state,getters) => getters.configMap.Email || 'admin@admin.com'
}

export default {
    namespaced:true,
    state,
    mutations,
    actions,
    getters
}