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

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

    /*****************************/
    const [queryForm, setQueryForm] = useState<GameH5Props>({
        pageNum: 1, pageSize: 100,
        sourceSystem: 'ZX_ONE',
        rechargeBeginDate: moment().format('YYYY-MM-DD'),
        rechargeEndDate: moment().format('YYYY-MM-DD'),
        costBeginDate: moment().format('YYYY-MM-DD'),
        costEndDate: moment().format('YYYY-MM-DD')
    })

    const getGameH5List = useAjax((params) => getGameH5ListApi(params))
    /*****************************/

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

    return <div>

        <TableData
            leftChild={<QueryForm
                initialValues={{ sourceSystem: 'ZX_ONE', rechargeDay: [moment(), moment()], regDay: [moment(), moment()] }}
                onChange={(data: any) => {
                    console.log(data)
                    const { regStartDay, regEndDay, rechargeDay, ...params } = data
                    let newQueryForm = JSON.parse(JSON.stringify(queryForm))
                    newQueryForm.pageNum = 1
                    if (regStartDay && regEndDay) {
                        newQueryForm.costBeginDate = regStartDay
                        newQueryForm.costEndDate = regEndDay
                    } else {
                        delete newQueryForm.costBeginDate
                        delete newQueryForm.costEndDate
                    }
                    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']
                    }
                    setQueryForm({ ...newQueryForm, ...params })
                }}
                isSource
                isRegDay={{ ranges: getPresets() }}
                isGameId
                rechargeDay={{ ranges: getPresets() }}
            />}
            scroll={{ x: 1000, y: 600 }}
            ajax={getGameH5List}
            fixed={{ left: 3, right: 1 }}
            dataSource={getGameH5List?.data?.records?.map((item: any, index: number) => ({ ...item, id: Number(queryForm.pageNum.toString() + index.toString()) }))}
            total={getGameH5List?.data?.total}
            page={queryForm.pageNum}
            pageSize={queryForm.pageSize}
            title='H5游戏充值表(T+1)'
            size="small"
            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={'H5游戏充值表'}
        />
    </div>
}

export default H5Recharge