import QueryForm from "@/components/QueryForm"
import { useAjax } from "@/Hook/useAjax"
import TablePro from "@/pages/gameDataStatistics/components/TablePro"
import { getRoleChatDetailsListApi, GetRoleChatDetailsListApiProps } from "@/services/gsData"
import React, { useEffect, useState } from "react"
import moment from "moment"
import { Modal } from "antd"
import columns13 from "./tableConfigDetails"

interface RoleChatDetailsProps {
    roleId: string
    dt: string
    visible?: boolean
    onClose?: () => void
}
/**
 * 角色IP风险监控
 * @returns 
 */
const RoleChatDetails: React.FC<RoleChatDetailsProps> = ({ roleId, dt, visible, onClose }) => {


    /****************************************/
    const [queryForm, setQueryForm] = useState<GetRoleChatDetailsListApiProps>({
        pageNum: 1,
        pageSize: 30,
        startDate: dt,
        endDate: dt,
    })
    const getRoleChatDetailsList = useAjax((params) => getRoleChatDetailsListApi(params))
    /****************************************/

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

    return <Modal
        title={<strong>角色聊天详情</strong>}
        visible={visible}
        footer={null}
        onCancel={onClose}
        width={1000}
        bodyStyle={{ padding: 0 }}
    >
        <TablePro
            leftChild={<QueryForm
                initialValues={{ createRoleDay: [moment(dt), moment(dt)] }}
                isCreateRoleDay={{ placeholder: ['日期开始', '日期结束'] }}
                onChange={(data: any) => {
                    console.log(data)
                    const { createRoleDay, ...params } = data
                    let newQueryForm = JSON.parse(JSON.stringify(queryForm))
                    newQueryForm.pageNum = 1
                    if (createRoleDay && createRoleDay?.length === 2) {
                        newQueryForm['startDate'] = moment(createRoleDay[0]).format('YYYY-MM-DD')
                        newQueryForm['endDate'] = moment(createRoleDay[1]).format('YYYY-MM-DD')
                    } else {
                        delete newQueryForm['startDate']
                        delete newQueryForm['endDate']
                    }
                    setQueryForm({ ...newQueryForm, ...params })
                }}
            />}
            config={columns13()}
            configName={'角色聊天详情列表'}
            fixed={{ left: 2, right: 1 }}
            scroll={{ x: 1000, y: 620 }}
            loading={getRoleChatDetailsList.loading}
            ajax={getRoleChatDetailsList}
            page={getRoleChatDetailsList?.data?.current || 1}
            pageSize={getRoleChatDetailsList?.data?.size || 20}
            total={getRoleChatDetailsList?.data?.total || 0}
            dataSource={getRoleChatDetailsList?.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 })
            }}
        />
    </Modal>
}

export default RoleChatDetails