import api from '../request'
import { store } from '@/store'
export default {
    /**
     * 获取配置文件
     * @param {string} keyword      // 1.banner时   shopBanner     2.vip时 DiscountCard     3.materialDesc时  materialDesc
     * @param {string} type         // 1.banner 和  materialDesc 时  JxStore     2.Vip时  DiscountCard
     */
    getConfigInfo: async (type) => {
        try {
            let res = await api('v2/cms/QueryConfigs', {
                data: {
                    keyword: type === 'banner' ? 'shopBanner' : (type === 'vip' ? 'DiscountCard' : (type === 'materialDesc' ? 'materialDesc' : '')),
                    type: (type === 'banner' || type === 'materialDesc') ? 'JxStore' : 'DiscountCard'
                }
            })
            if (res && res.length > 0) {
                return JSON.parse(res[0].value)
            } else {
                return []
            }

        } catch (e) {
            throw (e)
        }
    },

    /**
     * 推荐商家
     */
    getNearlyStore: async (data,dns = 'wxC4') => {
        try {
            // if (!data.lat) return []
            if (!(data && data.lat)) return []

            // #ifdef MP-KUAISHOU
            // data.lat = 30.69015
            // data.lng = 104.05293
            // #endif
            
            if(data.brandID && data.brandID.length) data.brandID = JSON.stringify(data.brandID)
            let res = await api('v2/store/GetHomePageByLocation', {
                data
            },
            dns)

            // 过滤重复项
            let array = []
            let storeIDsArr = []
            if (res && res.length > 0) {
                res.map((item) => {
                    let findIndex = storeIDsArr.findIndex(i => i === item.storeInfo.storeID)
                    if (findIndex === -1) {
                        storeIDsArr.push(item.storeInfo.storeID)
                        array.push(item)
                    }
                })
            }
            return array
        } catch (e) {
            console.error('getNearlyStore', e)
            throw (e)
        }
    },

    /**
     * 获取门店信息
     */
    getStoreInfo: async (data) => {
        try {
            let res = await api('v2/store/GetStores', {
                data
            })
            return res
        } catch (e) {
            console.error('getStoreInfo', e)
            throw (e)
        }
    },

    /**
     * 获取门店减运策略
     */
    getWayBillFeeMinus: async (storeID) => {
        try {
            let res = await api('v2/store/GetStoreVendorMaps', {
                data: {
                    storeID,
                    vendorID: 9
                }
            })
            if (res && res.length > 0) {
                store.commit('indexPage/saveWaybillSLG', {
                    satisfy: res[0].deliveryFeeDeductionSill / 100,
                    reduce: res[0].deliveryFeeDeductionFee / 100
                })
            } else {
                store.commit('indexPage/saveWaybillSLG', {
                    satisfy: 0,
                    reduce: 0
                })
            }
            return res
        } catch (e) {
            console.error('getWayBillFeeMinus', e)
            throw (e)
        }
    },

    /**
     * 获取品牌
     * @param {*} brandID
     * @param {*} storeID
     * brandID
     */
    getBrand: async (data) => {
        try {
            let res = await api('v2/store/GetBrands', {
                data
            })
            return res
        } catch (e) {
            console.error('getBrand', e)
            throw (e)
        }
    },

    /**
     * 获取配送时间
     */
    getDeliverTime: async (storeID) => {
        try {
            let res = await api('v2/jxorder/GetAvailableDeliverTime', {
                data: {
                    storeID
                }
            })
            return res
        } catch (e) {
            console.error('getDeliverTime', e)
            throw (e)
        }
    },

    /**
     * 物料篇-----------
     * 刷新物料库存
     */
    refreshStock: async () => {
        try {
            let res = await api('v2/store/sku/RefreshMatterStock', {
                method: 'POST'
            })
            return res
        } catch (e) {
            console.error('', e)
            throw (e)
        }
    },

    /**
     * 物料是否能够购买，以及销量
     */
    getMatterStoreOrderStatus: async (storeID) => {
        try {
            let res = await api(`v2/order/GetMatterStoreOrderCount?storeID=${storeID}`)
            return res
        } catch (e) {
            console.error('', e)
            throw (e)
        }
    }
}