import { reactive, shallowRef } from 'vue'
import { useDataTable } from '@/hooks/datatable'
import {
    queryWrOrderDetail,
    queryWrTradeDetail,
    queryTHJPurchaseTradeDetail,
    queryHoldLB, queryWrOutInApply,
    queryTHJPromotionIncome,
    queryTHJPromotionIncomeDetail,
    queryTHJPurchaseTransferOrder,
    queryTHJPurchaseTransferOrderDetail
} from '@/services/api/order'
import { useLoginStore } from '@/stores'

// 查询仓单持有记录
export function useQueryHoldLB() {
    const { dataList, total, pageIndex, pageSize, pageCount } = useDataTable<Model.HoldLBRsp>()
    const loading = shallowRef(false)
    const { getFirstAccountId } = useLoginStore()

    const getHoldLB = async (warehouseid?: number, deliverygoodsid?: number, wrstandardid?: number, wrfactortypeid?: number, dgitemname?: string) => {
        loading.value = true
        await queryHoldLB({
            data: {
                accountid: getFirstAccountId(),
                warehouseid: warehouseid,
                deliverygoodsid: deliverygoodsid,
                wrstandardid: wrstandardid,
                wrfactortypeid: wrfactortypeid,
                dgitemname: dgitemname,
                page: pageIndex.value,
                pagesize: pageSize.value,
            },
            success: (res) => {
                total.value = res.total
                dataList.value = res.data
            },
            complete: () => {
                loading.value = false
            }
        })
        return dataList.value
    }

    return {
        loading,
        dataList,
        total,
        pageIndex,
        pageSize,
        pageCount,
        getHoldLB,
    }
}

// 查询提货申请
export function useQueryWrOutInApply() {
    const { dataList, total, pageIndex, pageSize, pageCount } = useDataTable<Model.WrOutInApplyRsp>()
    const loading = shallowRef(false)
    const { getUserId } = useLoginStore()

    const getWrOutInApply = async (begindate?: string, enddate?: string) => {
        loading.value = true
        await queryWrOutInApply({
            data: {
                userid: getUserId(),
                begindate: begindate,
                enddate: enddate,
                page: 1,
                pagesize: pageSize.value,
            },
            success: (res) => {
                total.value = res.total
                dataList.value = res.data
            },
            complete: () => {
                loading.value = false
            }
        })
        return dataList.value
    }

    return {
        loading,
        dataList,
        total,
        pageIndex,
        pageSize,
        pageCount,
        getWrOutInApply,
    }
}

// 查询仓单委托单
export function useQueryWrOrderDetail() {
    const { dataList, total, pageIndex, pageSize, pageCount } = useDataTable<Model.WrOrderDetailRsp>()
    const loading = shallowRef(false)
    const { getUserId } = useLoginStore()
    const formData = reactive<Partial<Model.WrOrderDetailReq>>({
        userid: getUserId(),
        haswr: 1,
        page: pageIndex.value,
        pagesize: pageSize.value,
    })

    const getWrOrderDetail = async () => {
        loading.value = true
        await queryWrOrderDetail({
            data: formData,
            success: (res) => {
                total.value = res.total
                dataList.value = res.data
            },
            complete: () => {
                loading.value = false
            }
        })
        return dataList.value
    }

    return {
        loading,
        dataList,
        total,
        pageIndex,
        pageSize,
        pageCount,
        getWrOrderDetail,
        formData
    }
}

// 查询仓单成交明细
export function useQueryWrTradeDetail() {
    const { dataList, total, pageIndex, pageSize, pageCount } = useDataTable<Model.WrTradeDetailRsp>()
    const loading = shallowRef(false)
    const { getUserId } = useLoginStore()

    const formData = reactive<Partial<Model.WrTradeDetailReq>>({
        userid: getUserId(),
        haswr: 1,
        page: pageIndex.value,
        pagesize: pageSize.value,
    })

    const getWrTradeDetail = async () => {
        loading.value = true
        await queryWrTradeDetail({
            data: formData,
            success: (res) => {
                total.value = res.total
                dataList.value = res.data
            },
            complete: () => {
                loading.value = false
            }
        })
        return dataList.value
    }

    return {
        loading,
        dataList,
        total,
        pageIndex,
        pageSize,
        pageCount,
        getWrTradeDetail,
        formData
    }
}

