import { v4 } from 'uuid'
import { useLoginStore } from '@/stores'
import { ClientType } from '@/constants/client'
import http from '@/services/http'
import tradeServer from '@/services/socket/trade'
import moment from 'moment'

const loginStore = useLoginStore()
const { userId, firstAccountId } = loginStore.$toRefs()

/**
 * 预售大厅/我的预售/集采大厅/我的集采 列表查询
 */
export function queryGZPreSell(params: Ermcp.GZPreSellReq) {
    return http.commonRequest<Ermcp.GZPreSellRsp[]>({
        url: '/Guangzuan/QueryGZPreSell',
        params,
    })
}

/**
 * 我的预售申请列表查询
 */
export function queryGZWrPreSaleApply(params: Ermcp.GZWrPreSaleApplyReq) {
    params.userid = userId.value
    return http.commonRequest<Ermcp.GZWrPreSaleApplyRsp[]>({
        url: '/Guangzuan/QueryGZWrPreSaleApply',
        params,
    })
}

/**
 * 我的预售认购列表查询
 */
export function queryGZMyPresell(params: Ermcp.GZMyPresellReq) {
    return http.commonRequest<Ermcp.GZMyPresellRsp[]>({
        url: '/Guangzuan/QueryGZMyPresell',
        params,
    })
}

/**
 * 我参与的预售(预售中\执行中)\我参与的集采(集采中\执行中) 列表查询
 */
export function queryGZMyTradingPreSell(params: Ermcp.GZMyTradingPreSellReq) {
    params.userid = userId.value
    return http.commonRequest<Ermcp.GZMyTradingPreSellRsp[]>({
        url: '/Guangzuan/QueryGZMyTradingPreSell',
        params,
    })
}

/**
 * 查询预售申请价格表
 */
export function queryPresaleorderapplyprice(params: Ermcp.PresaleorderapplypriceReq) {
    return http.commonRequest<Ermcp.PresaleorderapplypriceRsp[]>({
        url: '/Guangzuan/QueryPresaleorderapplyprice',
        params,
    })
}

/**
 * 广钻预售申请
 */
export function gzPresaleApply(params: Partial<Proto.GZPresaleApplyReq>) {
    return tradeServer.sendMessage<Proto.GZPresaleApplyRsp>({
        params: {
            SellUserID: userId.value,
            SellAccountID: firstAccountId.value,
            MarketID: 63201,
            ClientType: ClientType.Web,
            ClientSerialNo: v4(),
            ...params
        },
        reqName: 'GZPresaleApplyReq',
        rspName: 'GZPresaleApplyRsp',
        marketId: 63201
    })
}

/**
 * 广钻预售认购下单
 */
export function gzPresaleOrder(params: Partial<Proto.GZPresaleOrderReq>) {
    return tradeServer.sendMessage<Proto.GZPresaleOrderRsp>({
        params: {
            UserID: userId.value,
            AccountID: firstAccountId.value,
            MarketID: 63201,
            ClientOrderTime: moment().format('YYYY-MM-DD HH:mm:ss'),
            ClientType: ClientType.Web,
            ClientSerialNo: v4(),
            ...params
        },
        reqName: 'GZPresaleOrderReq',
        rspName: 'GZPresaleOrderRsp',
        marketId: 63201
    })
}

/**
 * 广钻集采申请
 */
export function gzCenterPurchaseApply(params: Partial<Proto.GZCenterPurchaseApplyReq>) {
    return tradeServer.sendMessage<Proto.GZCenterPurchaseApplyRsp>({
        params: {
            SellUserID: userId.value,
            SellAccountID: firstAccountId.value,
            MarketID: 62201,
            ClientType: ClientType.Web,
            ClientSerialNo: v4(),
            ...params
        },
        reqName: 'GZCenterPurchaseApplyReq',
        rspName: 'GZCenterPurchaseApplyRsp',
        marketId: 62201
    })
}

/**
 * 广钻集采认购下单
 */
export function gzCenterPurchaseOrder(params: Partial<Proto.GZCenterPurchaseOrderReq>) {
    return tradeServer.sendMessage<Proto.GZCenterPurchaseOrderRsp>({
        params: {
            UserID: userId.value,
            AccountID: firstAccountId.value,
            MarketID: 62201,
            ClientOrderTime: moment().format('YYYY-MM-DD HH:mm:ss'),
            ClientType: ClientType.Web,
            ClientSerialNo: v4(),
            ...params
        },
        reqName: 'GZCenterPurchaseOrderReq',
        rspName: 'GZCenterPurchaseOrderRsp',
        marketId: 62201
    })
}