import React, { useEffect, useState } from "react"
import TableData from "../../components/TableData"
import QueryForm from "@/components/QueryForm"
import { useAjax } from "@/Hook/useAjax"
import { ActiveDataProps, getActiveDataListApi, getActiveDataTotalApi } from "@/services/gameData/game"
import columns12 from "./tableConfig"
import moment from "moment"
import { ActiveEnum, TableEnum, getPresets } from "@/components/QueryForm/const"

/**
 * 游戏留存数据
 * @returns 
 */
const Active: React.FC = () => {

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

    const getActiveDataList = useAjax((params) => getActiveDataListApi(params))
    const getActiveDataTotal = useAjax((params) => getActiveDataTotalApi(params))
    /************************************/

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

    return <div>
        <TableData
            leftChild={<QueryForm
                initialValues={{ regDay: [moment(), moment()], sourceSystem: 'ZX_ONE', tableTypes: 'buy', activeTypes: 'reg' }}
                onChange={(data: any) => {
                    console.log(data)
                    const { regStartDay, gameClassify, 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
                    }
                    newQueryForm.classify = gameClassify
                    setQueryForm({ ...newQueryForm, ...params })
                }}
                isSource
                isRegDay={{ ranges: getPresets() }}
                isGameIds
                isBGGameClassify
                isUserEnterType
                isActiveTypes
                isGameDimension
            />}
            isZj
            totalData={totalData}
            scroll={{ x: 1000, y: 600 }}
            ajax={getActiveDataList}
            fixed={{ left: 2, right: 0 }}
            dataSource={getActiveDataList?.data?.records?.map((item: any, index: number) => ({ ...item, activeTypes: queryForm?.activeTypes, id: Number(queryForm.pageNum.toString() + index.toString()) }))}
            total={getActiveDataList?.data?.total}
            page={queryForm.pageNum}
            pageSize={queryForm.pageSize}
            sortData={{
                field: queryForm?.sortFiled,
                order: queryForm?.sortType === 'asc' ? 'ascend' : 'descend'
            }}
            estimatedRowHeight={() => 41}
            title={`游戏留存数据(用户活跃数据2023-08-20开始统计)`}
            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()}
            configName={'游戏留存数据'}
        />
    </div>
}

export default Active