import { useAjax } from "@/Hook/useAjax"
import { getRolePayRetainedApi, GetRolePayRetainedProps, getRolePayRetainedTotalApi } from "@/services/gsData"
import React, { useEffect, useState } from "react"
import moment from "moment"
import TablePro from "@/pages/gameDataStatistics/components/TablePro"
import QueryFormGS from "@/components/QueryForm/indexGs"
import columns12 from "./tableConfig"

/**
 * GS角色付费留存
 * @returns 
 */
const RolePayRetained: React.FC = () => {

    /***************************************/
    const [queryForm, setQueryForm] = useState<GetRolePayRetainedProps>({
        pageNum: 1,
        pageSize: 30,
        sourceSystem: 'ZX_ONE',
        dtBegin: moment().format('YYYY-MM-DD'),
        dtEnd: moment().format('YYYY-MM-DD')
    })
    const [totalData, setTotalData] = useState<any[]>([])

    const getRolePayRetainedTotal = useAjax((params) => getRolePayRetainedTotalApi(params))
    const getRolePayRetained = useAjax((params) => getRolePayRetainedApi(params))
    /***************************************/

    useEffect(() => {
        getRolePayRetained.run(queryForm)
        getRolePayRetainedTotal.run(queryForm).then((res: { id: number; gsName: string, beginDay?: string }) => {
            res.id = 1
            res.gsName = '总计'
            res.beginDay = queryForm.dtBegin || moment().subtract(366, 'days').format('YYYY-MM-DD')
            setTotalData([res])
        })
    }, [queryForm])

    return <div>
        <TablePro
            leftChild={<QueryFormGS
                initialValues={{ sourceSystem: 'ZX_ONE', roleSource: 2, rechargeDay: [moment(), moment()] }}
                isSource
                isGsId
                rechargeDay={{ placeholder: ['指派日期开始', '指派日期结束'] }}
                onChange={(data: any) => {
                    console.log(data)
                    const { rechargeDay, gsId, ...params } = data
                    let newQueryForm = JSON.parse(JSON.stringify(queryForm))
                    newQueryForm.pageNum = 1
                    newQueryForm.gsIdList = gsId
                    if (rechargeDay && rechargeDay?.length === 2) {
                        newQueryForm['dtBegin'] = moment(rechargeDay[0]).format('YYYY-MM-DD')
                        newQueryForm['dtEnd'] = moment(rechargeDay[1]).format('YYYY-MM-DD')
                    } else {
                        delete newQueryForm['dtBegin']
                        delete newQueryForm['dtEnd']
                    }
                    setQueryForm({ ...newQueryForm, ...params })
                }}
            />}
            isZj
            totalData={totalData}
            config={columns12()}
            configName={'GS角色付费留存'}
            fixed={{ left: 4, right: 0 }}
            scroll={{ x: 1000, y: 620 }}
            title='GS角色付费留存'
            loading={getRolePayRetained.loading}
            ajax={getRolePayRetained}
            page={getRolePayRetained?.data?.current || 1}
            pageSize={getRolePayRetained?.data?.size || 20}
            total={getRolePayRetained?.data?.total || 0}
            dataSource={getRolePayRetained?.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 RolePayRetained