import { shallowRef, reactive } from 'vue'
import { formatDate } from '@/filters'
import { useDataTable } from '@/hooks/datatable'
import { EchartsDataset } from '@/hooks/echarts/line/interface'
import { queryTHJPurchaseTransfer, queryTHJPurchaseTransferDetail } from '@/services/api/contract'

// 合同转让列表
export function useContractList() {
    const { dataList, total, pageIndex, pageSize, pageCount } = useDataTable<Model.THJPurchaseTransferRsp>()
    const loading = shallowRef(false)

    const getWrstandardList = async () => {
        loading.value = true
        await queryTHJPurchaseTransfer({
            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 useContractDetails(wrstandardid: number) {
    const loading = shallowRef(false)
    const details = shallowRef<Partial<Model.THJPurchaseTransferDetailRsp>>({})

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

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

                res.data.history.forEach(({ c, ts }) => {
                    chartData.price.push(c)
                    chartData.date.push(formatDate(ts, 'YYYY-MM-DD'))
                })
            },
            complete: () => {
                loading.value = false
            }
        })
    }

    return {
        loading,
        details,
        chartData,
        getContractDetails,
    }
}