// import { DEFAULT_PAGE_SIZE } from '@/types/common/constants';
import { DEFAULT_PAGE_SIZE } from '@/constant/pagination';
import { QueryPersonGroupPageParams, PersonGroupListItem } from '@/types/person-group/type';
import { queryUserGroupPage } from '@/api/system/person-group';
import { ref } from 'vue';
import { cloneDeep } from 'lodash-es';

const defaultPersonGroupListRequest: QueryPersonGroupPageParams = {
  pageNumber: 1,
  pageSize: DEFAULT_PAGE_SIZE,
};

export default function usePersonGroupListQuery() {
  const personGroupListRequestParams = ref(cloneDeep(defaultPersonGroupListRequest));
  const personGroupList = ref<PersonGroupListItem[]>();
  const total = ref(0);
  const loading = ref(false);

  const setRequestParams = (params: QueryPersonGroupPageParams) => {
    Object.assign(personGroupListRequestParams.value, params);
  };

  const resetRequestParams = () => {
    Object.assign(personGroupListRequestParams.value, defaultPersonGroupListRequest);
  };

  const queryPersonGroupList = async () => {
    try {
      loading.value = true;
      const result = await queryUserGroupPage(personGroupListRequestParams.value);
      personGroupList.value = result.records;
      total.value = result.totalRow;
    } catch (e) {
      console.error(e);
    } finally {
      loading.value = false;
    }
  };
  return {
    personGroupListRequestParams,
    personGroupList,
    loading,
    total,
    setRequestParams,
    queryPersonGroupList,
  };
}
