import { shallowRef } from 'vue'
import { useDataTable } from '@/hooks/datatable'
import { querySiteColumnDetail, querySiteColumnConfig } from '@/services/api/news'

// 新闻资讯列表
export function useSiteColumnDetail() {
    const { dataList, total, pageIndex, pageSize, pageCount } = useDataTable<Model.SiteColumnDetailRsp>()
    const loading = shallowRef(false)
    const siteColumns = shallowRef<Model.SiteColumnConfigRsp[]>([])
    const currentColumnId = shallowRef(0)

    /// 查询栏目配置
    const getSiteColumnConfig = querySiteColumnConfig({
        success: (res) => {
            siteColumns.value = res.data
            /// 默认第一个
            if (res.data.length) {
                currentColumnId.value = res.data[0].id
            }
        }
    })

    const getSiteColumnDetail = async () => {
        loading.value = true
        await getSiteColumnConfig
        await querySiteColumnDetail({
            data: {
                columnid: currentColumnId.value,
                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,
        siteColumns,
        currentColumnId,
        total,
        pageIndex,
        pageSize,
        pageCount,
        getSiteColumnDetail,
    }
}

// 查询资讯栏目配置
export function useSiteColumnConfig() {

    const { dataList, total, pageIndex, pageSize, pageCount } = useDataTable<Model.SiteColumnConfigRsp>()
    const loading = shallowRef(false)

    const getSiteColumnConfig = (columntype?: number) => {
        loading.value = true
        return querySiteColumnConfig({
            data: {
                columntype: columntype,
                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,
        getSiteColumnConfig,
    }
}