import React, { useEffect, useState } from "react"
import TableData from "../../components/TableData"
import columns12 from './tableConfig'
import { useAjax } from "@/Hook/useAjax"
import { PromoteTotalProps, getPromoteTotalApi, getPromoteTotalListApi } from "@/services/gameData/extensionData"
import QueryForm from "@/components/QueryForm"
import moment from "moment"
import { getGameChoiceParentListType1Api } from "@/services/gameData"
import { getPresets } from "@/components/QueryForm/const"

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

    /************************************/
    const [queryForm, setQueryForm] = useState<PromoteTotalProps>({
        pageNum: 1,
        pageSize: 100,
        sourceSystem: 'ZX_ONE',
        costBeginDay: moment().format('YYYY-MM-DD'),
        costEndDay: moment().format('YYYY-MM-DD'),
        rechargeBeginDay: moment().format('YYYY-MM-DD'),
        rechargeEndDay: moment().format('YYYY-MM-DD'),
    })
    const [gameType, setGameType] = useState<any>({})
    const [totalData, setTotalData] = useState<any[]>([])

    const getPromoteTotalList = useAjax((params) => getPromoteTotalListApi(params))
    const getPromoteTotal = useAjax((params) => getPromoteTotalApi(params))
    const getGameChoiceParentListType1 = useAjax(() => getGameChoiceParentListType1Api())
    /************************************/


    useEffect(() => {
        getPromoteTotal.run(queryForm).then((res: { id: number; accountId: string }) => {
            res.id = 1
            res.accountId = '总计'
            setTotalData([res])
        })
        getPromoteTotalList.run(queryForm)
    }, [queryForm])

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

    return <div>

        <TableData
            leftChild={<QueryForm
                initialValues={{ sourceSystem: 'ZX_ONE', consumeDay: [moment(), moment()], rechargeDay: [moment(), moment()] }}
                onChange={(data: any) => {
                    const { pitcherId, rechargeDay, type, ...params } = data
                    let newQueryForm = JSON.parse(JSON.stringify(queryForm))
                    newQueryForm.pageNum = 1
                    if (rechargeDay && rechargeDay?.length === 2) {
                        newQueryForm['rechargeBeginDay'] = moment(rechargeDay[0]).format('YYYY-MM-DD')
                        newQueryForm['rechargeEndDay'] = moment(rechargeDay[1]).format('YYYY-MM-DD')
                    } else {
                        delete newQueryForm['rechargeBeginDay']
                        delete newQueryForm['rechargeEndDay']
                    }
                    newQueryForm.sysUserId = pitcherId
                    newQueryForm.accountType = type
                    setQueryForm({ ...newQueryForm, ...params })
                }}
                isSource
                isAccountId
                isAgentId
                isConsumeDay={{ ranges: getPresets() }}
                isBGGameClassify
                isType
                // isCpName
                // isGameName
                isGameId
                // isGameType
                rechargeDay={{ ranges: getPresets() }}
                isSysUserId
            />}
            isZj
            totalData={totalData}
            scroll={{ x: 1000, y: 600 }}
            ajax={getPromoteTotalList}
            dataSource={getPromoteTotalList?.data?.records}
            total={getPromoteTotalList?.data?.total}
            page={queryForm.pageNum}
            pageSize={queryForm.pageSize}
            sortData={{
                field: queryForm?.sortFiled,
                order: queryForm?.sortType === 'asc' ? 'ascend' : 'descend'
            }}
            fixed={{ left: 5, right: 0 }}
            title='推广总数据(T+1)'
            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)}
            configName={'推广总数据'}
        />
    </div>
}

export default Total