import { shallowRef } from 'vue'
import { useDataTable } from '@/hooks/datatable'
import { queryWrOrderDetail, queryWrTradeDetail, queryTHJPurchaseTradeDetail, queryHoldLB, queryWrOutInApply } 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 getWrOrderDetail = async (haswr: number, marketid?: number, bcancel?: number, wrtradetype?: number, begindate?: string, enddate?: string) => {
        loading.value = true
        await queryWrOrderDetail({
            data: {
                userid: getUserId(),
                haswr: haswr,
                bcancel: bcancel,
                marketid: marketid,
                wrtradetype: wrtradetype,
                begindate: begindate,
                enddate: enddate,
                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,
        getWrOrderDetail,
    }
}

// 查询仓单成交明细
export function useQueryWrTradeDetail() {
    const { dataList, total, pageIndex, pageSize, pageCount } = useDataTable<Model.WrTradeDetailRsp>()
    const loading = shallowRef(false)
    const { getUserId } = useLoginStore()
    
    const getWrTradeDetail = async (haswr: number, marketid?: number, begindate?: string, enddate?: string) => {
        loading.value = true
        await queryWrTradeDetail({
            data: {
                userid: getUserId(),
                haswr: haswr,
                marketid: marketid,
                begindate: begindate,
                enddate: enddate,
                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,
        getWrTradeDetail,
    }
}

// 查询我的订单-采购订单
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,
    }
}