import { shallowRef, reactive } from 'vue'
import { useDataTable } from '@/hooks/datatable'
import { EchartsDataset } from '@/hooks/echarts/line/interface'
import { queryTHJWrstandard, queryTHJWrstandardDetail, addUserFavoriteGoods, removeUserFavoriteGoods, queryTHJProduct, queryMarketRun, queryTHJTradeData, querySpotGoodsPrice } 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: active.value === 0,
                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 useQuerySpotGoodsPriceLists() {
    const { dataList, total, pageIndex, pageSize, pageCount } = useDataTable<Model.SpotGoodsPriceRsp>()
    const loading = shallowRef(false)

    const getQuerySpotGoodsPriceLists = () => {
        loading.value = true
        return querySpotGoodsPrice({
            data: {
                page: pageIndex.value,
                pagesize: pageSize.value,
            },
            success: (res) => {
                total.value = res.total
                dataList.value = res.data
            },
            complete: () => {
                loading.value = false
            }
        })
    }

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

// 查询市场运行信息
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,
    }
}
