import { shallowRef } from 'vue'
import { useDataTable } from '@/hooks/datatable'
import { getTableColumns } from '@/business/table'
import { queryMarketRun } from '@/services/api/common'
import { queryMemberReport, queryWRPositionReport, queryTradeReport } from '@/services/api/report'
import { useLoginStore } from '@/stores'
import { Market } from '@/constants/market'

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

// 会员报表列表
export function useMemberReportList() {
    const { dataList, total, pageIndex, pageSize } = useDataTable<Ermcp.MemberReportRsp>()
    const loading = shallowRef(false)
    const columns = shallowRef(getTableColumns('report_member'))
    const marketInfo = shallowRef<Ermcp.MarketRunRsp>() // 市场信息
    const cycleType = shallowRef(5)
    const dateValue = shallowRef<string[]>([])

    const getMarketInfo = queryMarketRun({
        marketID: Market.GZ
    }).then((res) => {
        if (res.data.length) {
            marketInfo.value = res.data[0]
        }
    })

    const getMemberReportList = async () => {
        loading.value = true
        await getMarketInfo.then(() => {
            if (dateValue.value.length === 0) {
                const defaultDate = new Date().toISOString()
                const { pretradedate } = marketInfo.value ?? {}

                cycleType.value = 5
                dateValue.value = [defaultDate, defaultDate]
                if (pretradedate) {
                    dateValue.value = [pretradedate, pretradedate]
                }
            }
        })

        const [begindate, enddate] = dateValue.value
        return queryMemberReport({
            userid: userId.value,
            page: pageIndex.value,
            pagesize: pageSize.value,
            cycletype: cycleType.value === 5 ? undefined : cycleType.value.toString(),
            begindate: begindate,
            enddate: enddate,
        }).then((res) => {
            total.value = res.total
            dataList.value = res.data
        }).finally(() => {
            loading.value = false
        })
    }

    return {
        loading,
        dataList,
        total,
        pageIndex,
        pageSize,
        columns,
        cycleType,
        dateValue,
        getMemberReportList,
    }
}

// 仓储报表列表
export function useWarehousingReportList() {
    const { dataList, total, pageIndex, pageSize } = useDataTable<Ermcp.WRPositionReportRsp>()
    const loading = shallowRef(false)
    const columns = shallowRef(getTableColumns('report_warehousing'))
    const marketInfo = shallowRef<Ermcp.MarketRunRsp>() // 市场信息
    const cycleType = shallowRef(5)
    const dateValue = shallowRef<string[]>([])

    const getMarketInfo = queryMarketRun({
        marketID: Market.GZ
    }).then((res) => {
        if (res.data.length) {
            marketInfo.value = res.data[0]
        }
    })

    const getWarehousingReportList = async (params: Partial<Ermcp.WRPositionReportReq> = {}) => {
        loading.value = true
        await getMarketInfo.then(() => {
            if (dateValue.value.length === 0) {
                const defaultDate = new Date().toISOString()
                const { pretradedate } = marketInfo.value ?? {}

                cycleType.value = 5
                dateValue.value = [defaultDate, defaultDate]
                if (pretradedate) {
                    dateValue.value = [pretradedate, pretradedate]
                }
            }
        })
        const [begindate, enddate] = dateValue.value

        return queryWRPositionReport({
            userid: userId.value,
            page: pageIndex.value,
            pagesize: pageSize.value,
            cycletype: cycleType.value === 5 ? undefined : cycleType.value.toString(),
            begindate: begindate,
            enddate: enddate,
            zscategory: params.zscategory,
            zscurrencytype: params.zscurrencytype,
        }).then((res) => {
            total.value = res.total
            dataList.value = res.data
        }).finally(() => {
            loading.value = false
        })
    }

    return {
        loading,
        dataList,
        total,
        pageIndex,
        pageSize,
        columns,
        cycleType,
        dateValue,
        getWarehousingReportList,
    }
}

// 交易报表列表
export function useTradeReportList() {
    const { dataList, total, pageIndex, pageSize } = useDataTable<Ermcp.TradeReportRsp>()
    const loading = shallowRef(false)
    const columns = shallowRef(getTableColumns('report_trade'))
    const marketInfo = shallowRef<Ermcp.MarketRunRsp>() // 市场信息
    const cycleType = shallowRef(5)
    const dateValue = shallowRef<string[]>([])

    const getMarketInfo = queryMarketRun({
        marketID: Market.GZ
    }).then((res) => {
        if (res.data.length) {
            marketInfo.value = res.data[0]
        }
    })

    const getTradeReportList = async (params: Partial<Ermcp.WRPositionReportReq> = {}) => {
        loading.value = true
        await getMarketInfo.then(() => {
            if (dateValue.value.length === 0) {
                const defaultDate = new Date().toISOString()
                const { pretradedate } = marketInfo.value ?? {}

                cycleType.value = 5
                dateValue.value = [defaultDate, defaultDate]
                if (pretradedate) {
                    dateValue.value = [pretradedate, pretradedate]
                }
            }
        })
        const [begindate, enddate] = dateValue.value

        return queryTradeReport({
            userid: userId.value,
            page: pageIndex.value,
            pagesize: pageSize.value,
            cycletype: cycleType.value === 5 ? undefined : cycleType.value.toString(),
            begindate: begindate,
            enddate: enddate,
            zscategory: params.zscategory,
            zscurrencytype: params.zscurrencytype,
        }).then((res) => {
            total.value = res.total
            dataList.value = res.data
        }).finally(() => {
            loading.value = false
        })
    }

    return {
        loading,
        dataList,
        total,
        pageIndex,
        pageSize,
        columns,
        cycleType,
        dateValue,
        getTradeReportList,
    }
}