import api from '../request'
import { gdMapKey } from "@/apis/config"
import { json2query } from '@/utils'

export default {
    /**
     * 高德坐标解析
     */
    posToCity: async (data) => {
        try {
            let res = await api('v3/geocode/regeo', {
                data: {
                    key: gdMapKey,
                    location: `${data.longitude},${data.latitude}`,
                    extensions: data.extensions ? 'all' : 'base'
                }
            }, 'gdMap')
            return res
        } catch (e) {
            console.error('posToCity', e)
            throw (e)
        }
    },

    /**
     * 获取建议地址
     */
    getSuggestion: async (keywords, city) => {
        try {
            let res = await api('v3/place/text', {
                data: {
                    key: gdMapKey,
                    keywords,
                    city,
                    citylimit: true
                }
            }, 'gdMap')
            return res
        } catch (e) {
            console.error('getSuggestion', e)
            throw (e)
        }
    },

    /**
     * 按当前位置匹配门店
     */
    storeListByLocation: async (data) => {
        try {

            // #ifdef MP-KUAISHOU
            // data.lat = 30.69015
            // data.lng = 104.05293
            // #endif

            let res = await api('v2/store/GetStoreListByLocation', {
                data
            })
            return res
        } catch (e) {
            console.error('storeListByLocation', e)
            throw (e)
        }
    },

    /**
     * 关键字检索城市
     */
    getCity: async (params) => {
        try {
            let res = await api('v2/cms/GetPlaces', {
                data: {
                    level: params.level,
                    includeDisabled: true,
                    keyword: params.keyword
                }
            })
            return mapCity(res || [])
        } catch (e) {
            console.error('getCity', e)
            throw (e)
        }
    },

    /**
     * 查询地址管理
     * @param {*} params 暂无
     */
    getMyDeliveryAddress: async (params = {}) => {
        try {
            let res = await api('v2/user2/QueryMyDeliveryAddress', {
                data: params
            })
            return res
        } catch (e) {
            console.error('getMyDeliveryAddress', e)
            throw (e)
        }
    },

    /**
     * 添加地址
     * @param {Object} form 参数对象
     * consigneeName 收货人
     * consigneeMobile 收货人手机
     * address 详细地址
     * detailAddress
     * lng 经度 Number
     * lat 纬度 Number
     * 
     * 非必须
     * tag 标签（家、公司、学校）
     * remark 备注
     * isDefault 是否是默认 true false
     */
    addAddressList: async (form = {}) => {
        try {
            let res = await api('v2/user2/AddMyDeliveryAddress', {
                method: 'POST',
                data: json2query(form)
            })
            return res
        } catch (e) {
            console.error('addMyDeliveryAddress', e)
            throw (e)
        }
    },

    /**
     * 修改地址
     * @param {Object} form 
     */
    updateMyDeliveryAddress: async (form = {}) => {
        try {
            let res = await api('v2/user2/UpdateMyDeliveryAddress', {
                method: 'PUT',
                data: json2query(form)
            })
            return res
        } catch (e) {
            console.error('updateMyDeliveryAddress', e)
            throw (e)
        }
    },

    /**
     * 删除地址
     * @param {Number} addressID 地址ID
     */
    delMyDeliveryAddress: async (addressID) => {
        try {
            let res = await api('v2/user2/DeleteMyDeliveryAddress?id=' + addressID, {
                method: 'DELETE'
            })
            return res
        } catch (e) {
            console.error('delMyDeliveryAddress', e)
            throw (e)
        }
    }

}

const mapCity = (arr) => {
    if (arr.length === 0) return []
    return arr.map(item => ({
        id: item.id,
        code: item.code,
        name: item.name
    }))
}