import { shallowRef, ref } from 'vue'
import { useDataTable } from '@/hooks/datatable'
import { getTableColumns } from '@/business/table'
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 categoryList = [
    { label: '单颗裸钻', value: Category.Diamond },
    { label: '单颗彩钻', value: Category.Fancy }
]

/**
 * 求购大厅
 * @returns 
 */
export function useBuyOrder() {
    const { dataList, total, pageIndex, pageSize, inputList, selectList, buttonList, filterMethod, getQueryParam } = useDataTable<Ermcp.BuyOrderRsp>()
    const loading = shallowRef(false)
    const columns = shallowRef(getTableColumns('listing_buyorder'))

    selectList.value = [
        {
            label: '商品分类',
            key: 'zscategory',
            options: categoryList,
        },
    ]

    inputList.value = [
        { label: '单号', keys: ['wrtradeorderid'], type: 'number' },
        { label: '买方', keys: ['buyusername'] },
        { label: '商品', keys: ['zstabledisplay'] },
    ]

    filterMethod.onReset = () => {
        getBuyOrderList()
    }

    filterMethod.onSearch = () => {
        getBuyOrderList()
    }

    // 获取求购大厅委托单
    const getBuyOrderList = () => {
        const param = getQueryParam()
        loading.value = true
        return queryBuyOrder({
            data: {
                page: pageIndex.value,
                pagesize: pageSize.value,
                zscategorys: param.zscategory ? param.zscategory.toString() : categoryList.map((e) => e.value).join(','),
                wrtradeorderid: param.wrtradeorderid,
                zsallproperties: param.zstabledisplay,
                buyusername: param.buyusername,
            },
            success: (res) => {
                total.value = res.total
                dataList.value = res.data
            },
            complete: () => {
                loading.value = false
            }
        })
    }

    return {
        loading,
        dataList,
        total,
        pageIndex,
        pageSize,
        columns,
        inputList,
        selectList,
        buttonList,
        getBuyOrderList,
    }
}

/**
 * 出售大厅
 * @returns 
 */
export function useSellOrder() {
    const { dataList, total, pageIndex, pageSize, inputList, selectList, buttonList, filterMethod, getQueryParam } = useDataTable<Ermcp.SellOrderRsp>()
    const loading = shallowRef(false)
    const columns = shallowRef(getTableColumns('listing_sellorder'))

    selectList.value = [
        {
            label: '商品分类',
            key: 'zscategory',
            options: categoryList,
        },
    ]

    inputList.value = [
        { label: '单号', keys: ['wrtradeorderid'], type: 'number' },
        { label: '卖方', keys: ['sellusername'] },
        { label: '商品', keys: ['goodsno'] },
    ]

    filterMethod.onReset = () => {
        getSellOrderList()
    }

    filterMethod.onSearch = () => {
        getSellOrderList()
    }

    // 获取出售大厅委托单
    const getSellOrderList = async () => {
        const param = getQueryParam()
        loading.value = true
        return querySellOrder({
            data: {
                page: pageIndex.value,
                pagesize: pageSize.value,
                zscategorys: param.zscategory ? param.zscategory.toString() : categoryList.map((e) => e.value).join(','),
                wrtradeorderid: param.wrtradeorderid,
                zsallproperties: param.goodsno,
                buyusername: param.sellusername,
            },
            success: (res) => {
                total.value = res.total
                dataList.value = res.data
            },
            complete: () => {
                loading.value = false
            }
        })
    }

    return {
        loading,
        dataList,
        total,
        pageIndex,
        pageSize,
        columns,
        inputList,
        selectList,
        buttonList,
        getSellOrderList,
    }
}

/**
 * 我的出售
 */
