import { ref, shallowRef } from 'vue'
import { v4 } from 'uuid'
import { ClientType, OrderSrc } from '@/constants/client'
import { useLoginStore } from '@/stores'
import { spotPresaleDestingOrder, spotPresaleTransferCancel, spotPresaleTransferDesting, spotPresaleTransferListing, 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 { route } = useNavigation()
    const item = route.params.item
    const detail = shallowRef<Model.THJPurchaseTradeDetailRsp>()

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

    const confirmSubmit = () => {
        const param = detail.value

        if (param) {
            loading.value = true
            const data: Partial<Proto.SpotPresaleDeliveryConfirmReq>={
                UserID: getUserId(),
                Remark: '',
                WRTradeDetailID: Long.fromNumber(param.wrtradedetailid),
                ClientSerialNo: v4(), // 客户端流水号
                ClientType: ClientType.Web // 终端类型
            }
            return spotPresaleDeliveryConfirm({
                data,
                complete: () => {
                    loading.value = false
                }
            })
        }
        return Promise.reject('预售交收申请失败')
    }

    return {
        loading,
        confirmSubmit,
        detail
    }
}

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

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

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

    const applySubmit = () => {
        const param = detail.value

        if (param) {
            loading.value = true
            const data: Partial<Proto.SpotPresaleBreachOfContractApplyReq>={
                UserID: getUserId(),
                WRTradeDetailID: Long.fromNumber(param.wrtradedetailid),
                ClientSerialNo: v4(), // 客户端流水号
                ClientType: ClientType.Web // 终端类型
            }
            return spotPresaleBreachOfContractApply({
                data,
                complete: () => {
                    loading.value = false
                }
            })
        }
        return Promise.reject('违约申请失败')
    }

    return {
        loading,
        applySubmit,
        detail
    }
}

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

    const formData = ref<Partial<Proto.SpotPresalePlaymentReq>>({
        UserID: getUserId(), // 用户ID，必填
        ClientType: ClientType.Web, // 终端类型
        ClientSerialNo: v4(), // 客户端流水号
    })

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

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

    const playmentSubmit = () => {
        const param = detail.value
        
        if (param) {
            loading.value = true
            const data: Partial<Proto.SpotPresaleTransferListingReq>={
                ...formData,
                WRTradeDetailID: Long.fromNumber(param.wrtradedetailid),
            }
            return spotPresalePlayment({
                data,
                complete: () => {
                    loading.value = false
                }
            })
        }
        return Promise.reject('付款失败')
    }

    return {
        loading,
        playmentSubmit,
        detail
    }
}

// 铁合金现货预售转让挂牌接口
export function useSpotPresaleTransferListing() {
    const loading = shallowRef(false)
    const { getUserId } = useLoginStore()

    const formData = ref<Partial<Proto.SpotPresaleTransferListingReq>>({
        UserID: getUserId(), // 用户ID，必填
        ClientType: ClientType.Web, // 终端类型
        ClientSerialNo: v4(), // 客户端流水号
    })

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

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

    const listingSubmit = () => {
        const param = detail.value
        if (param) {
            loading.value = true
            const data:Partial<Proto.SpotPresaleTransferListingReq>={
                ...formData,
                WRTradeDetailID: Long.fromNumber(param.wrtradedetailid),
            }

            return spotPresaleTransferListing({
                data,
                complete: () => {
                    loading.value = false
                }
            })
        }
        return Promise.reject('预售转让失败')
    }

    return {
        loading,
        formData,
        listingSubmit,
        detail
    }
}

// 铁合金现货预售转让撤销接口请求
export function useSpotPresaleTransferCancel() {
    const loading = shallowRef(false)
    const { getUserId } = useLoginStore()

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

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

    const transferCancelSubmit = () => {
        const param = detail.value
        if (param) {
            loading.value = true
            const data: Partial<Proto.SpotPresaleTransferCancelReq>={
                UserID: getUserId(),
                WRTradeDetailID: Long.fromNumber(param.wrtradedetailid),
                ClientSerialNo: v4(), // 客户端流水号
                ClientType: ClientType.Web // 终端类型
            }

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

    return {
        loading,
        transferCancelSubmit,
        detail
    }
}

// 铁合金现货预售转让摘牌接口请求
export function useSpotPresaleTransferDesting() {
    const loading = shallowRef(false)
    const { getUserId , getFirstAccountId} = useLoginStore()

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

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

    const destingSubmit = () => {
        const param = detail.value
        if (param) {
            loading.value = true
            const data: Partial<Proto.SpotPresaleTransferDestingReq>={
                UserID: getUserId(),
                AccountID: getFirstAccountId(),
                TransferID: Long.fromNumber(param.wrtradedetailid),
                ClientSerialNo: v4(), // 客户端流水号
                ClientType: ClientType.Web // 终端类型
            }

            return spotPresaleTransferDesting({
                data,
                complete: () => {
                    loading.value = false
                }
            })
        }
        
        return Promise.reject('预售转让失败')
    }

    return {
        loading,
        destingSubmit,
        detail
    }
}

// 挂牌撤单请求接口
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,
    }
}