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

/**
 * 角色IP风险监控
 * @returns 
 */
const RoleIpMonitor: React.FC = () => {


    /****************************************/
    const [queryForm, setQueryForm] = useState<GetRoleIpInfoListProps>({
        pageNum: 1,
        pageSize: 30
    })
    const getRoleIpInfoList = useAjax((params) => getRoleIpInfoListApi(params))
    /****************************************/

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

    return <div>
        <TablePro
            leftChild={<QueryForm
                isUserId
                isNickname
                isGameRoleName
                isGameRoleId
                isGameName
                isSuperParentGameId
                isGameIds
                isGameServerName
                isServerIds
                isCreateRoleDay={{}}
                isPayIntervalTime={{ tips: '角色等级区间' }}
                isIp
                onChange={(data: any) => {
                    console.log(data)
                    const { nickname, superParentGameId, createRoleDay, serverIds, regPayIntervalTime, lastActiveTime, ip, ...params } = data
                    let newQueryForm = JSON.parse(JSON.stringify(queryForm))
                    newQueryForm.pageNum = 1
                    newQueryForm.serverId = serverIds
                    newQueryForm.userName = nickname
                    newQueryForm.regIp = ip
                    if (createRoleDay && createRoleDay?.length === 2) {
                        newQueryForm['createTimeMin'] = moment(createRoleDay[0]).format('YYYY-MM-DD')
                        newQueryForm['createTimeMax'] = moment(createRoleDay[1]).format('YYYY-MM-DD')
                    } else {
                        delete newQueryForm['createTimeMin']
                        delete newQueryForm['createTimeMax']
                    }
                    if (regPayIntervalTime?.length > 0 && (regPayIntervalTime[0] || regPayIntervalTime[1])) {
                        newQueryForm.roleLevelMin = regPayIntervalTime[0]
                        newQueryForm.roleLevelMax = regPayIntervalTime[1]
                    } else {
                        delete newQueryForm.roleLevelMin
                        delete newQueryForm.roleLevelMax
                    }
                    setQueryForm({ ...newQueryForm, ...params })
                }}
            />}
            config={columns12()}
            configName={'角色IP风险监控'}
            fixed={{ left: 2, right: 1 }}
            scroll={{ x: 1000, y: 620 }}
            title='角色IP风险监控'
            loading={getRoleIpInfoList.loading}
            ajax={getRoleIpInfoList}
            page={getRoleIpInfoList?.data?.current || 1}
            pageSize={getRoleIpInfoList?.data?.size || 20}
            total={getRoleIpInfoList?.data?.total || 0}
            dataSource={getRoleIpInfoList?.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['sortType'] = sortData?.order === 'ascend' ? 'asc' : 'desc'
                    newQueryForm['sortFiled'] = sortData?.field
                } else {
                    delete newQueryForm['sortType']
                    delete newQueryForm['sortFiled']
                }
                newQueryForm.pageNum = current || newQueryForm.pageNum
                newQueryForm.pageSize = pageSize || newQueryForm.pageSize
                setQueryForm({ ...newQueryForm })
            }}
        />
    </div>
}

export default RoleIpMonitor