import http from "../../../api/fastposter";
 
function removeTrailingSlash(str) {
    if (str.endsWith('/')) {
        return str.slice(0, -1); // 截取从索引0到倒数第2个字符之间的内容
    }
    return str; // 如果最后一个字符不是斜杠，则原样返回字符串
}

const state = {
    debug: false,
    showLogin: false,
    showCode: false,
    showChangePwd: false,
    showTokenManager: false,
    showUserManager: false,
    showBatch: false,
    batchPosterId: 0,   // 批量海报ID
    batchId: 0,   // 批量ID
    user: {
        username: '',
        type: 1
    },
    fonts: [],
    token: '',
}

const getters = {
    debug() {
        return state.debug
    },
    isLogin() {
        return state.user.username !== ''
    },
    token() {
        return http.token()
    },
    apiUrl() {
        return removeTrailingSlash(http.baseURL)
    },
    uploadUrl() {
        return http.baseURL + 'api/upload'
    },
    fonts() {
        return state.fonts
    }
}

const actions = {
    setDebug({commit}, flag) {
        commit('setDebug', flag)
    },
    changepwd({commit}, playload) {
        return new Promise((resolve) => {
            http.changepwd(playload, r => {
                if (r.code === 0) {
                    resolve(r)
                }
            })
        });
    },
    login({commit}, playload) {
        return new Promise((resolve) => {
            http.login(playload, r => {
                if (r.code === 0) {
                    commit('login', r)
                    actions.getUserInfo({commit})
                    resolve(r)
                }
            })
        });
    },
    logout({commit}, playload) {
        return new Promise((resolve) => {
            http.logout(playload, r => {
                if (r.code === 0) {
                    commit('logout', r)
                    resolve(r)
                }
            })
        });
    },
    getUserInfo({commit}, playload) {
        return new Promise((resolve) => {
            http.userinfo(playload, r => {
                if (r.code === 0) {
                    commit('setUserInfo', r)
                    actions.loadFonts({commit})
                    resolve(r)
                }
            })
        });
    },
    loadFonts({commit}, playload) {
        return new Promise((resolve) => {
            http.http.get('api/fonts').then(r => {
                if (r.code === 0) {
                    commit('setFonts', r)
                    // 异步加载字体
                    r.data.fonts.forEach(fn => http.loadfont(fn.code))
                    resolve(r)
                }
            })
        })
    }
}
const mutations = {
    setDebug(state, flag) {
        state.debug = flag
    },
    login(state, r) {
        state.token = r.data.token
        localStorage.setItem(process.env.VUE_APP_FP_TOKEN, r.data.token)
        state.showLogin = false
    },
    logout(state, r) {
        localStorage.removeItem(process.env.VUE_APP_FP_TOKEN)
        state.token = ''
        let x = {
            username: '', type: 1
        }
        state.user = {...x}
    },
    setUserInfo(state, r) {
        state.token = localStorage.getItem(process.env.VUE_APP_FP_TOKEN)
        state.user = {...r.data.user}
    },
    setFonts(state, r) {
        state.fonts = r.data.fonts
    }
}

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