import { useAjax } from "@/Hook/useAjax"
import React, { useEffect, useState } from "react"
import moment from "moment"
import { PitcherGameDayProps, getPitcherGameDayListApi, getPitcherGameDaySumApi } from "@/services/gameData/pitcher"
import { getPresets } from "@/components/QueryForm/const"
import TableData, { version } from "../../components/TableData"
import QueryForm from "@/components/QueryForm"
import columns12 from "./tableConfig"


/**
 * 投手游戏每日数据
 * @returns 
 */
const EvertDayGame: React.FC = () => {

    /*****************************/
    const [queryForm, setQueryForm] = useState<PitcherGameDayProps>({
        pageNum: 1, pageSize: 40,
        sourceSystem: 'ZX_ONE',
        beginDate: moment().format('YYYY-MM-DD'), //subtract(30, 'd').
        endDate: moment().format('YYYY-MM-DD')
    })
    const [totalData, setTotalData] = useState<any[]>([])

    const getPitcherGameDayList = useAjax((params) => getPitcherGameDayListApi(params))
    const getPitcherGameDaySum = useAjax((params) => getPitcherGameDaySumApi(params))
    /*****************************/

    useEffect(() => {
        getPitcherGameDayList.run(queryForm)
        getPitcherGameDaySum.run(queryForm).then((res: { id: number; pitcher: string, beginDay?: string }) => {
            res.id = 1
            res.pitcher = '总计'
            res.beginDay = queryForm.beginDate || moment().subtract(366, 'days').format('YYYY-MM-DD')
            setTotalData([res])
        })
    }, [queryForm])


    return <div>
        <TableData
            leftChild={<QueryForm
                initialValues={{ consumeDay: [moment(), moment()], sourceSystem: 'ZX_ONE' }}
                onChange={(data: any) => {
                    console.log(data)
                    const { costBeginDay, costEndDay, gameClassify, cpName, ...params } = data
                    let newQueryForm = JSON.parse(JSON.stringify(queryForm))
                    newQueryForm.pageNum = 1
                    if (costBeginDay && costEndDay) {
                        newQueryForm.beginDate = costBeginDay
                        newQueryForm.endDate = costEndDay
                    } else {
                        delete newQueryForm.beginDate
                        delete newQueryForm.endDate
                    }
                    queryForm['gameType'] = gameClassify
                    queryForm['gameCp'] = cpName
                    setQueryForm({ ...newQueryForm, ...params })
                }}
                isSource
                isConsumeDay={{ ranges: getPresets() }}
                isSysUserIds
                isGameIds
                isGameDimension
                isCpName
            />}
            isZj
            totalData={totalData}
            scroll={{ x: 1000, y: 600 }}
            ajax={getPitcherGameDayList}
            fixed={{ left: 2, right: 1 }}
            dataSource={getPitcherGameDayList?.data?.records?.map((item: any, index: number) => ({ ...item, id: Number(queryForm.pageNum.toString() + index.toString()) }))}
            total={getPitcherGameDayList?.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={`投手游戏每日数据`}
            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()}
            configName={'投手游戏每日数据'}
        />
    </div>
}

export default EvertDayGame