import React, { useEffect, useState } from "react"
import TableData, { version } from "../../components/TableData"
import columns12 from "./tableConfig"
import { GameDayProps, getGameDayListApi, getGameDayTotalApi } from "@/services/gameData/game"
import { useAjax } from "@/Hook/useAjax"
import QueryForm from "@/components/QueryForm"
import moment from "moment"
import RechargeTrendGame from "@/components/rechargeTrend/index_game"
import './index.less'
import { getPresets } from "@/components/QueryForm/const"

/**
 * 游戏每日数据
 * @returns 
 */
const Everyday: React.FC = () => {

    /*****************************/
    const [queryForm, setQueryForm] = useState<GameDayProps>({
        pageNum: 1, pageSize: 40,
        registeredBeginDate: moment().format('YYYY-MM-DD'),
        registeredEndDate: moment().format('YYYY-MM-DD'),
        sourceSystem: 'ZX_ONE',
        tableTypes: 'buy'
    })
    const [totalData, setTotalData] = useState<any[]>([])

    const [rtConfig, setRtConfig] = useState<{ gameId?: number, gameName?: string, beginDate?: string, sourceSystem?: string, visible: boolean }>({ visible: false })

    const getGameDayList = useAjax((params) => getGameDayListApi(params))
    const getGameDayTotal = useAjax((params) => getGameDayTotalApi(params))
    /*****************************/

    useEffect(() => {
        getGameDayList.run(queryForm)
        getGameDayTotal.run(queryForm).then((res: { id: number; costDate: string; beginDay: string | undefined }) => {
            res.id = 1
            res.costDate = '总计'
            res.beginDay = queryForm.registeredBeginDate || moment().subtract(366, 'days').format('YYYY-MM-DD')
            setTotalData([res])
        })
    }, [queryForm])

    const rechargeTrendHandle = (data: any) => {
        setRtConfig({ visible: true, gameName: data?.gameName, gameId: data?.gameId, sourceSystem: data?.sourceSystem, beginDate: data.costDate })
    }

    return <div>
        <TableData
            leftChild={<QueryForm
                initialValues={{ regDay: [moment(), moment()], sourceSystem: 'ZX_ONE', tableTypes: 'buy' }}
                onChange={(data: any) => {
                    const { regStartDay, regEndDay, rechargeDay, ...params } = data
                    let newQueryForm = JSON.parse(JSON.stringify(queryForm))
                    newQueryForm.pageNum = 1
                    if (regStartDay && regEndDay) {
                        newQueryForm.registeredBeginDate = regStartDay
                        newQueryForm.registeredEndDate = regEndDay
                    } else {
                        delete newQueryForm.registeredBeginDate
                        delete newQueryForm.registeredEndDate
                    }
                    if (rechargeDay && rechargeDay?.length === 2) {
                        newQueryForm['rechargeBeginDate'] = moment(rechargeDay[0]).format('YYYY-MM-DD')
                        newQueryForm['rechargeEndDate'] = moment(rechargeDay[0]).format('YYYY-MM-DD')
                    } else {
                        delete newQueryForm['rechargeBeginDate']
                        delete newQueryForm['rechargeEndDate']
                    }
                    setQueryForm({ ...newQueryForm, ...params })
                }}
                isUserEnterType
                isSource
                isRegDay={{ ranges: getPresets() }}
                isGameIds
                isGameDimension
            />}
            isZj
            totalData={totalData}
            scroll={{ x: 1000, y: 600 }}
            ajax={getGameDayList}
            fixed={{ left: 3, right: 2 }}
            dataSource={getGameDayList?.data?.records?.map((item: any, index: number) => ({ ...item, id: Number(queryForm.pageNum.toString() + index.toString()) }))}
            total={getGameDayList?.data?.total}
            page={queryForm.pageNum}
            pageSize={queryForm.pageSize}
            estimatedRowHeight={() => {
                let mySelectFieldData = localStorage.getItem(`myAdFieldConfig${version}_游戏每日付费趋势`)
                if (mySelectFieldData) {
                    let newSelectFieldData = JSON.parse(mySelectFieldData).date_field
                    return newSelectFieldData?.length * 20.5 || 20.5 * 5
                } else {
                    return 20.5 * 5
                }
            }}
            sortData={{
                field: queryForm?.sortFiled,
                order: queryForm?.sortType === 'asc' ? 'ascend' : 'descend'
            }}
            title={`游戏每日付费趋势`}
            onChange={(props: any) => {
                let { pagination, sortData } = props
                let { current, pageSize } = pagination
                let newQueryForm = JSON.parse(JSON.stringify(queryForm))
                if (sortData && sortData?.order) {
                    newQueryForm['sortType'] = sortData?.order === 'ascend' ? 'asc' : 'desc'
                    newQueryForm['sortFiled'] = sortData?.field
                } else {
                    delete newQueryForm['sortType']
                    delete newQueryForm['sortFiled']
                }
                newQueryForm.pageNum = current
                newQueryForm.pageSize = pageSize
                setQueryForm({ ...newQueryForm })
            }}
            config={columns12(rechargeTrendHandle)}
            configName={'游戏每日付费趋势'}
        />

        {rtConfig.visible && <RechargeTrendGame {...rtConfig} onClose={() => setRtConfig({ visible: false })} />}
    </div>
}

export default Everyday