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, { version } 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: 40,
        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 || moment().subtract(366, 'days').format('YYYY-MM-DD')
            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() }}
                isGameIds
                isBGGameClassify
                isUserEnterType
                isGameDimension
            />}
            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'
            }}
            estimatedRowHeight={() => {
                let mySelectFieldData = localStorage.getItem(`myAdFieldConfig${version}_游戏首日复充`)
                if (mySelectFieldData) {
                    let newSelectFieldData = JSON.parse(mySelectFieldData).date_field
                    return (newSelectFieldData?.length + 1) * 20.5 || 20.5 * 4
                } else {
                    return 20.5 * 4
                }
            }}
            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(queryForm.sourceSystem)}
            configName={'游戏首日复充'}
        />
    </div>
}

export default Again