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

export const useStaticsStore = defineStore('statics', {
    state: () => ({
        items: [],
        fetching: true,
        total: 0
    }),
    actions: {
        async index() {
            this.fetching = true
            const { status, data } = await axios.get('/admin/settings')
            this.fetching = false
            if (status === 200) {
                 this.items = data
                // this.total = data.total
            }
        },
        async edit(data) {
            this.fetching = true
            const result =  await axios.patch('/admin/settings/edit', data)
            this.fetching = false
            return result
            // if (status === 200) {
            //      this.items = data
            //     // this.total = data.total
            // }
        },
        async create(id) {
            const result = await axios.post('/courseCategory', {
                name: id.value
            })


            return result;
        },

        async store(form) {
            const result = await axios.post('/admin/categories', form)

            if (result.status === 201)
                this.items.unshift(result.data)

            return result;
        },
        async update(id, form) {
            const result = await axios.put(`/admin/categories/${id}`, form)

            if (result.status === 200) {
                const index = this.items.findIndex(({ _id }) => _id == id)
                this.items[index] = result.data
            }

            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;
        },
    }
})