import HocError from '@/Hoc/HocError'
import React, { useEffect, useMemo } from 'react'
import { useModel } from 'umi'
import TableData from '../../components/TableData'
import { columns } from './tableConfig'
/**广告投放详细数据 */
function Page() {
    const { state: { dataSource }, getChannelOverview, dispatch } = useModel('useData.useTableData')
    

    useEffect(()=> {
        if(window.location.hash?.indexOf('?') !== -1 && window.location.hash?.indexOf('channel') !== -1) {
            let cs = window.location.hash?.split('?')
            let channel = cs[1]?.split('=')[1]
            dispatch({ type: 'channel', params: { channel: decodeURI(channel) } })
        }
    }, [])

    //加工数据
    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[] = []
        columns().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
    }, [columns])
    return <>
        <TableData
            isBook
            isQs
            isTableDateArr
            isZj
            isDownload
            isGroupTab={false}
            isAdmin
            defaultSortOrder={{ columnKey: 'cost', order: 'descend'}}
            isUserId
            isSelctAcc
            isDataSelectPitcher
            sortArr={sortArr}
            columns={columns}
            ajax={getChannelOverview}
            title='广告投放详细数据'
            dataSource={data}
            scroll={{ x: 1000, y: 600 }}
            className='ranking'
        />
    </>
}

export default HocError(Page)