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 { getRoleManageListApi, getRoleManageProps } from "@/services/gsData"

/**
 * 游戏角色管理
 * @returns 
 */
const RoleManage: React.FC = () => {

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

    const getRoleManageList = useAjax((params) => getRoleManageListApi(params))
    /***************************************/

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

    return <div>
        <TablePro
            leftChild={<QueryFormGS
                initialValues={{ sourceSystem: 'ZX_ONE', roleSource: 2 }}
                isSource
                isGsId
                isServeDay={{}}
                rechargeDay={{ placeholder: ['角色创建开始日期', '角色创建结束日期'] }}
                isSuperParentGameId
                isParentId
                isServerIds
                isServeStatus
                isGsStatus
                isGameRoleName
                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['roleCreateDayBegin'] = moment(rechargeDay[0]).format('YYYY-MM-DD')
                        newQueryForm['roleCreateDayEnd'] = moment(rechargeDay[1]).format('YYYY-MM-DD')
                    } else {
                        delete newQueryForm['roleCreateDayBegin']
                        delete newQueryForm['roleCreateDayEnd']
                    }
                    setQueryForm({ ...newQueryForm, ...params })
                }}
            />}
            config={columns12()}
            configName={'游戏角色管理'}
            fixed={{ left: 4, right: 0 }}
            scroll={{ x: 1000, y: 620 }}
            title='游戏角色管理'
            loading={getRoleManageList.loading}
            ajax={getRoleManageList}
            page={getRoleManageList?.data?.current || 1}
            pageSize={getRoleManageList?.data?.size || 20}
            total={getRoleManageList?.data?.total || 0}
            dataSource={getRoleManageList?.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 RoleManage