import { useAjax } from "@/Hook/useAjax"
import { PitcherTotalProps, getPitcherTotalListApi, getPitcherTotalSumApi } from "@/services/gameData/pitcher"
import React, { useEffect, useState } from "react"
import moment from "moment"
import columns12 from "./tableConfig"
import QueryForm from "@/components/QueryForm"
import TableData from "../../components/TableData"
import { getPresets } from "@/components/QueryForm/const"

/**
 * 投手总数据
 * @returns 
 */
const Total: React.FC = () => {

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

    const getPitcherTotalList = useAjax((params) => getPitcherTotalListApi(params))
    const getPitcherTotalSum = useAjax((params) => getPitcherTotalSumApi(params))
    /*****************************/

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


    return <div>
        <TableData
            leftChild={<QueryForm
                initialValues={{ rechargeDay: [moment(), moment()], consumeDay: [moment(), moment()], sourceSystem: 'ZX_ONE' }}
                onChange={(data: any) => {
                    console.log(data)
                    const { costBeginDay, costEndDay, rechargeDay, ...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
                    }
                
                    if (rechargeDay && rechargeDay?.length === 2) {
                        newQueryForm['amountBeginDate'] = moment(rechargeDay[0]).format('YYYY-MM-DD')
                        newQueryForm['amountEndDate'] = moment(rechargeDay[1]).format('YYYY-MM-DD')
                    } else {
                        delete newQueryForm['amountBeginDate']
                        delete newQueryForm['amountEndDate']
                    }
                    setQueryForm({ ...newQueryForm, ...params })
                }}
                isSource
                isConsumeDay={{ ranges: getPresets() }}
                rechargeDay={{ ranges: getPresets() }}
                isSysUserIds
            />}
            isZj
            totalData={totalData}
            scroll={{ x: 1000, y: 600 }}
            ajax={getPitcherTotalList}
            fixed={{ left: 2, right: 1 }}
            dataSource={getPitcherTotalList?.data?.records?.map((item: any, index: number) => ({ ...item, id: Number(queryForm.pageNum.toString() + index.toString()) }))}
            total={getPitcherTotalList?.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()}
            configName={'投手总数据'}
        />
    </div>
}

export default Total