import { useAjax } from "@/Hook/useAjax"
import { MediaGameDayProps, getMediaGameDayListApi, getMediaGameDayTotalApi } from "@/services/gameData/medium"
import React, { useEffect, useState } from "react"
import moment from "moment"
import columns12 from "./tableConfig"
import { getPresets } from "@/components/QueryForm/const"
import TableData from "../../components/TableData"
import QueryForm from "@/components/QueryForm"


/**
 * 游戏媒体每日数据
 * @returns 
 */
const GameEvery: React.FC = () => {

    /*****************************/
    const [queryForm, setQueryForm] = useState<MediaGameDayProps>({
        pageNum: 1, pageSize: 50,
        sourceSystem: 'ZX_ONE',
        beginDate: moment().format('YYYY-MM-DD'), //subtract(30, 'd').
        endDate: moment().format('YYYY-MM-DD')
    })
    const [totalData, setTotalData] = useState<any[]>([])

    const getMediaGameDayList = useAjax((params) => getMediaGameDayListApi(params))
    const getMediaGameDayTotal = useAjax((params) => getMediaGameDayTotalApi(params))
    /*****************************/


    useEffect(() => {
        getMediaGameDayList.run(queryForm)
        getMediaGameDayTotal.run(queryForm).then((res: { id: number; gameName: string, beginDay?: string }) => {
            res.id = 1
            res.gameName = '总计'
            res.beginDay = queryForm.beginDate
            setTotalData([res])
        })
    }, [queryForm])

    return <div>
        <TableData
            leftChild={<QueryForm
                initialValues={{ consumeDay: [moment(), moment()], sourceSystem: 'ZX_ONE' }}
                onChange={(data: any) => {
                    console.log(data)
                    const { costBeginDay, costEndDay, type, gameClassify, cpName, ...params } = data
                    let newQueryForm = JSON.parse(JSON.stringify(queryForm))
                    newQueryForm.pageNum = 1
                    newQueryForm.accountType = type
                    newQueryForm.gameType = gameClassify
                    newQueryForm.gameCp = cpName
                    if (costBeginDay && costEndDay) {
                        newQueryForm.beginDate = costBeginDay
                        newQueryForm.endDate = costEndDay
                    } else {
                        delete newQueryForm.beginDate
                        delete newQueryForm.endDate
                    }
                    setQueryForm({ ...newQueryForm, ...params })
                }}
                isSource
                isConsumeDay={{ ranges: getPresets() }}
                isCpName
                isGameId
                isBGGameClassify
                isType
            />}
            isZj
            totalData={totalData}
            scroll={{ x: 1000, y: 600 }}
            ajax={getMediaGameDayList}
            fixed={{ left: 2, right: 1 }}
            dataSource={getMediaGameDayList?.data?.records?.map((item: any, index: number) => ({ ...item, id: Number(queryForm.pageNum.toString() + index.toString()) }))}
            total={getMediaGameDayList?.data?.total}
            page={queryForm.pageNum}
            pageSize={queryForm.pageSize}
            sortData={{
                field: queryForm?.sortFiled,
                order: queryForm?.sortType === 'asc' ? 'ascend' : 'descend'
            }}
            title={`游戏媒体每日数据`}
            onChange={(props: any) => {
                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={'游戏媒体每日数据'}
        />
    </div>
}


export default GameEvery