import { GameServerListProps, getGameServerListApi, getGameServerTotalApi } 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 { ActiveEnum, getPresets } from "@/components/QueryForm/const"

/**
 * 游戏区服
 * @returns 
 */
const ServerData: React.FC = () => {

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

    const getGameServerList = useAjax((params) => getGameServerListApi(params))
    const getGameServerTotal = useAjax((params) => getGameServerTotalApi(params))
    /********************************/

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

    return <div>
        <TableData
            leftChild={<QueryForm
                initialValues={{ sourceSystem: 'ZX_ONE', serverDay: [moment(), moment()], activeTypes: 'reg' }}
                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
                isServerName
                isServerId
                isActiveTypes
            />}
            isZj
            totalData={totalData}
            scroll={{ x: 1000, y: 600 }}
            ajax={getGameServerList}
            fixed={{ left: 5, right: 1 }}
            dataSource={getGameServerList?.data?.records?.map((item: any, index: number) => ({ ...item, activeTypes: queryForm?.activeTypes, id: Number(queryForm.pageNum.toString() + index.toString()) }))}
            total={getGameServerList?.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 ServerData