import { uniGetPosition } from '@/utils/uniapi'
import apiStore from '@/apis/apiStore';
import goodPage from '@/apis/goodPage';
import address from '@/apis/address';
import { mapSkuNames } from '@/utils'
export default {
    /**
     * 获取定位
     */
    getPosition: async ({ commit, getters }) => {
        let res = await uniGetPosition()

        let latitude = res.latitude ? res.latitude : getters.myLocation.lat
        let longitude = res.longitude ? res.longitude : getters.myLocation.lng

        let analyzeInfo = await address.posToCity({ longitude, latitude, extensions: 1 })

        commit('setMylocation', {
            lat: latitude,
            lng: longitude,
            city: analyzeInfo.addressComponent.city
        })
        commit('setPois', analyzeInfo.pois)
    },

    /**
     * 按当前位置匹配门店
     */
    storeListByLocation: async ({ commit }, params) => {
        if (params.isMaterial) {
            console.log('物料申请')
        } else {
            let res = await address.storeListByLocation(params.data)
            if (res.length === 0) return
            res = res.filter(item => item.id !== 666666)    // 剔除物料申请门店666666
            let storeMapJson = {}
            res.forEach((item, index) => {
                storeMapJson[item.id] = {
                    name: item.name,
                    status: item.status,
                    distance: item.distance,
                    mobile: item.tel1,
                    index: index + 1,
                    item: item,
                    address: {
                        address: item.address,
                        name: item.name
                    }
                }
            })
            commit('saveStoreMap', storeMapJson)
            commit('changeStoreID', [res[0].id])

            // 获取该门店的信息
            let storeIDInfo = await apiStore.getStoreInfo({ storeID: res[0].id })
            commit('saveStoreInfo', storeIDInfo)    // 保存当前门店信息
        }
    },

    /**
     * 获取门店信息
     * storeID
     */
    getStoreInfo: async ({ commit, getters, state }, params) => {
        let res = await apiStore.getStoreInfo({ storeID: params.storeID })
        if (params.type && params.type === 'fromStoreID') {

            // #ifdef MP-WEIXIN
            // 存储来自哪的信息
            if (res && res.stores.length !== 0) {
                let obj = {
                    ...state.materialInfo,
                    fromStoreInfo: res.stores[0]           // 来自哪里的信息
                }
                commit('saveMaterialInfo', obj)
            }
            // #endif

        } else {
            if (res && res.stores && res.stores.length !== 0) {
                let obj = getters.storeMap ? getters.storeMap : {}
                obj[res.stores[0].id] = {
                    name: res.stores[0].name,
                    status: res.stores[0].status,
                    distance: res.stores[0].distance ? res.stores[0].distance : 0,
                    // lat:res.stores[0].lat,
                    // lng: res.stores[0].lng,
                    mobile: res.stores[0].tel1,
                    index: 0,
                    item: res.stores[0],
                    address: {
                        address: res.stores[0].address,
                        name: res.stores[0].name
                    }
                }
                if (res.stores[0].brandID && getters.queryStoreID) commit('saveBrandID', res.stores[0].brandID) // 扫码进店，改变brandID
                commit('saveStoreMap', obj)
            }
        }



    },

    /**
     * 获取品牌信息
     */
    getBrandInfo: async ({ commit }, params) => {
        let res = await apiStore.getBrand(params)
        if (!res) return
        uni.setNavigationBarTitle({
            title: res[0].name
        })
        commit('saveBrandID', res[0].id)
    },

    /**
     * 获取热销商品
     */
    getTopGoods: async ({ commit }, storeID) => {
        let res = await goodPage.getTopGoods(storeID)
        let skuList = res || []
        commit('saveTopGoods', mapSkuNames(skuList))
    }
}