import { useAjax } from "@/Hook/useAjax"
import React, { useEffect, useState } from "react"
import columns12 from "./tableConfig"
import TablePro from "@/pages/gameDataStatistics/components/TablePro"
import QueryFormGS from "@/components/QueryForm/indexGs"
import moment from "moment"
import { getServerManageListApi, GetServerManageProps } from "@/services/gsData"

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

    /***************************************/
    const [queryForm, setQueryForm] = useState<GetServerManageProps>({
        pageNum: 1,
        pageSize: 30,
        sourceSystem: 'ZX_ONE'
    })

    const getServerManageList = useAjax((params) => getServerManageListApi(params))
    /***************************************/

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

    return <div>
        <TablePro
            leftChild={<QueryFormGS
                initialValues={{ sourceSystem: 'ZX_ONE' }}
                isSource
                isGsId
                isServeDay={{}}
                rechargeDay={{ placeholder: ['开服日期开始', '开服日期结束'] }}
                isSuperParentGameId
                isParentId
                isServerIds
                isServeStatus
                isGsStatus
                onChange={(data: any) => {
                    console.log(data)
                    const { rechargeDay, gsId, parentId, superParentGameId, serverIds, serveDay, ...params } = data
                    let newQueryForm = JSON.parse(JSON.stringify(queryForm))
                    newQueryForm.pageNum = 1
                    newQueryForm.gsIdList = gsId
                    newQueryForm.parentGameId = parentId
                    newQueryForm.superGameId = superParentGameId
                    newQueryForm.serverIdList = serverIds
                    if (serveDay && serveDay?.length === 2) {
                        newQueryForm['serveDayBegin'] = moment(serveDay[0]).format('YYYY-MM-DD')
                        newQueryForm['serveDayEnd'] = moment(serveDay[1]).format('YYYY-MM-DD')
                    } else {
                        delete newQueryForm['serveDayBegin']
                        delete newQueryForm['serveDayEnd']
                    }
                    if (rechargeDay && rechargeDay?.length === 2) {
                        newQueryForm['serverStartBegin'] = moment(rechargeDay[0]).format('YYYY-MM-DD')
                        newQueryForm['serverStartEnd'] = moment(rechargeDay[1]).format('YYYY-MM-DD')
                    } else {
                        delete newQueryForm['serverStartBegin']
                        delete newQueryForm['serverStartEnd']
                    }
                    setQueryForm({ ...newQueryForm, ...params })
                }}
            />}
            config={columns12()}
            configName={'游戏区服管理'}
            fixed={{ left: 4, right: 0 }}
            scroll={{ x: 1000, y: 620 }}
            title='游戏区服管理'
            loading={getServerManageList.loading}
            ajax={getServerManageList}
            page={getServerManageList?.data?.current || 1}
            pageSize={getServerManageList?.data?.size || 20}
            total={getServerManageList?.data?.total || 0}
            dataSource={getServerManageList?.data?.records?.map((item: any, index: number) => ({ ...item, id: Number(queryForm.pageNum.toString() + (index + '')) }))}
            onChange={(pagination: any, _: any, sortData: any) => {
                let { current, pageSize } = pagination
                let newQueryForm = JSON.parse(JSON.stringify(queryForm))
                if (sortData && sortData?.order) {
                    newQueryForm['sortAsc'] = sortData?.order === 'ascend' ? true : false
                    newQueryForm['sortFiled'] = sortData?.field
                } else {
                    delete newQueryForm['sortAsc']
                    delete newQueryForm['sortFiled']
                }
                newQueryForm.pageNum = current || newQueryForm.pageNum
                newQueryForm.pageSize = pageSize || newQueryForm.pageSize
                setQueryForm({ ...newQueryForm })
            }}
        />
    </div>
}

export default ServerManage