import api from '../request'
import { json2query } from '@/utils'

export default {
    /**
     * 获取购物车列表
     */
    getMyCart: async (storeIDs) => {
        try {
            if (!(storeIDs && storeIDs.length !== 0)) return
            let res = await api('v2/user2/LoadMyCart', {
                data: {
                    storeIDs: JSON.stringify(storeIDs)
                }
            })
            return res
        } catch (e) {
            console.error('getMyCart', e)
            throw (e)
        }
    },

    /**
     * 存储购物车信息
     * @params {Number} StoreID
     * @params {String} payload 购物车商品列表
     * payload [] 
     * {
     *   skuID, count, price, storeID, isChecked
     * }
     */
    saveToCart: async (storeID, list) => {
        try {
            let payload = list.filter(item => item.storeID === storeID).map(item => ({
                skuID: item.id,
                count: item.count,
                price: item.curPrice,
                storeID: item.storeID,
                isChecked: item.checked ? 1 : 0
            }))
            let res = await api('v2/user2/SaveMyCart', {
                method: 'POST',
                data: json2query({
                    storeID,
                    payload: JSON.stringify(payload)
                })
            })
            return res
        } catch (e) {
            console.error('saveToCart', e)
            throw (e)
        }
    },

    /**
     * 批量采购****************************
     */
    getSupplySupportStoreSkus: async (json) => {
        try {
            let res = await api('v2/jxorder/GetSupplySupportStoreSkus', {
                data: {
                    ...json
                }
            })
            return res
        } catch (e) {
            console.error('getSupplySupportStoreSkus', e)
            throw (e)
        }
    }
}