import { useAjax } from "@/Hook/useAjax"
import { getServerPayRetainedApi, GetServerPayRetainedProps, getServerPayRetainedTotalApi } from "@/services/gsData"
import React, { useEffect, useState } from "react"
import moment from "moment"
import TablePro from "@/pages/gameDataStatistics/components/TablePro"
import QueryFormGS from "@/components/QueryForm/indexGs"
import columns12 from "./tableConfig"


/**
 * GS区服付费留存
 * @returns 
 */
const ServerPayRetained: React.FC = () => {

    /***************************************/
    const [queryForm, setQueryForm] = useState<GetServerPayRetainedProps>({
        pageNum: 1,
        pageSize: 10,
        sourceSystem: 'ZX_ONE'
    })
    const [totalData, setTotalData] = useState<any[]>([])

    const getServerPayRetainedTotal = useAjax((params) => getServerPayRetainedTotalApi(params))
    const getServerPayRetained = useAjax((params) => getServerPayRetainedApi(params))
    /***************************************/

    useEffect(() => {
        getServerPayRetained.run(queryForm)
        getServerPayRetainedTotal.run(queryForm).then((res: { id: number; gsName: string, beginDay?: string }) => {
            res.id = 1
            res.gsName = '总计'
            res.beginDay = queryForm.serverStartBegin || moment().subtract(366, 'days').format('YYYY-MM-DD')
            setTotalData([res])
        })
    }, [queryForm])

    return <div>
        <TablePro
            leftChild={<QueryFormGS
                initialValues={{ sourceSystem: 'ZX_ONE' }}
                isSource
                isGsId
                isServeDay={{}}
                rechargeDay={{ placeholder: ['开服日期开始', '开服日期结束'] }}
                isSuperParentGameId
                isParentId
                isServerIds
                isServeStatus
                isGsStatus
                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['serverStartBegin'] = moment(rechargeDay[0]).format('YYYY-MM-DD')
                        newQueryForm['serverStartEnd'] = moment(rechargeDay[1]).format('YYYY-MM-DD')
                    } else {
                        delete newQueryForm['serverStartBegin']
                        delete newQueryForm['serverStartEnd']
                    }
                    setQueryForm({ ...newQueryForm, ...params })
                }}
            />}
            isZj
            totalData={totalData}
            config={columns12()}
            configName={'GS区服付费留存'}
            fixed={{ left: 4, right: 1 }}
            scroll={{ x: 1000, y: 620 }}
            title='GS区服付费留存'
            loading={getServerPayRetained.loading}
            ajax={getServerPayRetained}
            page={getServerPayRetained?.data?.current || 1}
            pageSize={getServerPayRetained?.data?.size || 20}
            total={getServerPayRetained?.data?.total || 0}
            dataSource={getServerPayRetained?.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 ServerPayRetained