import { useAjax } from "@/Hook/useAjax"
import { LoginLogProps, getLoginLogListApi } from "@/services/gameData/player"
import React, { useEffect, useState } from "react"
import columns12 from "./tableConfig"
import TableData from "../../components/TableData"
import QueryForm from "@/components/QueryForm"
import moment from "moment"
import { Button } from "antd"
import LoginIpUser from "./loginIpUser"

/**
 * 登录日志
 * @returns 
 */
const LoginLog: React.FC = () => {

    /************************/
    const [visible, setVisible] = useState<boolean>(false)
    const [queryForm, setQueryForm] = useState<LoginLogProps>({ pageNum: 1, pageSize: 50, sourceSystem: 'ZX_ONE' })
    const getLoginLogList = useAjax((params) => getLoginLogListApi(params))
    /************************/

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


    return <div>
        <TableData
            czChild={<Button size="small" disabled={!queryForm?.ip} type="primary" onClick={() => setVisible(true)}>登陆IP玩家列表</Button>}
            leftChild={<QueryForm
                initialValues={{ sourceSystem: 'ZX_ONE' }}
                onChange={(data: any) => {
                    console.log(data)
                    const { beginDay, parentId, ...par } = data
                    let newQueryForm = JSON.parse(JSON.stringify(queryForm))
                    newQueryForm.pageNum = 1
                    newQueryForm.parentGameId = parentId
                    if (beginDay && beginDay?.length === 2) {
                        newQueryForm['beginDate'] = moment(beginDay[0]).format('YYYY-MM-DD')
                        newQueryForm['endDate'] = moment(beginDay[1]).format('YYYY-MM-DD')
                    } else {
                        delete newQueryForm['beginDate']
                        delete newQueryForm['endDate']
                    }
                    setQueryForm({ ...newQueryForm, ...par })
                }}
                isSource
                isBeginDay
                isBGGameClassify
                isGameId
                isIp
                isOs
                isParentId
                isGameRoleName
                isGameRoleId
                isServerIdUn
                isUserId
                isUserName
                isDeviceType
                isLoginType
            />}
            scroll={{ x: 1000, y: 600 }}
            ajax={getLoginLogList}
            fixed={{ left: 1, right: 0 }}
            dataSource={getLoginLogList?.data?.records}
            page={getLoginLogList?.data?.current || 1}
            pageSize={getLoginLogList?.data?.size || 20}
            total={getLoginLogList?.data?.total || 0}
            title='玩家登录日志'
            onChange={(props: any) => {
                console.log('props--->', props)
                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={'玩家登录日志'}
        />

        {visible && <LoginIpUser visible={visible} onClose={() => setVisible(false)} ip={queryForm?.ip || ''}/>}
    </div>
}

export default LoginLog