import { GameServerEveryListProps, getGameServerEveryListApi, getGameServerEveryTotalApi } from "@/services/gameData/gameServer"
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"

/**
 * 区服每日数据
 * @returns 
 */
const ServerEveryData: React.FC = () => {

    /********************************/
    const [queryForm, setQueryForm] = useState<GameServerEveryListProps>({ pageNum: 1, pageSize: 40, beginDate: moment().format('YYYY-MM-DD'), endDate: moment().format('YYYY-MM-DD'), sourceSystem: 'ZX_ONE' })
    const [totalData, setTotalData] = useState<any[]>([])

    const getGameServerEveryList = useAjax((params) => getGameServerEveryListApi(params))
    const getGameServerEveryTotal = useAjax((params) => getGameServerEveryTotalApi(params))
    /********************************/

    useEffect(() => {
        getGameServerEveryTotal.run(queryForm).then((res: { id: number; dt?: string; gameName: string; }) => {
            res.id = 1
            res.dt = queryForm.beginDate || moment().subtract(366, 'days').format('YYYY-MM-DD')
            res.gameName = '总计'
            setTotalData([res])
        })
        getGameServerEveryList.run(queryForm)
    }, [queryForm])

    return <div>
        <TableData
            leftChild={<QueryForm
                initialValues={{ sourceSystem: 'ZX_ONE', serverDay: [moment(), moment()] }}
                onChange={(data: any) => {
                    const { serverDay, ...params } = data
                    let newQueryForm = JSON.parse(JSON.stringify(queryForm))
                    if (serverDay && serverDay?.length === 2) {
                        newQueryForm['beginDate'] = moment(serverDay[0]).format('YYYY-MM-DD')
                        newQueryForm['endDate'] = moment(serverDay[1]).format('YYYY-MM-DD')
                    } else {
                        delete newQueryForm['beginDate']
                        delete newQueryForm['endDate']
                    }
                    newQueryForm.pageNum = 1
                    setQueryForm({ ...newQueryForm, ...params })
                }}
                isSource
                isServerDay={{ ranges: getPresets() }}
                isGameIds
                isGameDimension
            />}
            isZj
            totalData={totalData}
            scroll={{ x: 1000, y: 600 }}
            ajax={getGameServerEveryList}
            fixed={{ left: 5, right: 1 }}
            dataSource={getGameServerEveryList?.data?.records?.map((item: any, index: number) => ({ ...item, id: Number(queryForm.pageNum.toString() + index.toString()) }))}
            total={getGameServerEveryList?.data?.total}
            page={queryForm.pageNum}
            pageSize={queryForm.pageSize}
            sortData={{
                field: queryForm?.sortFiled,
                order: queryForm?.sortType === 'asc' ? 'ascend' : 'descend'
            }}
            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 ServerEveryData