import React, { useEffect, useState } from "react"
import TableData from "../../components/TableData"
import columns12 from "./tableConfig"
import { useAjax } from "@/Hook/useAjax"
import { GameTotalProps, getGameTotalListApi } from "@/services/gameData/game"
import QueryForm from "@/components/QueryForm"
import { czPresets } from "@/components/QueryForm/const"
import moment from "moment"

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

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

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

    useEffect(() => {
        getGameTotalList.run(queryForm)
    }, [queryForm])

    return <div>

        <TableData
            leftChild={<QueryForm
                initialValues={{ regDay: [moment().subtract(30, 'd'), moment()], rechargeDay: [moment().subtract(30, 'd'), moment()] }}
                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 })
                }}
                isRegDay
                isGameName
                isBGGameClassify
                rechargeDay={{ ranges: czPresets }}
            />}
            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}
            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