/* eslint-disable */
import axios from 'axios'
import { result } from 'lodash'

export default {
    namespaced: true,
    state: {
        list: [],
        singleTemp: {},
        item: {},
        message: undefined,
        recipients: [],
        recipient: {},
        contactUsLog: [],
        enquireNowLog: [],
    },
    mutations: {
        setList(state, list) {
            state.list = list
        },
        setSingle(state, template) {
            state.singleTemp = template
        },
        setRecipients(state, recips) {
            state.recipients = recips
        },
        setRecipient(state, recip) {
            state.recipient = recip
        },
        removeRecipient(state, id) {
            var index = state.recipients.findIndex((recip) => recip.id == id)
            state.recipients.splice(index, 1)
        },
        deleteTemplate(state, id) {
            var index = state.list.findIndex((template) => template.id == id)
            state.list.splice(index, 1)
        },
        setContactUsLog(state, log) {
            state.contactUsLog = log
        },
        setEnquireNowLog(state, log) {
            console.log(log)
            state.enquireNowLog = log
        },
    },
    getters: {},
    actions: {
        getList({ commit }) {
            axios
                .get('/api/template/getSimple')
                .then((result) => commit('setList', result.data))
                .catch((ex) => console.log(ex.response))
        },
        async getSingleTemplate({ commit }, id) {
            await axios
                .get('/api/template/' + id)
                .then((result) => commit('setSingle', result.data[0]))
                .catch((ex) => console.log(ex.response))
        },
        addItem({ dispatch }, item) {
            axios
                .post('/api/template', item)
                .then(() => dispatch('getList'))
                .catch((ex) => {
                    return Promise.reject(ex.response)
                })
        },
        editItem({ dispatch }, item) {
            axios
                .put(`/api/template/${item.id}`, item)
                .then(() => dispatch('getList'))
                .catch((ex) => {
                    return Promise.reject(ex.response)
                })
        },
        async deleteMailTemplate({ commit }, id) {
            await axios
                .delete('/api/template/' + id)
                .then(() => {
                    commit('deleteTemplate', id)
                })
                .catch((ex) => {
                    return Promise.reject(ex.response)
                })
        },
        async getMailRecipients({ commit }) {
            await axios
                .get('/api/mail/mailrecipients')
                .then((result) => {
                    commit('setRecipients', result.data)
                })
                .catch((e) => {
                    console.log(e.response)
                })
        },
        async getMailRecipientById({ commit }, id) {
            await axios
                .get('/api/mail/mailrecipient/' + id)
                .then((result) => {
                    commit('setRecipient', result.data)
                })
                .catch((e) => {
                    console.log(e.response)
                })
        },
        async addMailRecipient({ commit }, rec) {
            await axios
                .post('/api/mail/mailrecipient', rec)
                .then((result) => {
                    console.log(result)
                })
                .catch((e) => {
                    console.log(e.response)
                })
        },
        async updateMailRecipient({ commit }, rec) {
            await axios
                .put('/api/mail/mailrecipients', rec)
                .then((result) => {
                    console.log(result)
                })
                .catch((e) => {
                    console.log(e.response)
                })
        },
        async deleteMailRecipient({ commit }, id) {
            await axios
                .delete('/api/mail/mailrecipient/' + id)
                .then(() => {
                    commit('removeRecipient', id)
                })
                .catch((e) => {
                    console.log(e.response)
                })
        },
        async getContactUsLog({ commit }) {
            await axios
                .get('/api/mail/getContactLog/')
                .then((result) => {
                    commit('setContactUsLog', result.data)
                })
                .catch((response) => {
                    console.log(response.response)
                })
        },
        async getEnquireNowLog({ commit }) {
            await axios
                .get('/api/mail/getEnquireLog/')
                .then((result) => {
                    commit('setEnquireNowLog', result.data)
                })
                .catch((response) => {
                    console.log(response.response)
                })
        },
    },
}