import React, { useEffect, useState } from "react"
import TableData from "../../components/TableData"
import columns12 from "./tableConfig"
import { useAjax } from "@/Hook/useAjax"
import { GameTotalProps, getGameTotalListApi, getGameTotalTotalApi } from "@/services/gameData/game"
import QueryForm from "@/components/QueryForm"
import { getPresets } from "@/components/QueryForm/const"
import moment from "moment"
import '../everyday/index.less'

/**
 * 游戏总数居
 * @returns 
 */
const Total: React.FC = () => {

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

    const getGameTotalList = useAjax((params) => getGameTotalListApi(params))
    const getGameTotalTotal = useAjax((params) => getGameTotalTotalApi(params))
    /*****************************/

    useEffect(() => {
        getGameTotalList.run(queryForm)
        getGameTotalTotal.run(queryForm).then((res: { id: number; gameName: string }) => {
            res.id = 1
            res.gameName = '总计'
            setTotalData([res])
        })
    }, [queryForm])

    return <div>
        <TableData
            leftChild={<QueryForm
                initialValues={{ regDay: [moment(), moment()], rechargeDay: [moment(), moment()], sourceSystem: 'ZX_ONE', tableTypes: 'buy' }}
                onChange={(data: any) => {
                    console.log(data)
                    const { regStartDay, regEndDay, rechargeDay, ...params } = data
                    let newQueryForm = JSON.parse(JSON.stringify(queryForm))
                    newQueryForm.pageNum = 1
                    if (rechargeDay && rechargeDay?.length === 2) {
                        newQueryForm['rechargeBeginDate'] = moment(rechargeDay[0]).format('YYYY-MM-DD')
                        newQueryForm['rechargeEndDate'] = moment(rechargeDay[1]).format('YYYY-MM-DD')
                    } else {
                        delete newQueryForm['rechargeBeginDate']
                        delete newQueryForm['rechargeEndDate']
                    }
                    if (regStartDay && regEndDay) {
                        newQueryForm.registeredBeginDate = regStartDay
                        newQueryForm.registeredEndDate = regEndDay
                    } else {
                        delete newQueryForm.registeredBeginDate
                        delete newQueryForm.registeredEndDate
                    }
                    setQueryForm({ ...newQueryForm, ...params })
                }}
                isSource
                isUserEnterType
                isRegDay={{ ranges: getPresets() }}
                isBGGameClassify
                rechargeDay={{ ranges: getPresets() }}
                isGameIds
                isGameDimension
            />}
            isZj
            totalData={totalData}
            scroll={{ x: 1000, y: 600 }}
            ajax={getGameTotalList}
            fixed={{ left: 5, right: 0 }}
            dataSource={getGameTotalList?.data?.records?.map((item: any, index: number) => ({ ...item, id: Number(queryForm.pageNum.toString() + index.toString()) }))}
            total={getGameTotalList?.data?.total}
            page={queryForm.pageNum}
            pageSize={queryForm.pageSize}
            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()}
            configName={'游戏总数据'}
        />
    </div>
}

export default Total