import { post, get, postL } from '@/utils/http'
import { trans } from './base'
import { SERVICE_URL } from '@/utils/config'
import store from '@/store'

export default {
    apis: {
        LIST: SERVICE_URL + '/api/addressBook',
        ADD: SERVICE_URL + '/api/addressBook/add',
        DELETE: SERVICE_URL + '/api/addressBook/delete/{?}',
        DEFAULT: SERVICE_URL + '/api/addressBook/getDefault',
        DEFAULT_UPDATE: SERVICE_URL + '/api/addressBook/update',
        UPDATEADDRESS: SERVICE_URL + '/api/mallOrder/updateAddress',
        getArea:SERVICE_URL + '/api/area'
    },
    async default() {
        const res = await trans(get(this.apis.DEFAULT))
        if (res && res.data && res.data.id) {
            return res.data
        }
        return null
    },
    async list(loading = false, size = 100) {
        const res = await trans(get(this.apis.LIST, { size }, { loading }))
        return res.data
    },
    async add(contactName, contactNo, location, province, city, district, address, defaultFlag) {
        const res = await trans(postL(this.apis.ADD, {
            contactName,
            contactNo,
            location,
            province,
            city,
            district: district || '',
            address,
            defaultFlag
        }))
        return res.success
    },
    async delete(id) {
        const res = await trans(postL(this.apis.DELETE, null, { pathParam: [id] }))
        return res.success
    },
    async setDefault(item) {
        const res = await trans(postL(this.apis.DEFAULT_UPDATE, item))
        return res.success
    },
    async updateAddress(params) {
        const res = await trans(postL(this.apis.UPDATEADDRESS, { ...params }))
        return res
    },
    async getArea(params){
      const res = await trans(get(this.apis.getArea, { ...params }))
      return res
    }
}
