import { Dispatch, useReducer } from 'react'
import { channe, daily, overview, Params } from '@/services/dataStatistics/dataCenter'
import { useAjax } from '@/Hook/useAjax'
type State = {
    /**radio值 */
    radioValue?: '1' | '2' | '3',
    /**折线图搜索天数 */
    LineDay?: '7' | '15',
    /**table切换值 */
    tableValue?: '1' | '2',
    /**table搜索日期值开始时间 */
    start?: string,
    /**table搜索日期值结束 */
    end?: string,
    /**table搜索天数 */
    date_range?: string,
    /**table搜索公众号值 */
    channel?: string,//
    /**个人概览 */
    overview?: {
        /**累计消耗*/
        cost: number,
        /**累计充值 */
        amount: number,
        /** 综合回本率*/
        roi: number,
        /**总号数 */
        channel_count: number,
        /** 在投号数*/
        on_channel_count: number,
        /**停投号数 */
        off_channel_count: number,
        /**本月消耗 */
        this_month_cost: number,
        /** 本月充值*/
        this_month_amount: number,
        /** 本月回本率*/
        this_month_roi: number,
        /** 上月消耗*/
        last_month_cost: number,
        /**上月充值 */
        last_month_amount: number,
        /**上月30天回本率 */
        last_month_roi: number,
        /** 上月至今回本率*/
        last_month_far_roi: number,
    },
    LineData1?: {
        [key: string]: any;
        legendName: string;
    }[],
    LineData2?: {
        [key: string]: any;
        legendName: string;
    }[],
    LineData3?: {
        [key: string]: any;
        legendName: string;
    }[],
    tableData?: any[],
    order_by?:string//排序字段
    order?:'asc'|'desc'//升序降序
}
export type Action = {
    type: 'radioChange' | 'LineData' | 'tableChange' | 'tableDate' | 'tableAppid' | 'LineDay' | 'tableDay' | 'setState',
    params?: State
}
export function reducer(state: State, action: Action) {
    let { type, params } = action
    let newState = JSON.parse(JSON.stringify(state))
    switch (type) {
        case 'radioChange':
        // return { ...state, radioValue: params?.radioValue }
        case 'tableChange':
        // return { ...state, tableValue: params?.tableValue }
        case 'tableDate':
        // return { ...state, tableDate: params?.tableDate }
        case 'tableAppid':
        // return { ...state, tableAppid: params?.tableAppid }
        case 'tableDay':
        // return { ...state, tableDay: params?.tableDay }
        case 'LineDay':
        // return { ...state, LineDay: params?.LineDay }
        case 'setState':
            Object.keys(params as State).forEach((key: string) => {
                newState[key] = (params as State)[key]
            })
            return newState
        // case 'LineData':
        //     return { ...state, LineData: params?.LineData }
        default:
            return state;
    }
}
const initData: State = {
    radioValue: '1',
    tableValue: '1',
    date_range: '7',
    LineData1: [],
    LineData2: [],
    LineData3: [],
}
export default function useWxGroupList() {
    const [state, dispatch]: [state: State, dispatch: Dispatch<Action>] = useReducer(reducer, initData)
    /**个人预览*/
    const getOverview = useAjax((params: Params) => overview(params),{ formatResult: true })
    /**日期汇总*/
    const getDaily = useAjax((params: Params) => daily(params),{ formatResult: true })
    /**公众号汇总*/
    const getChanne = useAjax((params: Params) => channe(params),{ formatResult: true })
    return {
        state,
        dispatch,
        getOverview,
        getDaily,
        getChanne,
    }
}
