
import axios from '../plugins/axios'
import { defineStore } from 'pinia'

export const useCategoriesStore = defineStore('categories', {
    state: () => ({
        items: [],
        fetching: true,
        total: 0
    }),
    actions: {
        async index() {
            this.fetching = true
            const { data } = await axios.get('/courseCategory')
            this.fetching = false
            return data
        },
    
    async create(id) {
        const result = await axios.post('/courseCategory', {
            name: id.value
        })
        return result;
    },
    async remove(id) {
        const result = await axios.delete(`/courseCategory/${id}`)

        if (result.status === 200) {
            const index = this.items.findIndex(({ _id }) => _id == id)
            this.items.splice(index, 1)
        }

        return result;
    },
    }
})