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, { version } from "../../components/TableData"
import columns12 from "./tableConfig"
import moment from "moment"
import RechargeTrend from "@/components/rechargeTrend"
import { getPresets } from "@/components/QueryForm/const"

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

    /***************************/
    const [queryForm, setQueryForm] = useState<EverydayListType>({ pageNum: 1, pageSize: 50, beginDay: moment().format('YYYY-MM-DD'), endDay: moment().format('YYYY-MM-DD'), sourceSystem: 'ZX_ONE' })
    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))
    /***************************/

    useEffect(() => {
        getPromoteDayTotal.run(queryForm).then((res: { id: number; dt: string; beginDay: string | undefined }) => {
            res.id = 1
            res.dt = '总计'
            res.beginDay = queryForm.beginDay
            setTotalData([res])
        })
        getPromoteDayList.run(queryForm)
    }, [queryForm])

    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={{ ranges: getPresets() }}
                isBGGameClassify
                isGameId
                isSysUserId
            />}
            isZj
            totalData={totalData}
            scroll={{ x: 1000, y: 600 }}
            ajax={getPromoteDayList}
            fixed={{ left: 5, right: 2 }}
            dataSource={getPromoteDayList?.data?.records}
            total={getPromoteDayList?.data?.total}
            page={queryForm.pageNum}
            pageSize={queryForm.pageSize}
            sortData={{
                field: queryForm?.sortFiled,
                order: queryForm?.sortType === 'asc' ? 'ascend' : 'descend'
            }}
            estimatedRowHeight={() => {
                let mySelectFieldData = localStorage.getItem(`myAdFieldConfig${version}_推广每日数据`)
                if (mySelectFieldData) {
                    let newSelectFieldData = JSON.parse(mySelectFieldData).date_field
                    return newSelectFieldData?.length * 20.5 || 20.5 * 5
                } else {
                    return 20.5 * 5
                }
            }}
            title='推广每日数据(T+1)'
            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(rechargeTrendHandle)}
            configName={'推广每日数据'}
        />
        {rtConfig.visible && <RechargeTrend {...rtConfig} onClose={() => setRtConfig({ visible: false })} />}
    </div>
}

export default Everyday