import { shallowRef, reactive } from 'vue'
import { v4 } from 'uuid'
import { ClientType } from '@/constants/client'
import { Market } from '@/constants/market'
import { useDataTable } from '@/hooks/datatable'
import { queryMyBargainApply, queryMyDelistingApply, zsBuyOrderDestingNegPriceOperate, zsSellOrderDestingApplyOperate } from '@/services/api/trade'
import { useLoginStore } from '@/stores'
import { BuyOrSell } from '@/constants/order'
import Long from 'long'

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

/**
 * 我的询价
 */
export function useBargain() {
    const { dataList, total, pageIndex, pageSize } = useDataTable<Ermcp.MyBargainApplyRsp | Ermcp.MyDelistingApplyRsp>()
    const loading = shallowRef(false)

    // 获取我的询价列表
    const getBargainOrderList = async (params: Partial<(Ermcp.MyBargainApplyReq | Ermcp.MyDelistingApplyReq) & { buyorsell: number }> = {}) => {
        loading.value = true
        dataList.value = []

        const param: Ermcp.MyBargainApplyReq | Ermcp.MyDelistingApplyReq = {
            type: 0,
            page: pageIndex.value,
            pagesize: pageSize.value,
            userid: userId.value,
            applystatus: params.applystatus,
        }

        switch (params.buyorsell) {
            case BuyOrSell.Buy: {
                try {
                    const res = await queryMyBargainApply(param)
                    total.value = res.total
                    dataList.value = res.data
                } finally {
                    loading.value = false
                }
                break
            }
            case BuyOrSell.Sell: {
                try {
                    const res_1 = await queryMyDelistingApply(param)
                    total.value = res_1.total
                    dataList.value = res_1.data
                } finally {
                    loading.value = false
                }
                break
            }
            default: {
                return Promise.reject('参数错误')
            }
        }
    }

    return {
        loading,
        dataList,
        total,
        pageIndex,
        pageSize,
        getBargainOrderList,
    }
}

/**
 * 询价消息-买方询价
 */
export function useBuyBargain() {
    const { dataList, total, pageIndex, pageSize } = useDataTable<Ermcp.MyBargainApplyRsp>()
    const loading = shallowRef(false)

    // 获取询价列表
    const getBargainOrderList = async (params: Partial<Ermcp.MyBargainApplyReq> = {}) => {
        try {
            loading.value = true
            const res = await queryMyBargainApply({
                type: 1,
                page: pageIndex.value,
                pagesize: pageSize.value,
                userid: userId.value,
                applystatus: params.applystatus,
            })
            total.value = res.total
            dataList.value = res.data
        } finally {
            loading.value = false
        }
    }

    return {
        loading,
        dataList,
        total,
        pageIndex,
        pageSize,
        getBargainOrderList,
    }
}

/**
 * 询价消息-卖方询价
 */
export function useSellBargain() {
    const { dataList, total, pageIndex, pageSize } = useDataTable<Ermcp.MyDelistingApplyRsp>()
    const loading = shallowRef(false)

    // 获取询价列表
    const getDelistingOrderList = async (params: Partial<Ermcp.MyDelistingApplyReq> = {}) => {
        try {
            loading.value = true
            const res = await queryMyDelistingApply({
                type: 1,
                page: pageIndex.value,
                pagesize: pageSize.value,
                userid: userId.value,
                applystatus: params.applystatus,
            })
            total.value = res.total
            dataList.value = res.data
        } finally {
            loading.value = false
        }
    }

    return {
        loading,
        dataList,
        total,
        pageIndex,
        pageSize,
        getDelistingOrderList,
    }
}

/**
 * 钻石询价操作
 * @returns 
 */
export function useBargainOperate(selectedRow: Ermcp.MyBargainApplyRsp | Ermcp.MyDelistingApplyRsp) {
    const loading = shallowRef(false)
    const formData = reactive<{ AuditRemark: string }>({
        AuditRemark: ''
    })

    const formSubmit = async (OperateType: 0 | 1 | 2 | 3) => {
        loading.value = true

        // 买询价操作
        if ('wrbargainid' in selectedRow) {
            try {
                return await zsBuyOrderDestingNegPriceOperate({
                    UserID: userId.value,
                    AccountID: firstAccountId.value,
                    WRBargainID: Long.fromString(selectedRow.wrbargainid),
                    OperateType,
                    AuditRemark: formData.AuditRemark,
                    MarketID: Market.GZ,
                    ClientType: ClientType.Web,
                    OrderSrc: 1,
                    ClientSerialNo: v4() // 客户端流水号
                })
            } finally {
                loading.value = false
            }
        }

        // 卖询价操作
        if ('selldelistingapplyid' in selectedRow) {
            try {
                return await zsSellOrderDestingApplyOperate({
                    UserID: userId.value,
                    AccountID: firstAccountId.value,
                    SellDelistingApplyID: Long.fromString(selectedRow.selldelistingapplyid),
                    OperateType,
                    AuditRemark: formData.AuditRemark,
                    MarketID: Market.GZ,
                    ClientType: ClientType.Web,
                    OrderSrc: 1,
                    ClientSerialNo: v4(), // 客户端流水号
                })
            } finally {
                loading.value = false
            }
        }

        loading.value = false
        return Promise.reject('参数错误')
    }

    return {
        loading,
        formData,
        formSubmit,
    }
}