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

export const useCommentStore = defineStore('comments', {
    state: () => ({
        items: [],
        fetching: true,
        total: 0
    }),
    actions: {
        async reply(id, body) {
            this.fetching = true
            const  data  = await axios.post(`/course/${id}/comment`, body)
            this.fetching = false
           return data
        },

        
        async ticket(id) {
            this.fetching = true
            const { status, data } = await axios.get(`/ticket/${id}`)
            this.fetching = false
            return data
        },
        async close(id){
            this.fetching = true
            const { status, data } = await axios.patch(`/ticket/close/${id}`, id)
            this.fetching = false
            return data
        },    
       
        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;
        },
    }
})