import { useReducer } from 'react'
import { channel_analysis_select,pitcher_channel,channel_analysis_select_daily,ad_rank,bookOverview, bookRank, bookTrend, bossSummary,
    getAccApi, channelActive, getUserApi, channelAgainOrderTrend, channelOrderTrend, channelOverview, channelStatChannel, imageRank, 
    Params, pitcherOverview, pitcherTrend, getRechargeHisApi, getRechargeHisYaerApi, channelOrderTrendPay, setPayApi, recoveryQueryapi, getOsoRoiApi } from '@/services/dataStatistics/dataCenter'
import { useAjax } from '@/Hook/useAjax'
type State = {
    tableDate?: string,//table搜索单日期值
    channel?: string,//table搜索公众号名称
    channels?: string[],// 多选公众号
    tableId?: string,//table搜索公众号ID
    date_range?: string,//table搜索公众号n天
    start?: string,//开始时间
    end?: string,//结束时间
    start15?: string,//开始时间
    end15?: string,//结束时间
    cost_time_start?: string,//消耗开始时间
    cost_time_end?: string,//结束时间
    order_time_start?: string,//订单开始时间
    order_time_end?: string,//结束时间
    cost_year?: string, // 消耗所在年（格式：2021）
    order_month?: string, // 订单所在月份  （格式：2021-11）
    pitcher?: string,//投手名称
    book?: any,//table搜索书名
    dataSource?: any[]//table数据
    loding?: boolean//请求
    order_by?: string//排序字段
    order?: 'asc' | 'desc',//升序降序
    location?: 'MP' | 'GDT',//投放位置老
    type?: 'MP' | 'GDT',//投放位置新
    targetKeys?: any[],//
    platform?:string,//书城
    selectedKeys?: any[],//
    tableValue?: '1' | '2',
    stage?: string,//期数
    tabKey?: string,
    st?:string,//充值开始
    et?:string,//充值结束
    is_video?: number,
    is_pitcher_self?: boolean,
}
export type Action = {
    type: 'tableDate' | 'channel'| 'channels' | 'isVideo' | 'tableId' | 'book' | 'date_range' | 'tableDateArr' | 'tableDateArr15' | 'isPitcherSelf' | 'depleteDateDateArr' | 'orderDateDateArr' | 'costYear' | 'orderMonth' | 'tabKey' | 'init' | 'setState' | 'dataSource',
    params?: State
}
export function reducer(state: State, action: Action) {
    let { type, params } = action
    let newState = JSON.parse(JSON.stringify(state))
    switch (type) {
        case 'tableDate':
            return { ...state, tableDate: params?.tableDate }
        case 'channel':
            return { ...state, channel: params?.channel }
        case 'costYear':
            return { ...state, cost_year: params?.cost_year }
        case 'orderMonth':
            return { ...state, order_month: params?.order_month }
        case 'isPitcherSelf':
            return { ...state, is_pitcher_self: params?.is_pitcher_self }
        case 'channels':
            return { ...state, channels: params?.channels }
        case 'isVideo':
            return { ...state, is_video: params?.is_video }
        case 'tableId':
            return { ...state, tableId: params?.tableId }
        case 'book':
            return { ...state, book: params?.book }
        case 'date_range':
            return { ...state, date_range: params?.date_range }
        case 'tableDateArr':
            return { ...state, start: params?.start, end: params?.end }
        case 'tableDateArr15':
            return { ...state, start15: params?.start15, end15: params?.end15 }
        case 'depleteDateDateArr':
            return { ...state, cost_time_start: params?.start, cost_time_end: params?.end }
        case 'orderDateDateArr':
            return { ...state, order_time_start: params?.start, order_time_end: params?.end }
        case 'dataSource':
            return { ...state, dataSource: params?.dataSource }
        case 'tabKey':
            return { ...state, tabKey: params?.tabKey }
        case 'setState':
            Object.keys(params as State).forEach((key: string) => {
                newState[key] = (params as State)[key]
            })
            return newState
        case 'init':
            return {}
        default:
            return state;
    }
}
const initData: State = {
    date_range:'7'
}
export default function useTableData() {
    const [state, dispatch] = useReducer(reducer, initData)
    /**广告投放详细数据 */
    const getChannelOverview = useAjax((params: Params) => channelOverview(params), { formatResult: true })
    /**今日消耗排行榜 */
    const getBookRank = useAjax((params: Params) => bookRank(params), { formatResult: true })
    /**公众号付费趋势表 */
    const getChannelOrderTrend = useAjax((params: Params) => channelOrderTrend(params), { formatResult: true })
    /**公众号付费趋势（含赔付金） */
    const getChannelOrderTrendPay = useAjax((params: Params) => channelOrderTrendPay(params), { formatResult: true })
    /** 公众号7日复重趋势*/
    const getChannelAgainOrderTrend = useAjax((params: Params) => channelAgainOrderTrend(params), { formatResult: true })
    /**公众号付费粉丝激活数据 */
    const getChannelActive = useAjax((params: Params) => channelActive(params), { formatResult: true })
    /**公众号汇总 */
    const getChannelStatChannel = useAjax((params: Params) => channelStatChannel(params), { formatResult: true })
    /**投手付费趋势 */
    const getPitcherTrend = useAjax((params: Params) => pitcherTrend(params), { formatResult: true })
    /**投手回收查询 */
    const getRecoveryQuery = useAjax((params: Params) => recoveryQueryapi(params), { formatResult: true })
    /**公众号回收查询 */
    const getOsoRoi = useAjax((params: Params) => getOsoRoiApi(params), { formatResult: true })
     /**投手总数据 */
    const getPitcherOverview = useAjax((params: Params) => pitcherOverview(params), { formatResult: true })
     /**充值历史 */
    const getRechargeHis = useAjax((params: Params) => getRechargeHisApi(params), { formatResult: true })
     /**充值历史（年） */
    const getRechargeHisYaer = useAjax((params: Params) => getRechargeHisYaerApi(params), { formatResult: true })
    /**小说付费趋势 */
    const getBookTrend = useAjax((params: Params) => bookTrend(params), { formatResult: true })
    /**小说总数据 */
    /**小说付总数据 */
    const getBookOverview = useAjax((params: Params) => bookOverview(params), { formatResult: true })
    /**整体概况 */
    const getBossSummary = useAjax((params: Params) => bossSummary(params), { formatResult: true })
    /**素材库 */
    const getImageRank = useAjax((params: Params) => imageRank(params), { formatResult: true })
    /**素材消耗排行榜 */
    const getAdrank = useAjax((params: Params) => ad_rank(params), { formatResult: true })
    /** 搜索公众号 */
    const getAccAjax = useAjax((params: Params) => getAccApi(params), { formatResult: true })
    /** 获取拥有用户 */
    const getUserAjax = useAjax((params: Params) => getUserApi(params), { formatResult: true })
    /** 投手投放号 */
    const getPitcherChannel = useAjax((params: Params) => pitcher_channel(params), { formatResult: true })
    /** 公众号用户充值查询 */
    const getChannelAnalysis = useAjax((params: Params) => channel_analysis_select(params), { formatResult: true })  
    /** 公众号用户充值查询详情 */
    const getChannelAnalysisDetails = useAjax((params: Params) => channel_analysis_select_daily(params), { formatResult: true })  
    /** 设置赔付金 */
    const setPay = useAjax((params: { date: string, channel: string, pay: number }) => setPayApi(params), { formatResult: true })

    return {
        state,
        dispatch,
        getChannelOverview,
        getChannelOrderTrend,
        getChannelOrderTrendPay,
        getChannelAgainOrderTrend,
        getChannelActive,
        getChannelStatChannel,
        getPitcherTrend,
        getRecoveryQuery,
        getBookRank,
        getBookTrend,
        getBookOverview,
        getPitcherOverview,
        getRechargeHis,
        getBossSummary,
        getImageRank,
        getAdrank,
        getAccAjax,
        getRechargeHisYaer,
        getUserAjax,
        getPitcherChannel,
        getChannelAnalysis,
        getChannelAnalysisDetails,
        setPay,
        getOsoRoi
    }
}
