import { shallowRef, reactive } from 'vue'
import { useDataTable } from '@/hooks/datatable'
import { EchartsDataset } from '@/hooks/echarts/line/interface'
import { BuyOrSell } from '@/constants/order'
import { queryTHJWrstandard, queryOrderQuote, queryOrderQuoteDetail, queryWrMarketTradeConfig, queryTHJWrstandardDetail, addUserFavoriteGoods, removeUserFavoriteGoods, queryTHJProduct, queryMarketRun, queryTHJTradeData } from '@/services/api/goods'
import { useLoginStore } from '@/stores'

// 采购列表
export function useWrstandardList() {
    const { dataList, total, pageIndex, pageSize, pageCount } = useDataTable<Model.THJWrstandardRsp>()
    const loading = shallowRef(false)

    const getWrstandardList = async () => {
        loading.value = true
        await queryTHJWrstandard({
            data: {
                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,
        getWrstandardList,
    }
}

// 采购详细
export function useWrstandardDetails(wrstandardid: number) {
    const loading = shallowRef(false)
    const details = shallowRef<Partial<Model.THJWrstandardDetailRsp>>({})

    // 图表数据
    const chartData = reactive<EchartsDataset['line']['source']>({
        price: [],
        date: []
    })

    const getWrstandardDetails = () => {
        loading.value = true
        return queryTHJWrstandardDetail({
            data: {
                wrstandardid,
            },
            success: (res) => {
                details.value = res.data

                res.data.spotgoodspricelogs.forEach(({ spotgoodsprice, tradedate }) => {
                    chartData.price.push(spotgoodsprice)
                    chartData.date.push(tradedate)
                })
            },
            complete: () => {
                loading.value = false
            }
        })
    }

    return {
        loading,
        details,
        chartData,
        getWrstandardDetails,
    }
}


// 用户商品收藏信息
export function useUserFavoriteGoods(goodsid: number) {
    const { getUserId } = useLoginStore()

    // 添加收藏
    const getAddUserFavoriteGoods = () => {
        return addUserFavoriteGoods({
            data: {
                userID: getUserId(),
                goodsID: goodsid
            },
        })
    }

    // 移除收藏
    const getRemoveUserFavoriteGoods = () => {
        return removeUserFavoriteGoods({
            data: {
                userID: getUserId(),
                goodsID: goodsid
            },
        })
    }

    return {
        getAddUserFavoriteGoods,
        getRemoveUserFavoriteGoods,
    }
}

// 获取我的推广-交易数据
export function useQueryTHJTradeDataList() {
    const { dataList, total, pageIndex, pageSize, pageCount } = useDataTable<Model.THJTradeDataRsp>()
    const loading = shallowRef(false)
    const { getUserId } = useLoginStore()

    const getQueryTHJTradeDataList = async (marketid: number) => {
        loading.value = true
        await queryTHJTradeData({
            data: {
                userid: getUserId(),
                marketid: marketid,
                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,
        getQueryTHJTradeDataList,
    }
}

// 获取产品介绍列表
export function useQueryTHJProductLists() {
    const { dataList, total, pageIndex, pageSize, pageCount } = useDataTable<Model.THJProductRsp>()
    const loading = shallowRef(false)
    const { getUserId } = useLoginStore()
    const active = shallowRef(0)

    const getQueryTHJProductLists = async () => {
        loading.value = true
        await queryTHJProduct({
            data: {
                userid: getUserId(),
                favoriteflag: false,
                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,
        active,
        total,
        pageIndex,
        pageSize,
        pageCount,
        getQueryTHJProductLists,
    }
}



// 查询市场运行信息
export function useQueryMarketRunLists() {
    const { dataList, total, pageIndex, pageSize, pageCount } = useDataTable<Model.MarketRunRsp>()
    const loading = shallowRef(false)

    const getQueryMarketRunLists = (marketid?: number) => {
        loading.value = true
        return queryMarketRun({
            data: {
                marketID: marketid
            },
            success: (res) => {
                total.value = res.total
                dataList.value = res.data
            },
            complete: () => {
                loading.value = false
            }
        })
    }

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

// 查询市场运行信息
export function useQueryWrMarketTradeConfig() {
    const { dataList, total, pageIndex, pageSize, pageCount } = useDataTable<Model.WrMarketTradeConfigRsp>()
    const loading = shallowRef(false)

    const wrMarketTradeConfig = (marketid?: number) => {
        loading.value = true
        return queryWrMarketTradeConfig({
            data: {
                marketid: marketid
            },
            success: (res) => {
                total.value = res.total
                dataList.value = res.data
            },
            complete: () => {
                loading.value = false
            }
        })
    }

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

// 供求列表
export function useOrderQuoteList() {
    const { dataList, total, pageIndex, pageSize, pageCount } = useDataTable<Model.OrderQuoteRsp>()
    const loading = shallowRef(false)

    const getOrderQuoteList = async () => {
        loading.value = true
        await queryOrderQuote({
            data: {
                page: pageIndex.value,
                pagesize: pageSize.value,
                wrpricetype: 1,
            },
            success: (res) => {
                total.value = res.total
                dataList.value = res.data
            },
            complete: () => {
                loading.value = false
            }
        })
        return dataList.value
    }

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

// 供求详情
export function useOrderQuoteDetail(wrfactortypeid?: string) {
    const { dataList, total, pageIndex, pageSize, pageCount } = useDataTable<Model.OrderQuoteDetailRsp>()
    const loading = shallowRef(false)

    const getOrderQuoteList = async (buyorsell: BuyOrSell) => {
        if (wrfactortypeid) {
            loading.value = true
            await queryOrderQuoteDetail({
                data: {
                    wrpricetype: 1,
                    haswr: 1,
                    wrfactortypeid,
                    buyorsell,
                },
                success: (res) => {
                    total.value = res.total
                    dataList.value = res.data
                },
                complete: () => {
                    loading.value = false
                }
            })
        }
        return dataList.value
    }

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