import { post, get, postL } from '@/utils/http'
import { trans } from './base'
import { SERVICE_URL } from '@/utils/config'

export default {
    apis: {
        PUBLISH: SERVICE_URL + '/api/tradeGoods/publishFromInventory',
        LIST: SERVICE_URL + '/api/tradeGoods',
        DETAIL: SERVICE_URL + '/api/tradeGoods/detail',
        LIST_MY: SERVICE_URL + '/api/tradeGoods/listMy',
        CANCEL: SERVICE_URL + '/api/tradeGoods/cancel',
        CHANGE: SERVICE_URL + '/api/tradeGoods/changePrice',
        LIST_BID_MY: SERVICE_URL + '/api/tradeBid/listMy',
        LIST_BID_BY_GOODS: SERVICE_URL + '/api/tradeBid/listTradeGoods',
        BID: SERVICE_URL + '/api/tradeBid/submit',

        PUBLISH_LIST: SERVICE_URL + '/api/tradeBid/listMy',

        PREVIEW_SUBMIT: SERVICE_URL + '/api/tradeOrder/preSubmit',
        APPLY: SERVICE_URL + '/api/tradeOrder/submit',

        BUY_LIST: SERVICE_URL + '/api/tradeOrder/pageBuyer',
        SELL_LIST: SERVICE_URL + '/api/tradeOrder/pageSeller'
    },
    async publish(inventorys) {
        const res = await trans(postL(this.apis.PUBLISH, { inventorys }))
        return res.success
    },
    async list(current, size, priceMin, priceMax, keyword, loading) {
        let param = {
            current,
            size
        }
        !!priceMin && (param.priceMin = priceMin - 0)
        !!priceMax && (param.priceMax = priceMax - 0)
        !!keyword && (param.keyword = keyword)
        const res = await trans(post(this.apis.LIST, param, { loading }))
        return res.data
    },
    async detail(goodsId, loading) {
        const res = await trans(get(this.apis.DETAIL, { goodsId }, { loading }))
        return res.data
    },
    async listMy(current, size, status, loading) {
        const res = await trans(post(this.apis.LIST_MY, { current, size, status }, { loading }))
        return res.data
    },
    async bidListByGoods(current, size, tradeGoodsId) {
        const res = await trans(post(this.apis.LIST_BID_BY_GOODS, { current, size, tradeGoodsId }))
        return res.data
    },
    async cancel(goodsId) {
        const res = await trans(postL(this.apis.CANCEL + '?goodsId=' + goodsId))
        return res.success
    },
    async change(goodsId, price) {
        const res = await trans(postL(this.apis.CHANGE + '?goodsId=' + goodsId + '&price=' + price))
        return res.success
    },
    async listBidMy(current, size, loading) {
        const res = await trans(post(this.apis.LIST_BID_MY, { current, size }, { loading }))
        return res.data
    },
    async bid(tradeGoodsId, price) {
        const res = await trans(postL(this.apis.BID, { tradeGoodsId, price }))
        return res.success
    },
    async previewSubmit(goodsId) {
        const res = await trans(postL(this.apis.PREVIEW_SUBMIT, { goodsId }))
        return res.data
    },
    async apply(goodsId, paymentType) {
        const res = await trans(
            postL(this.apis.APPLY, { goodsId, paymentType })
        )
        return res.data
    },
    async buyList(current, size, tab, loading) {
        const res = await trans(post(this.apis.BUY_LIST, { current, size, tab }, { loading }))
        return res.data
    },
    async sellList(current, size, tab, loading) {
        const res = await trans(post(this.apis.SELL_LIST, { current, size, tab }, { loading }))
        return res.data
    }
}
