import {getReceiveOrderList} from '@apis';
import {PURCHASE_PATH} from '@routes';
import {menuStore} from '@stores';
import {useQuery} from '@tanstack/react-query';
import {Dayjs} from 'dayjs';
import {find} from 'lodash-es';
import {useMemo} from 'react';
import {useStore} from 'zustand';

export function useMoreLink() {
  const pages = useStore(menuStore, state => state.menus);
  const data = useMemo(
    function () {
      return find(pages, val => val.url === PURCHASE_PATH);
    },
    [pages],
  );

  function onMoreClick() {
    if (!data) return;

    (parent as any).tabAddDispatch({
      key: data.id,
      url: data.url,
      label: data.name,
    });
  }

  return onMoreClick;
}

export function useQueryList(date: Dayjs) {
  return useQuery({
    queryKey: [getReceiveOrderList.name, 'main', date],
    async queryFn({signal}) {
      const data = await getReceiveOrderList(
        {
          startTime: date.format('YYYY-MM-DD'),
          endTime: date.add(1, 'day').format('YYYY-MM-DD'),
          orderCode: '',
          materialName: '',
          wllbCode: '',
          supplierId: '',
          page: '1',
          limit: '8',
        },
        signal,
      );

      if (data.msg === '200') return data.data.list;

      return [];
    },
    initialData: [],
  });
}
