import QueryForm from "@/components/QueryForm";
import TablePro from "@/components/TablePro";
import { useAjax } from "@/Hook/useAjax";
import { getAppTxEveryDayListApi, GetAppTxEveryDayListProps, getAppTxEveryDayTotalApi } from "@/services/iaaData";
import React, { useEffect, useState } from "react"
import { useModel } from "umi";
import moment from "moment";
import columns12 from "./tableConfig";

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

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

    const getAppTxEveryDayList = useAjax((params) => getAppTxEveryDayListApi(params))
    const getAppTxEveryDayTotal = useAjax((params) => getAppTxEveryDayTotalApi(params))
    /****************************************/

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

    }, [queryForm, initialState?.iaaApp, initialState?.productType])

    return <div>
        <TablePro
            leftChild={<QueryForm
                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={'应用每日数据'}
            fixed={{ left: 4, right: 0 }}
            scroll={{ x: 1000, y: 620 }}
            title='应用每日数据'
            loading={getAppTxEveryDayList.loading}
            ajax={getAppTxEveryDayList}
            page={getAppTxEveryDayList?.data?.data?.current || 1}
            pageSize={getAppTxEveryDayList?.data?.data?.size || 20}
            total={getAppTxEveryDayList?.data?.data?.total || 0}
            dataSource={getAppTxEveryDayList?.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