export function useSaleOrder() {
    const { getUserId } = useLoginStore()
    const { dataList, total, pageIndex, pageSize, inputList, selectList, buttonList, filterMethod, getQueryParam } = useDataTable<Ermcp.MySellOrderRsp>()
    const loading = shallowRef(false)
    const columns = shallowRef(getTableColumns('listing_saleorder'))

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

    selectList.value = [
        {
            label: '商品分类',
            key: 'zscategory',
            options: categoryList,
        },
    ]

    inputList.value = [
        { label: '单号', keys: ['wrtradeorderid'], type: 'number' },
        { label: '商品', keys: ['goodsno'] },
    ]

    filterMethod.onReset = () => {
        filterData.value = {
            history: false,
            date: []
        }
        getSaleOrderList()
    }

    filterMethod.onSearch = () => {
        getSaleOrderList()
    }

    // 获取我的出售列表
    const getSaleOrderList = () => {
        const { history: ishis, date } = filterData.value
        const [begindate, enddate] = ishis && Array.isArray(date) ? date : []
        const param = getQueryParam()
        loading.value = true

        return queryMySellOrder({
            data: {
                page: pageIndex.value,
                pagesize: pageSize.value,
                userid: getUserId(),
                zscategorys: param.zscategory ? param.zscategory.toString() : categoryList.map((e) => e.value).join(','),
                wrtradeorderid: param.wrtradeorderid,
                zsallproperties: param.goodsno,
                ishis,
                begindate,
                enddate,
            },
            success: (res) => {
                total.value = res.total
                dataList.value = res.data
            },
            complete: () => {
                loading.value = false
            }
        })
    }

    return {
        loading,
        dataList,
        total,
        pageIndex,
        pageSize,
        columns,
        filterData,
        inputList,
        selectList,
        buttonList,
        getSaleOrderList,
    }
}

/**
 * 我的求购
 */
export function usePurchaseOrder() {
    const { getUserId } = useLoginStore()
    const { dataList, total, pageIndex, pageSize, inputList, selectList, buttonList, filterMethod, getQueryParam } = useDataTable<Ermcp.MyBuyOrderRsp>()
    const loading = shallowRef(false)
    const columns = shallowRef(getTableColumns('listing_purchaseorder'))

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

    selectList.value = [
        {
            label: '商品分类',
            key: 'zscategory',
            options: categoryList,
        },
    ]

    inputList.value = [
        { label: '单号', keys: ['wrtradeorderid'], type: 'number' },
        { label: '商品', keys: ['zstabledisplay'] },
    ]

    filterMethod.onReset = () => {
        filterData.value = {
            history: false,
            date: []
        }
        getPurchaseOrderList()
    }

    filterMethod.onSearch = () => {
        getPurchaseOrderList()
    }

    // 获取我的求购列表
    const getPurchaseOrderList = () => {
        const { history: ishis, date } = filterData.value
        const [begindate, enddate] = ishis && Array.isArray(date) ? date : []
        const param = getQueryParam()
        loading.value = true

        return queryMyBuyOrder({
            data: {
                page: pageIndex.value,
                pagesize: pageSize.value,
                userid: getUserId(),
                zscategorys: param.zscategory ? param.zscategory.toString() : categoryList.map((e) => e.value).join(','),
                wrtradeorderid: param.wrtradeorderid,
                zsallproperties: param.zstabledisplay,
                ishis,
                begindate,
                enddate,
            },
            success: (res) => {
                total.value = res.total
                dataList.value = res.data
            },
            complete: () => {
                loading.value = false
            }
        })
    }

    return {
        loading,
        dataList,
        total,
        pageIndex,
        pageSize,
        columns,
        filterData,
        inputList,
        selectList,
        buttonList,
        getPurchaseOrderList,
    }
}

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

    selectList.value = [
        {
            label: '商品分类',
            key: 'zscategory',
            selectedValue: Category.Diamond,
            noClear: true,
            options: categoryList,
        },
        {
            label: '方向',
            key: 'buyorsell',
            selectedValue: buyOrSell.value,
            noClear: true,
            options: getBuyOrSellList(),
        },
    ]

    inputList.value = [
        { label: '对手方', keys: ['matchusername'] },
        { label: '商品', keys: ['goodsno'] },
    ]

    filterMethod.onReset = () => {
        filterDate.value = []
        getDelistingOrderList()
    }

    filterMethod.onSearch = () => {
        getDelistingOrderList()
    }

    // 获取我的摘牌列表
    const getDelistingOrderList = () => {
        const [begindate, enddate] = filterDate.value || []
        const param = getQueryParam()
        loading.value = true

        return queryMyDeListing({
            data: {
                page: pageIndex.value,
                pagesize: pageSize.value,
                userid: getUserId(),
                buyorsell: param.buyorsell,
                matchusername: param.matchusername,
                zscategorys: param.zscategory ? param.zscategory.toString() : categoryList.map((e) => e.value).join(','),
                zsallproperties: param.goodsno,
                begindate,
                enddate,
            },
            success: (res) => {
                total.value = res.total
                buyOrSell.value = Number(selectList.value[1].selectedValue ?? -1)
                dataList.value = res.data
            },
            complete: () => {
                loading.value = false
            }
        })
    }

    return {
        loading,
        dataList,
        total,
        pageIndex,
        pageSize,
        buyOrSell,
        columns,
        filterDate,
        inputList,
        selectList,
        buttonList,
        getDelistingOrderList,
    }
}