import { getCommentsList } from '@/api/comments/comments';
import { REPLY_STATUS, COMMENT_STATUS } from '@/types/comments/constant';
import { Records, CommentsQuery } from '@/types/comments/type';
import { onMounted, ref } from 'vue';

export function useCommentsList() {
  const commentsList = ref<Records[]>([]);
  const pageNumber = ref<number>(1);
  const pageSize = ref<number>(10);
  const totalPage = ref<number>();
  const totalRow = ref<number>();

  const listFilter = ref({
    authStatus: COMMENT_STATUS.ALL,
    replyStatus: REPLY_STATUS.ALL,
  });

  const getList = () => {
    const params: CommentsQuery = {
      pageNumber: pageNumber.value,
      pageSize: pageSize.value,
    };
    if (listFilter.value.authStatus !== COMMENT_STATUS.ALL) {
      //若传1，则拉取审核通过的；若传0，则拉取审核未通过的和未审核的
      params.queryParam = {
        isApproved: listFilter.value.authStatus === COMMENT_STATUS.PASSED ? 1 : 0,
      };
    }
    if (listFilter.value.replyStatus !== REPLY_STATUS.ALL) {
      params.queryParam = {
        isReplied: listFilter.value.replyStatus,
      };
    }
    getCommentsList(params).then((res) => {
      commentsList.value = res.records;
      // 若有回复，将回复内容前面加上“回复:”
      commentsList.value.forEach((item) => {
        item.reply = '回复: ' + item.reply;
      });
      totalPage.value = res.totalPage;
      totalRow.value = res.totalRow;
    });
  };

  onMounted(() => {
    getList();
  });

  return {
    commentsList,
    pageNumber,
    pageSize,
    totalPage,
    totalRow,
    getList,
    listFilter,
  };
}

export default useCommentsList;
