import { shallowRef, reactive } from 'vue'
import { useDataTable } from '@/hooks/datatable'
import { EchartsDataset } from '@/hooks/echarts/line/interface'
import { BuyOrSell } from '@/constants/order'
import { queryTHJinvesotrdeposit } from '@/services/api/common'
import { queryOrderQuoteDetail, queryWrMarketTradeConfig, queryTHJWrstandardDetail, addUserFavoriteGoods, removeUserFavoriteGoods, queryMarketRun } from '@/services/api/goods'
import { loginStore } from '@/stores'

const { userId } = loginStore.$mapGetters()

export type OptionalType<T, K extends keyof T> = Omit<T, K> & { [key in K]: Partial<T[K]> } // 指定某个属性为可选

// 采购详细
export function useWrstandardDetails(wrstandardid: number) {
    const loading = shallowRef(false)
    const details = shallowRef<OptionalType<Model.THJWrstandardDetailRsp, 'goodsinfo'>>({
        deliverymodes: [],
        deliverymonth: [],
        goodsinfo: {},
        presaleapplydeposits: [],
        spotgoodspricelogs: []
    })
    const customDeposits = shallowRef<Model.THJinvesotrdepositRsp[]>([]) // 个性化定金比例

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

    // 获取采购详情
    const getWrstandardDetails = async () => {
        loading.value = true
        return queryTHJWrstandardDetail({ wrstandardid }).then((res) => {
            details.value = res.data
            res.data.spotgoodspricelogs.forEach(({ spotgoodsprice, tradedate }) => {
                chartData.price.push(spotgoodsprice)
                chartData.date.push(tradedate)
            })
        }).finally(() => {
            loading.value = false
        })
    }

    // 获取个性化定金比例
    const getTHJinvesotrdeposit = async (presaleapplyid: string) => {
        const res = await queryTHJinvesotrdeposit({
            userid: userId.value,
            presaleapplyid
        })
        customDeposits.value = res.data
    }

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

// 用户商品收藏信息
export function useUserFavoriteGoods(goodsid: number) {
    // 添加收藏
    const getAddUserFavoriteGoods = () => {
        return addUserFavoriteGoods({
            userID: userId.value,
            goodsID: goodsid
        })
    }

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

    return {
        getAddUserFavoriteGoods,
        getRemoveUserFavoriteGoods,
    }
}

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

    const getQueryMarketRunLists = async (marketid?: number) => {
        try {
            loading.value = true
            const res = await queryMarketRun({
                marketID: marketid
            })
            total.value = res.total
            dataList.value = res.data
        } finally {
            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 = async (marketid?: number) => {
        try {
            loading.value = true
            const res = await queryWrMarketTradeConfig({
                marketid: marketid
            })
            total.value = res.total
            dataList.value = res.data
        } finally {
            loading.value = false
        }
    }

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

// 供求详情
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({
                wrpricetype: 1,
                haswr: 1,
                wrfactortypeid,
                buyorsell,
            }).then((res) => {
                total.value = res.total
                dataList.value = res.data
            }).finally(() => {
                loading.value = false
            })
        }
        return dataList.value
    }

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