import { AgainListProps, getAgainListApi, getAgainTotalApi } from "@/services/gameData/game"
import React, { useEffect, useState } from "react"
import moment from "moment"
import { useAjax } from "@/Hook/useAjax"
import TableData from "../../components/TableData"
import QueryForm from "@/components/QueryForm"
import columns12 from "./tableConfig"
import { getPresets } from "@/components/QueryForm/const"


const Again: React.FC = () => {

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

    const getAgainList = useAjax((params) => getAgainListApi(params))
    const getAgainTotal = useAjax((params) => getAgainTotalApi(params))
    /************************************/

    useEffect(() => {
        getAgainList.run(queryForm)
        getAgainTotal.run(queryForm).then((res: { id: number; costDate: string; beginDay: string | undefined }) => {
            res.id = 1
            res.costDate = '总计'
            res.beginDay = queryForm.registeredBeginDate
            setTotalData([res])
        })
    }, [queryForm])

    return <div>
        <TableData
            leftChild={<QueryForm
                initialValues={{ regDay: [moment(), moment()], sourceSystem: 'ZX_ONE', tableTypes: 'buy' }}
                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() }}
                isGameId
                isBGGameClassify
                isUserEnterType
            />}
            isZj
            totalData={totalData}
            scroll={{ x: 1000, y: 600 }}
            ajax={getAgainList}
            fixed={{ left: 2, right: 1 }}
            dataSource={getAgainList?.data?.records?.map((item: any, index: number) => ({ ...item, id: Number(queryForm.pageNum.toString() + index.toString()) }))}
            total={getAgainList?.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 Again