import { useCallback, useReducer } from 'react';
import {
  addFansTag,
  editFansTag,
  getFansTagList,
  selectFansTagList,
  delFansTag,
  FansTag,
  getFansList,
  getFansDetail,
  putFansTag,
  setFansRemark,
  putFansSync,
  putFansSyncAll,
  Fans,
  fansSearch,
  syncAllTag,
  catFansNum,
  getFansStatOnlineApi,
  getFansOrderListApi,
  syncAllBooksApi,
  isBoundAdAccount,
  createAdUserPackage,
} from '@/services/operating/fans';
import { useAjax } from '@/Hook/useAjax';
type State = {
  visible: boolean; //弹窗开启与否
};
export type Action = {
  type: 'show';
  params?: any;
};
export function reducer(state: State, action: Action) {
  let { type } = action;
  switch (type) {
    case 'show':
      return { ...state, visible: !state.visible };
    default:
      return state;
  }
}

export default function useWxGroupList() {
  const [state, dispatch] = useReducer(reducer, { visible: false });
  /**添加粉丝标签 */
  const add = useAjax((params: FansTag) => addFansTag(params), { msgNmae: '新建标签' });
  /**同步粉丝标签 */
  const tagSyncAll = useAjax((mpId: number) => syncAllTag(mpId), { msgNmae: '同步标签' });
  /**编辑粉丝标签 */
  const edit = useAjax((params: FansTag) => editFansTag(params), { msgNmae: '编辑标签' });
  /**获取粉丝标签全部列表 */
  const get = useAjax((params: { mpId: string }) => getFansTagList(params));
  /**查询粉丝标签 */
  const select = useAjax((params: FansTag) => selectFansTagList(params));
  /**删除粉丝标签 */
  const del = useAjax((params: { tagId: string }) => delFansTag(params), { msgNmae: '删除标签' });
  /**粉丝列表*/
  const fansList = useAjax((params: Fans) => getFansList(params));
  /**粉丝统计信息*/
  const getFansStatOnline = useAjax((mpId: number) => getFansStatOnlineApi(mpId));
  /**粉丝订单数据*/
  const getFansOrderList = useAjax(
    (props: {
      queryDate: string;
      platformId: number;
      mpId: string;
      openId: string;
      pageNum: number;
      pageSize: number;
      orderStatus?: 0 | 1 | 2 | -1;
    }) => getFansOrderListApi(props),
  );
  /**粉丝详情*/
  const fansDetail = useAjax((params: { fansId: string }) => getFansDetail(params));
  /**粉丝name搜索*/
  const searchFans = useAjax((params: { nickname: string; mpId: number }) => fansSearch(params), {
    debounceInterval: 100,
  });
  /**给粉丝打标签*/
  const fansTag = useAjax((params: Fans) => putFansTag(params));
  /**给粉丝设置备注*/
  const fansRemark = useAjax((params: any) => setFansRemark(params));
  /**重新同步指定粉丝的信息*/
  const fansSync = useAjax((params: { fansId: string }) => putFansSync(params));
  /**重新同步公众号所有粉丝信息*/
  const fansSyncAll = useAjax((params: { mpId: string }) => putFansSyncAll(params));
  /** 同步书城信息 */
  const syncAllBooks = useAjax((params: { mpId: number; platformId: number }) =>
    syncAllBooksApi(params),
  );
  /**显示关闭弹窗 */
  const visible = useCallback(() => {
    dispatch({ type: 'show' });
  }, []);
  /**条件查找粉丝活跃人数 */
  const catFans = useAjax((params) => catFansNum(params));
  /**是否绑定广告账户*/
  const is_boundAdAccount = useAjax((mpId)=>isBoundAdAccount(mpId))
  /**创建人群包*/
  const create_adUserPackage = useAjax((params)=>createAdUserPackage(params),{msgNmae:'创建'})
  return {
    state,
    get,
    add,
    del,
    edit,
    select,
    visible,
    fansList,
    fansDetail,
    fansTag,
    fansRemark,
    fansSync,
    fansSyncAll,
    searchFans,
    tagSyncAll,
    catFans,
    getFansStatOnline,
    getFansOrderList,
    syncAllBooks,
    is_boundAdAccount,
    create_adUserPackage
  };
}
