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

/**
 * 应用变现趋势-ROI
 * @returns 
 */
const AppCashTrendRoi: React.FC = () => {

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

    const getByteAppTrendRoiList = useAjax((params) => getByteAppTrendRoiListApi(params))
    const getByteAppTrendRoiTotal = useAjax((params) => getByteAppTrendRoiTotalApi(params))
    /****************************************/

    useEffect(() => {
        if (initialState?.iaaApp?.length && initialState?.productType) {
            getByteAppTrendRoiList.run({ ...queryForm, appId: initialState.iaaApp, productType: initialState.productType })
            getByteAppTrendRoiTotal.run({ ...queryForm, appId: initialState.iaaApp, productType: initialState.productType }).then((res: { data: { id: number; appName: string, costDayBegin: string } }) => {
                if (res?.data) {
                    let data = res?.data
                    data.id = 1
                    data.costDayBegin = queryForm.costDayBegin || moment().format('YYYY-MM-DD')
                    data.appName = '总计'
                    setTotalData([data])
                } else {
                    setTotalData([{ id: 1, appName: '总计' }])
                }
            })
        } else {
            setTotalData([{ id: 1, appName: '总计' }])
            getByteAppTrendRoiList.mutate({ data: undefined })
        }
    }, [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={'头条应用变现趋势ROI'}
            fixed={{ left: 4, right: 0 }}
            scroll={{ x: 1000, y: 620 }}
            title='应用变现趋势-ROI'
            loading={getByteAppTrendRoiList.loading}
            ajax={getByteAppTrendRoiList}
            page={getByteAppTrendRoiList?.data?.data?.current || 1}
            pageSize={getByteAppTrendRoiList?.data?.data?.size || 20}
            total={getByteAppTrendRoiList?.data?.data?.total || 0}
            dataSource={getByteAppTrendRoiList?.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 AppCashTrendRoi