import HocError from '@/Hoc/HocError'
import React, { useEffect, useMemo, useState } from 'react'
import { useAccess, useModel } from 'umi'
import TableData from '../../components/TableData'
import  fcolumns  from './tableConfig'
/**广告投放详细数据 */
function Page() {
    const { state: { dataSource, pitcher }, dispatch, getBookRank, getUserAjax } = useModel('useData.useTableData')
    const access = useAccess()
    const { currentUser }: any = useModel('@@initialState', model => ({ currentUser: model.initialState?.currentUser }))
    const [pitcherId, setPitcherId] = useState<number>(0)

    useEffect(() => {
        if (pitcher) {
            let pitcherData = getUserAjax?.data?.data?.find((item: { nick_name: string }) => item.nick_name === pitcher)
            setPitcherId(pitcherData?.user_id || 0)
        }
    }, [pitcher])

    //加工数据
    const data = useMemo(() => {
        if (dataSource) {
            return dataSource?.map((item: any, index: number) => {
                item['id'] = index
                return item
            })
        }
        return []
    }, [dataSource])
    //获取排序列表
    const sortArr = useMemo(() => {
        let arr: any[] = []
        fcolumns()().map((item: any) => {
            if (typeof item.title === 'string') {
                arr.push({ title: item.title, value: item.dataIndex, key: item.dataIndex })
            }
            if (typeof item.title === 'object' && typeof item.title.props.children[0] === 'string') {
                arr.push({ title: item.title, value: item.dataIndex, key: item.dataIndex })
            }
        })
        return arr
    }, [fcolumns])
    return <>
        <TableData
            isBook
            isTableDateArr={access['是否组长显示所有时间']}
            isTableDateOne={!access['是否组长显示所有时间']}
            isQs
            isType
            isGroupTab={false}
            sortArr={sortArr}
            columns={fcolumns(dispatch, pitcherId, currentUser.userId)}
            ajax={getBookRank}
            title='今日消耗排行榜'
            dataSource={data}
            scroll={{ x: 1000, y: 600 }}
            className='ranking'
        />
    </>
}

export default HocError(Page)