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 { getByteAppTrendLtvListApi, getByteAppTrendLtvTotalApi, getByteAppTrendRetainedListApi, getByteAppTrendRetainedTotalApi, GetByteAppTrendRoiListApi } from "@/services/iaaData";

/**
 * 变现趋势留存
 * @returns 
 */
const AppCashTrendRetained: 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'),
        retainedType: 'new_user'
    })
    const [totalData, setTotalData] = useState<any[]>([])

    const getByteAppTrendRetainedList = useAjax((params) => getByteAppTrendRetainedListApi(params))
    const getByteAppTrendRetainedTotal = useAjax((params) => getByteAppTrendRetainedTotalApi(params))
    /****************************************/

    useEffect(() => {
        if (initialState?.iaaApp) {
            const [appId, productType] = initialState.iaaApp.split('||')
            getByteAppTrendRetainedList.run({ ...queryForm, appId, productType })
            getByteAppTrendRetainedTotal.run({ ...queryForm, appId, 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: '总计' }])
                }
            })
        }

    }, [queryForm, initialState?.iaaApp])

    return <div>
        <TablePro
            leftChild={<QueryForm
                initialValues={{ day1: [moment(), moment()], retainedType: 'new_user' }}
                isRetainedType
                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={getByteAppTrendRetainedList.loading}
            ajax={getByteAppTrendRetainedList}
            page={getByteAppTrendRetainedList?.data?.data?.current || 1}
            pageSize={getByteAppTrendRetainedList?.data?.data?.size || 20}
            total={getByteAppTrendRetainedList?.data?.data?.total || 0}
            dataSource={getByteAppTrendRetainedList?.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 AppCashTrendRetained