import { useEffect, useReducer } from 'react'
import { useAjax } from '@/Hook/useAjax'
import { getAdvancedList, getAserviceList, getAtemplateList, getInteractList, getUserList, reviewAdvanced, reviewAservice, reviewAtemplate, reviewInteract, seeAdvanced, seeAservice, seeAtemplate, seeInteract } from '@/services/operating/examine'
type State = {
    userList?: any[]//公众号列表
    mpId?: number | string//选中的公众号
}
const initData: State = {
    userList: [],
    mpId: ''
}
export type Action = {
    type: 'set',
    params: State
}
export function reducer(state: State, action: Action) {
    let { type, params } = action
    let newState = JSON.parse(JSON.stringify(state))
    switch (type) {
        case 'set':
            Object.keys(params).forEach((key) => {
                newState[key] = params[key]
            })
            return newState
        default:
            return state;
    }
}

export default function useWxGroupList() {
    const [state, dispatch]: [State, React.Dispatch<Action>] = useReducer(reducer, initData)
    /**获取公众列表*/
    const ajaxUserList = useAjax(() => getUserList())
    /**==========================群发============================*/
    const advancedList = useAjax((params) => getAdvancedList(params))
    const putReviewAdvanced = useAjax((params) => reviewAdvanced(params))
    const lookAdvanced = useAjax((params) => seeAdvanced(params))
    /**==========================客服============================*/
    const aserviceList = useAjax((params) => getAserviceList(params))
    const putReviewAservice = useAjax((params) => reviewAservice(params))
    const lookAservice = useAjax((params) => seeAservice(params))
    /**==========================模板============================*/
    const atemplateList = useAjax((params) => getAtemplateList(params))
    const putReviewAtemplate = useAjax((params) => reviewAtemplate(params))
    const lookAtemplate = useAjax((params) => seeAtemplate(params))
    /**==========================立即/延迟============================*/
    const interactList = useAjax((params) => getInteractList(params))
    const putReviewInteract = useAjax((params) => reviewInteract(params))
    const lookInteract = useAjax((params) => seeInteract(params))
    /**设置公众号列表*/
    // useEffect(() => {
    //     if (state?.userList?.length === 0 && sessionStorage.getItem('Admin-Token')) {
    //         ajaxUserList.run().then((data: any[]) => {
    //             if (data?.length > 0) {
    //                 dispatch({ type: 'set', params: { userList: data } })
    //             }
    //         })
    //     }
    // }, [state.userList])
    return {
        state,
        dispatch,
        ajaxUserList,
        advancedList,
        putReviewAdvanced,
        putReviewAservice,
        putReviewAtemplate,
        putReviewInteract,
        lookAdvanced,
        aserviceList,
        lookAservice,
        atemplateList,
        lookAtemplate,
        interactList,
        lookInteract
    }
}
