import QueryForm from "@/components/QueryForm"
import { useAjax } from "@/Hook/useAjax"
import { EverydayListType, getPromoteDayListApi, getPromoteDayTotalApi } from "@/services/gameData/extensionData"
import React, { useEffect, useState } from "react"
import TableData from "../../components/TableData"
import columns12 from "./tableConfig"
import { getGameChoiceParentListType1Api } from "@/services/gameData"
import moment from "moment"
import RechargeTrend from "@/components/rechargeTrend"

const Everyday: React.FC = () => {

    /***************************/
    const [queryForm, setQueryForm] = useState<EverydayListType>({ pageNum: 1, pageSize: 20, beginDay: moment().format('YYYY-MM-DD'), endDay: moment().format('YYYY-MM-DD'), sourceSystem: 'ZX_ONE' })
    const [gameType, setGameType] = useState<any>({})
    const [totalData, setTotalData] = useState<any[]>([])
    const [rtConfig, setRtConfig] = useState<{ accountId?: number, visible: boolean, agentId?: number, beginDate?: string, sourceSystem?: string }>({ visible: false })

    const getPromoteDayList = useAjax((params) => getPromoteDayListApi(params))
    const getPromoteDayTotal = useAjax((params) => getPromoteDayTotalApi(params))
    const getGameChoiceParentListType1 = useAjax(() => getGameChoiceParentListType1Api())
    /***************************/

    useEffect(() => {
        getPromoteDayTotal.run(queryForm).then(res => {
            res.id = 1
            res.dt = '总计'
            setTotalData([res])
        })
        getPromoteDayList.run(queryForm)
    }, [queryForm])

    useEffect(() => {
        getGameChoiceParentListType1.run().then(res => {
            let newType: any = {}
            res.forEach((item: { id: number, name: string }) => {
                newType[item.id] = item.name
            })
            setGameType(newType)
        })
    }, [])

    const rechargeTrendHandle = (data: any) => {
        if (data?.accountId) {
            setRtConfig({ visible: true, accountId: data?.accountId, agentId: data.agentId, beginDate: data.dt, sourceSystem: data.sourceSystem })
        }
    }

    return <div>
        <TableData
            leftChild={<QueryForm
                initialValues={{ sourceSystem: 'ZX_ONE', consumeDay: [moment(), moment()] }}
                onChange={(data: any) => {
                    const { pitcherId, costBeginDay, costEndDay, type, ...params } = data
                    console.log(params, pitcherId)
                    let newQueryForm = JSON.parse(JSON.stringify(queryForm))
                    newQueryForm.pageNum = 1
                    newQueryForm.sysUserId = pitcherId
                    newQueryForm.beginDay = costBeginDay
                    newQueryForm.endDay = costEndDay
                    newQueryForm.accountType = type
                    setQueryForm({ ...newQueryForm, ...params })
                }}
                isSource
                isAccountId
                isAgentId
                isType
                isConsumeDay={{}}
                isBGGameClassify
                // isCpName
                // isGameName
                isGameId
                // isGameType
                isSysUserId
            />}
            isZj
            totalData={totalData}
            scroll={{ x: 1000, y: 600 }}
            ajax={getPromoteDayList}
            fixed={{ left: 5, right: 1 }}
            dataSource={getPromoteDayList?.data?.records}
            total={getPromoteDayList?.data?.total}
            page={queryForm.pageNum}
            pageSize={queryForm.pageSize}
            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(gameType, rechargeTrendHandle)}
            configName={'推广每日数据'}
        />
        {rtConfig.visible && <RechargeTrend {...rtConfig} onClose={() => setRtConfig({ visible: false })} />}
    </div>
}

export default Everyday