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

/**
 * 角色战力排行榜
 * @returns 
 */
const RoleFightingRanking: React.FC = () => {

    /********************************/
    const [queryForm, setQueryForm] = useState<GetCombatRankingProps>({ pageNum: 1, pageSize: 100, sourceSystem: 'ZX_ONE', beginDate: moment().format('YYYY-MM-DD'), endDate: moment().format('YYYY-MM-DD'), isMergeServer: true, isParticipateMerge: false })

    const getCombatRankingList = useAjax((params) => getCombatRankingListApi(params))
    /********************************/

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

    return <div>
        <TableData
            leftChild={<QueryForm
                initialValues={{ sourceSystem: 'ZX_ONE', serverDay: [moment(), moment()], isMergeServer: true, isParticipateMerge: true }}
                onChange={(data: any) => {
                    console.log(data)
                    const { parentId, serverDay, isParticipateMerge, ...params } = data
                    let newQueryForm = JSON.parse(JSON.stringify(queryForm))
                    newQueryForm.pageNum = 1
                    newQueryForm.parentGameId = parentId
                    newQueryForm.isParticipateMerge = !isParticipateMerge
                    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
                    }
                    if (!params?.superParentGameId) {
                        newQueryForm.isMergeServer = true
                    }
                    setQueryForm({ ...newQueryForm, ...params })
                }}
                isSource
                isServerDay={{ ranges: getPresetsRanking() }}
                // isGameId
                isSuperParentGameId
                // isParentId
                isIsMergeServer
                isIsParticipateMerge
                isServerIds
                isGameServerName
                isRankingNum
            />}
            scroll={{ x: 1000, y: 600 }}
            ajax={getCombatRankingList}
            fixed={{ left: 2, right: 0 }}
            dataSource={getCombatRankingList?.data?.records?.map((item: any, index: number) => ({ ...item, id: Number(queryForm.pageNum.toString() + index.toString()) }))}
            total={getCombatRankingList?.data?.total}
            page={queryForm.pageNum}
            pageSize={queryForm.pageSize}
            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 RoleFightingRanking