import address from '@/apis/address';

export default {
    /**
     * 获取地址列表
     */
    getList: async ({ commit }) => {
        let res = await address.getMyDeliveryAddress()
        commit('setList', res)
    },

    /**
     * 删除送货地址
     */
    delListItem: async ({ commit }, addressID) => {
        await address.delMyDeliveryAddress(addressID)
        commit('delListItem', addressID)
    },

    /**
     * 修改送货地址
     */
    updateListItem: async ({ commit, dispatch }, params) => {
        let json = { ...params }
        json.address = encodeURIComponent(params.address)
        json.detailAddress = encodeURIComponent(params.detailAddress)
        json.consigneeName = encodeURIComponent(params.consigneeName)

        await address.updateMyDeliveryAddress(json)
        await dispatch('getList')
        commit('updateListItem', json.id)
    },

    /**
     * 添加送货地址
     */
    addListItem: async ({ commit, dispatch }, params) => {
        let json = { ...params }
        json.address = encodeURIComponent(params.address)
        json.detailAddress = encodeURIComponent(params.detailAddress)
        json.consigneeName = encodeURIComponent(params.consigneeName)
        await address.addAddressList(json)
        await dispatch('getList')
    }
}