import { ref, shallowRef } from 'vue'
import { v4 } from 'uuid'
import { ClientType, OrderSrc } from '@/constants/client'
import { useLoginStore } from '@/stores'
import { spotPresaleDestingOrder, spotPresalePlayment, wrListingCancelOrder, spotPresaleDeliveryConfirm, spotPresaleBreachOfContractConfirm, spotPresaleBreachOfContractApply } from '@/services/api/trade'
import { formatDate } from "@/filters";
import { useNavigation } from '@/hooks/navigation'
import Long from 'long'

// 采购摘牌
export function usePurchaseOrderDesting() {
    const { getUserId, getFirstAccountId } = useLoginStore()
    const loading = shallowRef(false)

    const formData = ref<Partial<Proto.SpotPresaleDestingOrderReq>>({
        UserID: getUserId(), // 用户ID，必填
        AccountID: getFirstAccountId(), // 资金账号，必填
        ClientType: ClientType.Web // 终端类型
    })

    const formSubmit = () => {
        loading.value = true
        return spotPresaleDestingOrder({
            data: {
                ...formData.value,
                Qty: Number(formData.value.Qty),
                ClientSerialNo: v4() // 客户端流水号
            },
            complete: () => {
                loading.value = false
            }
        })
    }

    return {
        loading,
        formData,
        formSubmit,
    }
}

// 铁合金现货预售交收确认
export function useSpotPresaleDeliveryConfirm() {
    const { getUserId } = useLoginStore()
    const loading = shallowRef(false)

    const confirmSubmit = (id: number) => {
        loading.value = true
        return spotPresaleDeliveryConfirm({
            data: {
                UserID: getUserId(),
                Remark: '',
                ClientType: ClientType.Web, // 终端类型
                WRTradeDetailID: id,
                ClientSerialNo: v4() // 客户端流水号
            },
            complete: () => {
                loading.value = false
            }
        })
    }

    return {
        loading,
        confirmSubmit,
    }
}

// 铁合金现货预售违约确认
export function useSpotPresaleBreachOfContractConfirm() {
    const loading = shallowRef(false)

    const breachSubmit = (buyAmount: number, exchangeAmount: number, id: number ) => {
        loading.value = true
        return spotPresaleBreachOfContractConfirm({
            data: {
                HandleRemark: '',
                WRTradeDetailID: id,
                BuyAmount: buyAmount,
                ExchangeAmount: exchangeAmount,
                ClientSerialNo: v4(), // 客户端流水号
                ClientType: ClientType.Web // 终端类型
            },
            complete: () => {
                loading.value = false
            }
        })
    }

    return {
        loading,
        breachSubmit,
    }
}

// 铁合金现货预售违约确认
export function useSpotPresaleBreachOfContractApply() {
    const loading = shallowRef(false)
    const { getUserId } = useLoginStore()

    const applySubmit = (id: number ) => {
        loading.value = true
        return spotPresaleBreachOfContractApply({
            data: {
                UserID: getUserId(),
                WRTradeDetailID: id,
                ClientSerialNo: v4(), // 客户端流水号
                ClientType: ClientType.Web // 终端类型
            },
            complete: () => {
                loading.value = false
            }
        })
    }

    return {
        loading,
        applySubmit,
    }
}

// 铁合金现货预售付款处理接口
export function useSpotPresalePlayment() {
    const loading = shallowRef(false)
    const { getUserId } = useLoginStore()

    const playmentSubmit = (id: number ) => {
        loading.value = true
        return spotPresalePlayment({
            data: {
                UserID: getUserId(),
                WRTradeDetailID: id,
                ClientSerialNo: v4(), // 客户端流水号
                ClientType: ClientType.Web // 终端类型
            },
            complete: () => {
                loading.value = false
            }
        })
    }

    return {
        loading,
        playmentSubmit,
    }
}

// 挂牌撤单请求接口
export function useWrListingCancelOrder() {
    const loading = shallowRef(false)
    const { getUserId, getFirstAccountId, getLoginId } = useLoginStore()

    const { route } = useNavigation()
    const item = route.params.item
    const detail = shallowRef<Model.WrOrderDetailRsp>()

    if (item) {
        detail.value = JSON.parse(item.toString())
    }

    const cancelSubmit = () => {
        const param = detail.value
        if (param) {
            loading.value = true
            const data:Partial<Proto.WRListingCancelOrderReq>={
                UserID: getUserId(),
                AccountID: getFirstAccountId(),
                OperatorID: getLoginId(),
                OrderSrc: OrderSrc.ORDERSRC_CLIENT,
                OldWRTradeOrderID: Long.fromString(param.wrtradeorderid),
                ClientOrderTime: formatDate(new Date().toString(), 'YYYY-MM-DD HH:mm:ss'),
                ClientSerialNo: v4(), // 客户端流水号
                ClientType: ClientType.Web, // 终端类型
                BuyOrSell: param.buyorsell,
            }

            return wrListingCancelOrder({
                data,
                complete: () => {
                    loading.value = false
                }
            })
        }
        return Promise.reject('撤销失败')
    }

    return {
        loading,
        cancelSubmit,
        detail,
    }
}