import React, { useEffect, useState } from "react"
import DayAd from "./dayAd"
import columns12 from "./tableConfig"
import { getPresets } from "@/components/QueryForm/const"
import TableData from "../../components/TableData"
import QueryForm from "@/components/QueryForm"
import { PromotionDataDay, getPromotionDataTencentListApi, getPromotionDataTencentTotalApi } from "@/services/gameData/adlist"
import { useAjax } from "@/Hook/useAjax"
import moment from "moment"



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

    /***************************************/
    const [queryForm, setQueryForm] = useState<PromotionDataDay>({ pageNum: 1, pageSize: 100, sourceSystem: 'ZX_ONE', costBeginDate: moment().format('YYYY-MM-DD'), costEndDate: moment().format('YYYY-MM-DD') })
    const [totalData, setTotalData] = useState<any[]>([])
    const [visible, setVisible] = useState<boolean>(false)
    const [promotionId, setPromotionId] = useState<number>()
    const [adName, setAdName] = useState<string>('')

    const getPromotionDataTencentList = useAjax((params) => getPromotionDataTencentListApi(params))
    const getPromotionDataTencentTotal = useAjax((params) => getPromotionDataTencentTotalApi(params))
    /***************************************/

    useEffect(() => {
        getPromotionDataTencentList.run(queryForm)
        getPromotionDataTencentTotal.run(queryForm).then(res => {
            res.id = 1
            res.accountName = '总计'
            setTotalData([res])
        })
    }, [queryForm])

    const dayHandle = (data: any) => {
        setVisible(true)
        setAdName(data.promotionName)
        setPromotionId(data.promotionId)
    }

    return <div>
        <TableData
            leftChild={<QueryForm
                initialValues={{ sourceSystem: 'ZX_ONE', consumeDay: [moment(), moment()] }}
                onChange={(data: any) => {
                    const { type, gameClassify, costBeginDay, costEndDay, sysUserName, ...params } = data
                    let newQueryForm = JSON.parse(JSON.stringify(queryForm))
                    newQueryForm.pageNum = 1
                    newQueryForm.pitcherName = sysUserName
                    newQueryForm.accountType = type
                    newQueryForm.classify = gameClassify
                    if (costBeginDay && costEndDay) {
                        newQueryForm.costBeginDate = costBeginDay
                        newQueryForm.costEndDate = costEndDay
                    } else {
                        delete newQueryForm.costBeginDate
                        delete newQueryForm.costEndDate
                    }

                    setQueryForm({ ...newQueryForm, ...params })
                }}
                isSource
                isAccountId
                isAccount
                isType
                isAgentId
                isBGGameClassify
                isConsumeDay={{ ranges: getPresets() }}
                isCpName
                isGameId
                isSysUserId
                isProjectId
                isProjectName
                isPromotionId
                isPromotionName
                isAdTXStatus
            />}
            isZj
            totalData={totalData}
            scroll={{ x: 1000, y: 600 }}
            ajax={getPromotionDataTencentList}
            fixed={{ left: 3, right: 1 }}
            dataSource={getPromotionDataTencentList?.data?.records?.map((item: any, index: number) => ({ ...item, id: Number(queryForm.pageNum.toString() + index.toString()) }))}
            total={getPromotionDataTencentList?.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(dayHandle)}
            configName={'腾讯广告监控'}
        />

        {visible && <DayAd adName={adName} visible={visible} onClose={() => { setVisible(false); setPromotionId(undefined) }} queryForm={queryForm} promotionId={promotionId} />}
    </div>
}

export default TencentMonitor