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

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

    /************************************/
    const [queryForm, setQueryForm] = useState<PromoteTotalProps>({
        pageNum: 1,
        pageSize: 20,
        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 getPromoteTotalList = useAjax((params) => getPromoteTotalListApi(params))
    const getGameChoiceParentListType1 = useAjax(() => getGameChoiceParentListType1Api())
    /************************************/


    useEffect(() => {
        getPromoteTotalList.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)
        })
    }, [])

    return <div>

        <TableData
            leftChild={<QueryForm
                initialValues={{ sourceSystem: 'ZX_ONE', consumeDay: [moment(), moment()], rechargeDay: [moment(), moment()] }}
                onChange={(data: any) => {
                    const { pitcherId, rechargeDay, ...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
                    setQueryForm({ ...newQueryForm, ...params })
                }}
                isSource
                isAccountId
                isAgentId
                isConsumeDay={{}}
                isBGGameClassify
                // isCpName
                // isGameName
                isGameId
                // isGameType
                rechargeDay={{}}
                isSysUserId
            />}
            scroll={{ x: 1000, y: 600 }}
            ajax={getPromoteTotalList}
            dataSource={getPromoteTotalList?.data?.records}
            total={getPromoteTotalList?.data?.total}
            page={queryForm.pageNum}
            pageSize={queryForm.pageSize}
            fixed={{ left: 5, right: 0 }}
            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)}
            configName={'推广总数据'}
        />
    </div>
}

export default Total