import React, { useEffect, useState } from "react"
import TableData from "../../components/TableData"
import columns12 from "./tableConfig"
import { useAjax } from "@/Hook/useAjax"
import { AccountProps, getAccountRechargeRankingListApi } from "@/services/gameData/rankingList"
import QueryForm from "@/components/QueryForm"
import { getPresetsRanking } from "@/components/QueryForm/const"
import moment from "moment"

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

    /********************************/
    const [queryForm, setQueryForm] = useState<AccountProps>({ pageNum: 1, pageSize: 100, sourceSystem: 'ZX_ONE', beginDay: moment().format('YYYY-MM-DD'), endDay: moment().format('YYYY-MM-DD') })

    const getAccountRechargeRankingList = useAjax((params) => getAccountRechargeRankingListApi(params))
    /********************************/

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

    return <div>

        <TableData
            leftChild={<QueryForm
                initialValues={{ sourceSystem: 'ZX_ONE', consumeDay: [moment(), moment()] }}
                onChange={(data: any) => {
                    console.log(data)
                    const { costBeginDay, costEndDay, ...params } = data
                    let newQueryForm = JSON.parse(JSON.stringify(queryForm))
                    newQueryForm.pageNum = 1
                    if (costBeginDay && costEndDay) {
                        newQueryForm.beginDay = costBeginDay
                        newQueryForm.endDay = costEndDay
                    } else {
                        delete newQueryForm.beginDay
                        delete newQueryForm.endDay
                    }
                    setQueryForm({ ...newQueryForm, ...params })
                }}
                isSource
                isConsumeDay={{ ranges: getPresetsRanking() }}
                isSysUserId
                isType
                isAccountId
                isGameId
            />}
            scroll={{ x: 1000, y: 600 }}
            ajax={getAccountRechargeRankingList}
            fixed={{ left: 3, right: 3 }}
            dataSource={getAccountRechargeRankingList?.data?.records?.map((item: any, index: number) => ({ ...item, id: Number(queryForm.pageNum.toString() + index.toString()) }))}
            total={getAccountRechargeRankingList?.data?.total}
            page={queryForm.pageNum}
            pageSize={queryForm.pageSize}
            title='推广账号消耗排行榜(T+1)'
            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 Account