// 查询我的订单-采购订单
export function useTHJPurchaseTradeDetail() {
    const { dataList, total, pageIndex, pageSize, pageCount } = useDataTable<Model.THJPurchaseTradeDetailRsp>()
    const loading = shallowRef(false)
    const { getUserId } = useLoginStore()

    const getTHJPurchaseTradeDetail = async (type?: number) => {
        loading.value = true
        await queryTHJPurchaseTradeDetail({
            data: {
                userid: getUserId(),
                type: type,
                page: pageIndex.value,
                pagesize: pageSize.value,
            },
            success: (res) => {
                total.value = res.total
                dataList.value = res.data
            },
            complete: () => {
                loading.value = false
            }
        })
        return dataList.value
    }

    return {
        loading,
        dataList,
        total,
        pageIndex,
        pageSize,
        pageCount,
        getTHJPurchaseTradeDetail,
    }
}

// 查询我的推广-推广收益
export function useQueryTHJPromotionIncome() {
    const { dataList, total, pageIndex, pageSize, pageCount } = useDataTable<Model.THJPromotionIncomeRsp>()
    const loading = shallowRef(false)
    const { getUserId } = useLoginStore()

    const getTHJPromotionIncome = async (status?: number) => {
        loading.value = true
        await queryTHJPromotionIncome({
            data: {
                userid: getUserId(),
                profitstatus: status,
                page: pageIndex.value,
                pagesize: pageSize.value,
            },
            success: (res) => {
                total.value = res.total
                dataList.value = res.data
            },
            complete: () => {
                loading.value = false
            }
        })
        return dataList.value
    }

    return {
        loading,
        dataList,
        total,
        pageIndex,
        pageSize,
        pageCount,
        getTHJPromotionIncome,
    }
}

// 查询我的推广-推广收益-明细
export function useQueryTHJPromotionIncomeDetail(month: string) {
    const { dataList, total, pageIndex, pageSize, pageCount } = useDataTable<Model.THJPromotionIncomeDetailRsp>()
    const loading = shallowRef(false)
    const { getUserId } = useLoginStore()

    const getTHJPromotionIncomeDetail = async () => {
        loading.value = true
        await queryTHJPromotionIncomeDetail({
            data: {
                userid: getUserId(),
                mouth: month,
                page: pageIndex.value,
                pagesize: pageSize.value,
            },
            success: (res) => {
                total.value = res.total
                dataList.value = res.data
            },
            complete: () => {
                loading.value = false
            }
        })
        return dataList.value
    }

    return {
        loading,
        dataList,
        total,
        pageIndex,
        pageSize,
        pageCount,
        getTHJPromotionIncomeDetail,
    }
}

// 查询我的订单-转让/受让订单
export function useQueryTHJPurchaseTransferOrder() {
    const { dataList, total, pageIndex, pageSize, pageCount } = useDataTable<Model.THJPurchaseTransferOrderRsp>()
    const loading = shallowRef(false)
    const { getUserId } = useLoginStore()

    const getTHJPurchaseTransferOrder = async (ordertype: number) => {
        loading.value = true
        await queryTHJPurchaseTransferOrder({
            data: {
                userid: getUserId(),
                page: pageIndex.value,
                pagesize: pageSize.value,
                ordertype: ordertype
            },
            success: (res) => {
                total.value = res.total
                dataList.value = res.data
            },
            complete: () => {
                loading.value = false
            }
        })
        return dataList.value
    }

    return {
        loading,
        dataList,
        total,
        pageIndex,
        pageSize,
        pageCount,
        getTHJPurchaseTransferOrder,
    }
}

// 查询我的订单-转让/受让订单详情
export function useQueryTTHJPurchaseTransferOrderDetail(transferid: string) {
    const details = shallowRef<Model.THJPurchaseTransferOrderDetailRsp>()
    const loading = shallowRef(false)

    const getTHJPurchaseTransferOrderDetail = () => {
        loading.value = true
        queryTHJPurchaseTransferOrderDetail({
            data: {
                transferid,
            },
            success: (res) => {
                if (res.data.length) {
                    details.value = res.data[0]
                }
            },
            complete: () => {
                loading.value = false
            }
        })
    }

    return {
        loading,
        details,
        getTHJPurchaseTransferOrderDetail,
    }
}