import { ActiveEnum, getPresets } from "@/components/QueryForm/const"
import React, { useEffect, useState } from "react"
import TableData from "../../components/TableData"
import QueryForm from "@/components/QueryForm"
import { useAjax } from "@/Hook/useAjax"
import moment from "moment"
import columns12 from "./tableConfig"
import { getMediumActiveDataListApi, getMediumActiveDataTotalApi, MediumActiveDataProps } from "@/services/gameData/medium"

/**
 * 游戏媒体留存
 * @returns 
 */
const MediumActive: React.FC = () => {
 
    /************************************/
    const [queryForm, setQueryForm] = useState<MediumActiveDataProps>({
        activeTypes: 'reg' as ActiveEnum,
        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 getMediumActiveDataList = useAjax((params) => getMediumActiveDataListApi(params))
    const getMediumActiveDataTotal = useAjax((params) => getMediumActiveDataTotalApi(params))
    /************************************/

    useEffect(() => {
        getMediumActiveDataList.run(queryForm)
        getMediumActiveDataTotal.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', activeTypes: 'reg' }}
                onChange={(data: any) => {
                    console.log(data)
                    const { regStartDay, gameClassify, regEndDay, rechargeDay, type, ...params } = data
                    let newQueryForm = JSON.parse(JSON.stringify(queryForm))
                    newQueryForm.pageNum = 1
                    newQueryForm.accountType = type
                    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
                isActiveTypes
                isType
                isGameDimension
            />}
            isZj
            totalData={totalData}
            scroll={{ x: 1000, y: 600 }}
            ajax={getMediumActiveDataList}
            fixed={{ left: 2, right: 0 }}
            dataSource={getMediumActiveDataList?.data?.records?.map((item: any, index: number) => ({ ...item, activeTypes: queryForm?.activeTypes, id: Number(queryForm.pageNum.toString() + index.toString()) }))}
            total={getMediumActiveDataList?.data?.total}
            page={queryForm.pageNum}
            pageSize={queryForm.pageSize}
            sortData={{
                field: queryForm?.sortFiled,
                order: queryForm?.sortType === 'asc' ? 'ascend' : 'descend'
            }}
            estimatedRowHeight={() => 41}
            title={`游戏媒体留存数据(用户活跃数据2023-08-20开始统计)(T+1)`}
            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 MediumActive