import { shallowRef, ref } from 'vue'
import { useDataTable } from '@/hooks/datatable'
import { queryBuyOrder, querySellOrder, queryMyBuyOrder, queryMySellOrder, queryMyDeListing } from '@/services/api/trade'
import { useLoginStore } from '@/stores'
import { getBuyOrSellList, BuyOrSell } from '@/constants/order'
import { Category } from '@/constants/diamond'

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

const categoryList = [
    { label: '单颗裸钻', value: Category.Diamond },
    { label: '单颗彩钻', value: Category.Fancy }
]

/**
 * 求购大厅
 * @returns 
 */
export function useBuyOrder() {
    const { dataList, total, pageIndex, pageSize } = useDataTable<Ermcp.BuyOrderRsp>()
    const loading = shallowRef(false)

    // 获取求购大厅委托单
    const getBuyOrderList = async (params: Partial<Ermcp.BuyOrderReq> = {}) => {
        try {
            loading.value = true
            const res = await queryBuyOrder({
                page: pageIndex.value,
                pagesize: pageSize.value,
                zscategorys: params.zscategorys ?? categoryList.map((e) => e.value).join(','),
                wrtradeorderid: params.wrtradeorderid,
                zsallproperties: params.zsallproperties,
                buyusername: params.buyusername,
            })
            total.value = res.total
            dataList.value = res.data
        } finally {
            loading.value = false
        }
    }

    return {
        loading,
        dataList,
        total,
        pageIndex,
        pageSize,
        categoryList,
        getBuyOrderList,
    }
}

/**
 * 出售大厅
 * @returns 
 */
export function useSellOrder() {
    const { dataList, total, pageIndex, pageSize } = useDataTable<Ermcp.SellOrderRsp>()
    const loading = shallowRef(false)

    // 获取出售大厅委托单
    const getSellOrderList = async (params: Partial<Ermcp.SellOrderReq> = {}) => {
        try {
            loading.value = true
            const res = await querySellOrder({
                page: pageIndex.value,
                pagesize: pageSize.value,
                zscategorys: params.zscategorys ?? categoryList.map((e) => e.value).join(','),
                wrtradeorderid: params.wrtradeorderid,
                zsallproperties: params.zsallproperties,
                buyusername: params.buyusername,
            })
            total.value = res.total
            dataList.value = res.data
        } finally {
            loading.value = false
        }
    }

    return {
        loading,
        dataList,
        total,
        pageIndex,
        pageSize,
        categoryList,
        getSellOrderList,
    }
}

/**
 * 我的出售
 */
export function useSaleOrder() {
    const { dataList, total, pageIndex, pageSize } = useDataTable<Ermcp.MySellOrderRsp>()
    const loading = shallowRef(false)

    const filterData = ref<{ history: boolean, date: string[] }>({
        history: false,
        date: []
    })

    // 获取我的出售列表
    const getSaleOrderList = async (params: Partial<Ermcp.MySellOrderReq> = {}) => {
        try {
            loading.value = true
            const { history: ishis, date } = filterData.value
            const [begindate, enddate] = ishis && Array.isArray(date) ? date : []
            const res = await queryMySellOrder({
                page: pageIndex.value,
                pagesize: pageSize.value,
                userid: userId.value,
                zscategorys: params.zscategorys ?? categoryList.map((e) => e.value).join(','),
                wrtradeorderid: params.wrtradeorderid,
                zsallproperties: params.zsallproperties,
                ishis,
                begindate,
                enddate,
            })
            total.value = res.total
            dataList.value = res.data
        } finally {
            loading.value = false
        }
    }

    return {
        loading,
        dataList,
        total,
        pageIndex,
        pageSize,
        filterData,
        categoryList,
        getSaleOrderList,
    }
}

/**
 * 我的求购
 */
export function usePurchaseOrder() {
    const { dataList, total, pageIndex, pageSize } = useDataTable<Ermcp.MyBuyOrderRsp>()
    const loading = shallowRef(false)

    const filterData = ref<{ history: boolean, date: string[] }>({
        history: false,
        date: []
    })

    // 获取我的求购列表
    const getPurchaseOrderList = async (params: Partial<Ermcp.MyBuyOrderReq> = {}) => {
        try {
            loading.value = true
            const { history: ishis, date } = filterData.value
            const [begindate, enddate] = ishis && Array.isArray(date) ? date : []
            const res = await queryMyBuyOrder({
                page: pageIndex.value,
                pagesize: pageSize.value,
                userid: userId.value,
                zscategorys: params.zscategorys ?? categoryList.map((e) => e.value).join(','),
                wrtradeorderid: params.wrtradeorderid,
                zsallproperties: params.zsallproperties,
                ishis,
                begindate,
                enddate,
            })
            total.value = res.total
            dataList.value = res.data
        } finally {
            loading.value = false
        }
    }

    return {
        loading,
        dataList,
        total,
        pageIndex,
        pageSize,
        filterData,
        categoryList,
        getPurchaseOrderList,
    }
}

/**
 * 我的摘牌
 */
export function useDelistingOrder(buyorsell = BuyOrSell.Buy) {
    const { dataList, total, pageIndex, pageSize } = useDataTable<Ermcp.MyDeListingRsp>()
    const loading = shallowRef(false)
    const filterDate = shallowRef<string[]>([])
    const buyOrSell = shallowRef(buyorsell) // 根据查询的方向固定显示

    // 获取我的摘牌列表
    const getDelistingOrderList = async (params: Partial<Ermcp.MyDeListingReq> = {}) => {
        try {
            loading.value = true
            const [begindate, enddate] = filterDate.value || []
            const res = await queryMyDeListing({
                page: pageIndex.value,
                pagesize: pageSize.value,
                userid: userId.value,
                buyorsell: params.buyorsell,
                matchusername: params.matchusername,
                zscategorys: params.zscategorys ?? categoryList.map((e) => e.value).join(','),
                zsallproperties: params.zsallproperties,
                begindate,
                enddate,
            })
            total.value = res.total
            buyOrSell.value = Number(params.buyorsell ?? -1)
            dataList.value = res.data
        } finally {
            loading.value = false
        }
    }

    return {
        loading,
        dataList,
        total,
        pageIndex,
        pageSize,
        buyOrSell,
        filterDate,
        categoryList,
        buyOrSellList: getBuyOrSellList(),
        getDelistingOrderList,
    }
}