import { getNovelAppListApi, getNovelAppTotalApi, GetNovelPutUserListProps } from "@/services/iaaData";
import React, { useEffect, useState } from "react"
import { useModel } from "umi";
import moment from "moment";
import { useAjax } from "@/Hook/useAjax";
import TablePro from "@/components/TablePro";
import columns12 from "./tableConfig";
import QueryFormNovel from "@/components/QueryForm/queryFormNovel";

/**
 * 应用每日数据
 * @returns 
 */
const AppEveryDayData: React.FC<{ configName?: string }> = ({ configName }) => {

    /****************************************/
    const { initialState } = useModel('@@initialState');
    const [queryForm, setQueryForm] = useState<GetNovelPutUserListProps>({
        pageNum: 1,
        pageSize: 30,
        costDayBegin: moment().format('YYYY-MM-DD'),
        costDayEnd: moment().format('YYYY-MM-DD'),
    })
    const [totalData, setTotalData] = useState<any[]>([])

    const getNovelAppList = useAjax((params) => getNovelAppListApi(params))
    const getNovelAppTotal = useAjax((params) => getNovelAppTotalApi(params))
    /****************************************/

    useEffect(() => {
        if (initialState?.iaaApp?.length && initialState?.productType) {
            getNovelAppList.run({ ...queryForm, appId: initialState.iaaApp, productType: initialState.productType })
            getNovelAppTotal.run({ ...queryForm, appId: initialState.iaaApp, productType: initialState.productType }).then((res: { data: { id: number; day: string } }) => {
                if (res?.data) {
                    let data = res?.data
                    data.id = 1
                    data.day = '总计'
                    setTotalData([data])
                } else {
                    setTotalData([{ id: 1, day: '总计' }])
                }
            })
        } else {
            setTotalData([{ id: 1, day: '总计' }])
            getNovelAppList.mutate({ data: undefined })
        }
    }, [queryForm, initialState?.iaaApp, initialState?.productType])

    return <div>
        <TablePro
            leftChild={<QueryFormNovel
                initialValues={{ day1: [moment(), moment()] }}
                day1={{ placeholder: ['消耗日期开始', '消耗日期结束'] }}
                onChange={(data: any) => {
                    console.log(data)
                    const { day1, ...params } = data
                    let newQueryForm = JSON.parse(JSON.stringify(queryForm))
                    newQueryForm.pageNum = 1
                    if (day1 && day1?.length === 2) {
                        newQueryForm['costDayBegin'] = moment(day1[0]).format('YYYY-MM-DD')
                        newQueryForm['costDayEnd'] = moment(day1[1]).format('YYYY-MM-DD')
                    } else {
                        delete newQueryForm['costDayBegin']
                        delete newQueryForm['costDayEnd']
                    }
                    setQueryForm({ ...newQueryForm, ...params })
                }}
            />}
            isZj
            totalData={totalData}
            config={columns12()}
            configName={configName || '小说腾讯应用每日数据'}
            fixed={{ left: 4, right: 0 }}
            scroll={{ x: 1000, y: 620 }}
            title='应用每日数据'
            loading={getNovelAppList.loading}
            ajax={getNovelAppList}
            page={getNovelAppList?.data?.data?.current || 1}
            pageSize={getNovelAppList?.data?.data?.size || 20}
            total={getNovelAppList?.data?.data?.total || 0}
            dataSource={getNovelAppList?.data?.data?.records?.map((item: any, index: number) => ({ ...item, id: Number(queryForm.pageNum.toString() + (index + '')) }))}
            onChange={(pagination: any, _: any, sortData: any) => {
                let { current, pageSize } = pagination
                let newQueryForm = JSON.parse(JSON.stringify(queryForm))
                if (sortData && sortData?.order) {
                    newQueryForm['sortAsc'] = sortData?.order === 'ascend' ? true : false
                    newQueryForm['sortFiled'] = sortData?.field
                } else {
                    delete newQueryForm['sortAsc']
                    delete newQueryForm['sortFiled']
                }
                newQueryForm.pageNum = current || newQueryForm.pageNum
                newQueryForm.pageSize = pageSize || newQueryForm.pageSize
                setQueryForm({ ...newQueryForm })
            }}
        />
    </div>
}

export default AppEveryDayData