import { getDistance } from '@/utils/index'

export default {
    /**
     * 地址列表
     */
    addressList: (state) => {
        for (let i in state.list) {
            state.list[i].distance = getDistance(state.list[i].lat, state.list[i].lng)
        }
        return state.list
    },

    /**
     * 选中的送货地址
     */
    pickWaybillAd: (state) => {
        return state.pickWaybillAd
    },

    /**
     * 选择的地址信息
     */
    curPickAddress: (state) => {
        return state.curPickAddress
    },

    /**
     * 当前的配送地址
     */
    defaultAddress: (state) => {
        let arr = state.list.filter(item => item.detailAddress !== '自提订单：详细地址')
        if (arr.length === 0) return []
        // 得到设置的默认地址
        let setDefault = arr.filter(item => item.isDefault === 1)
        // 得到之前选择的地址
        let isPick = uni.getStorageSync('waybillAddress')
        let waybillAddress = state.pickWaybillAd
        if (isPick) {
            return isPick || waybillAddress
        } else {
            if (setDefault.length > 0) {
                return setDefault
            } else {
                uni.setStorageSync('waybillAddress', [arr[0]])
                return [arr[0]]
            }
        }
    }
}