/* eslint-disable */
import axios from 'axios'

export default {
    namespaced: true,
    state: {
        statusList: [],
        status: {},
    },
    mutations: {
        setStatusList(state, list) {
            state.statusList = list
        },
        addNewStatus(state, status) {
            state.statusList.push(status)
        },
        setSingleStatus(state, status) {
            state.status = status
        },
        deleteStatus(state, id) {
            var index = state.statusList.findIndex((stat) => stat.id == id)
            state.statusList.splice(index, 1)
        },
        clearStatus(state) {
            state.status = {
                code: '',
                description: '',
            }
        },
    },
    getters: {},
    actions: {
        getStatusList({ commit }) {
            axios
                .get('/api/status')
                .then((result) => commit('setStatusList', result.data))
                .catch(console.error)
        },
        async getSingleStatus({ commit }, id) {
            await axios
                .get('/api/status/' + id)
                .then((result) => {
                    commit('setSingleStatus', result.data[0])
                })
                .catch((e) => {
                    console.log(e.response)
                })
        },
        async createNewStatus({ commit }, status) {
            await axios
                .post('/api/status', status)
                .then((result) => {
                    commit('addNewStatus', result.data)
                })
                .catch((e) => {
                    console.log(e)
                })
        },
        async updateStatus({ commit }, status) {
            await axios
                .put('/api/status', status)
                .then((result) => {})
                .catch((e) => {
                    console.log(e)
                })
        },
        async deleteStatus({ commit }, id) {
            await axios.delete('/api/status/' + id).then(() => {
                commit('deleteStatus', id)
            })
        },
        clearStatus({ commit }) {
            commit('clearStatus')
        },
    },
}