import { defineStore } from 'pinia'
import { useApi } from '@/composables/useApi'

export const useCategoryStore = defineStore('categoryStore', {

  state: () => ({
    items: [],
    totalItems: 0,
    loading: false,
    error: null,
    rowsPerPage: 50
  }),

  actions: {

    async loadData(page = 1,payload = null) {
      try{
        const { data, error, loading, fetchAll, pagination } = useApi('categories')
        await fetchAll(page,this.rowsPerPage,payload)
        if (data.value) this.items = data.value;
        if (error.value) this.error = error.value;
        this.loading = loading.value;
        this.totalItems = pagination.total;
      }catch(error){
        console.error(error);
      }
    },

  }
})