/* eslint-disable */
import axios from 'axios'
import promise from 'core-js/fn/promise'

export default {
    namespaced: true,
    state: {
        individuals: [],
        agents: [],
        agent: {},
        agencies: [],
        agency: {},
        registerIndividual: {
            name: '',
            surname: '',
            email: '',
            cellNumber: '',
            telephone: '',
            username: '',
            password: '',
            acceptedTerms: false,
        },
        registerAgency: {
            name: '',
            eaabeffcNumber: '',
            companyRegNumber: '',
            user: {
                name: '',
                surname: '',
                email: '',
                cellNumber: '',
                telephone: '',
                username: '',
                password: '',
                confirm: '',
                role: '',
                acceptedTerms: false,
            },
        },
    },
    mutations: {
        setIndividual(state, jsregistertype) {
            state.registerIndividual = jsregistertype
        },
        setIndividuals(state, jsregistertype) {
            state.individuals = jsregistertype
        },
        setAgents(state, jsregistertype) {
            state.agents = jsregistertype
        },
        setSingleAgent(state, jsregistertype) {
            state.agent = jsregistertype
        },
        setAgencies(state, jsregistertype) {
            state.agencies = jsregistertype
        },
        setAgency(state, jsregistertype) {
            state.registerAgency = jsregistertype
        },
        addIndividual(state, jsregistertype) {
            state.registerIndividual = jsregistertype
        },
        addAgency(state, jsregistertype) {
            state.registerAgency = jsregistertype
        },
        addAgent(state, jsregistertype) {
            state.registerIndividual = jsregistertype
        },
        updateIndividual(state, jsregistertype) {
            state.registerIndividual.find(
                (item) => item.id === jsregistertype.id,
            ).username = type.username
            state.registerIndividual.find(
                (item) => item.id === jsregistertype.id,
            ).password = type.password
        },
        updateAgency(state, jsregistertype) {
            state.registerAgency.find(
                (item) => item.id === jsregistertype.id,
            ).eaabeffcNumber = jsregistertype.eaabeffcNumber
            state.registerAgency.find(
                (item) => item.id === jsregistertype.id,
            ).companyRegNumber = jsregistertype.companyRegNumber
        },
        agentUpdate(state, jsregistertype) {
            var index
            var count = 0
            state.agents.forEach((item) => {
                if (item.id === jsregistertype.id) {
                    index = count
                }
                count++
            })
            Object.assign(state.agents[index], jsregistertype)
        },
        clearIndividual(state) {
            state.registerIndividual = {
                name: '',
                surname: '',
                email: '',
                cellNumber: '',
                telephone: '',
                username: '',
                password: '',
                acceptedTerms: false,
            }
        },
        clearAgency(state) {
            state.registerAgency = {
                name: '',
                eaabeffcNumber: '',
                companyRegNumber: '',
                user: {
                    name: '',
                    surname: '',
                    email: '',
                    cellNumber: '',
                    username: '',
                    password: '',
                    confirm: '',
                    role: '',
                    acceptedTerms: false,
                },
            }
        },
        removeIndividual(state, id) {
            var index = state.individuals.findIndex((indiv) => indiv.id === id)
            state.individuals.splice(index, 1)
        },
        removeAgency(state, id) {
            state.registerAgency.pop(
                state.registerAgency.find((agency) => agency.id === id),
            )
        },
        removeAgent(state, id) {
            var index = state.agents.findIndex((agent) => agent.id === id)
            state.agents.splice(index, 1)
        },
    },
    getters: {},
    actions: {
        async forgotPasswordCheck({ commit }, mail) {
            await axios
                .post('api/register/forgotPassword/' + mail)
                .then((resp) => {
                    return Promise.resolve(resp.data)
                })
                .catch((ex) => {
                    console.log(ex.response)
                    return Promise.reject(ex.response)
                })
        },
        getIndividuals({ commit }) {
            axios
                .get('/api/individual')
                .then((result) => commit('setIndividuals', result.data))
                .catch(console.error)
        },
        getAgents({ commit }) {
            axios
                .get('/api/agent')
                .then((result) => {
                    commit('setAgents', result.data)
                })
                .catch(console.error)
        },
        async getAgentById({ commit }, userId) {
            await axios
                .get('/api/agent/single/' + userId)
                .then((result) => {
                    commit('setSingleAgent', result.data[0])
                })
                .catch((e) => {
                    console.log(e)
                })
        },
        getAgencies({ commit }) {
            axios
                .get('/api/agency')
                .then((result) => {
                    commit('setAgencies', result.data)
                })
                .catch(console.error)
        },
        getAgency({ commit }, id) {
            axios
                .get(`/api/agency/${id}`)
                .then((result) => commit('setAgency', result.data))
                .catch(console.error)
        },

        async saveIndividual({ commit }, item) {
            await axios
                .post('/api/register/register', item)
                .then((res) => {
                    commit('addIndividual', res.data)
                    return promise.resolve()
                })
                .catch((ex) => {
                    return promise.reject(ex.response)
                })
        },
        async saveAgency({ commit }, item) {
            await axios
                .post('/api/register/registeragency', item)
                .then((res) => {
                    commit('addAgency', res.data)
                    return promise.resolve()
                })
                .catch((ex) => {
                    return promise.reject(ex.response)
                })
        },
        async saveAgent({ commit }, item) {
            await axios
                .post('/api/agent/AgentFromUser', item)
                .then((res) => {
                    commit('addAgent', res.data)
                    return Promise.resolve(res)
                })
                .catch((ex) => {
                    return Promise.reject(ex.response)
                })
        },
        async updateIndividual({ commit }, userParam) {
            await axios
                .put('/api/user/', userParam)
                .then((result) => {})
                .catch(console.error)
        },
        updateAgency({ commit }, item) {
            axios
                .put('/api/agency', item)
                .then((result) => commit('updateAgency', item))
                .catch(console.error)
        },
        async updateAgent({ commit }, agent) {
            await axios
                .put('/api/agent', agent)
                .then((result) => {
                    commit('updateAgent', result.data)
                })
                .catch((ex) => {
                    console.log(ex)
                })
        },
        // clearIndividual({
        //   commit,
        // }) {
        //   commit('clearIndividual');
        // },
        // clearAgency({
        //   commit,
        // }) {
        //   commit('clearAgency');
        // },
        deleteIndividual({ commit }, id) {
            axios
                .delete(`/api/individual/${id}`)
                .then((result) => commit('removeIndividual', id))
                .catch(console.error)
        },
        deleteAgent({ commit }, id) {
            axios
                .delete(`/api/agent/${id}`)
                .then((result) => commit('removeAgent', id))
                .catch((ex) => console.log(ex.response))
        },
        deleteAgency({ commit }, id) {
            axios
                .delete(`/api/agency/${id}`)
                .then((result) => commit('removeAgency', id))
                .catch(console.error)
        },
    },
}