/* 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, type) {
            state.registerIndividual = type
        },
        setIndividuals(state, type) {
            state.individuals = type
        },
        setAgents(state, type) {
            state.agents = type
        },
        setSingleAgent(state, agent) {
            state.agent = agent
        },
        setAgencies(state, type) {
            state.agencies = type
        },
        setAgency(state, type) {
            state.registerAgency = type
        },
        addIndividual(state, type) {
            state.registerIndividual = type
        },
        addAgency(state, type) {
            state.registerAgency = type
        },
        addAgent(state, type) {
            state.registerIndividual = type
        },
        updateIndividual(state, type) {
            state.registerIndividual.find((item) => item.id === type.id).username =
                type.username
            state.registerIndividual.find((item) => item.id === type.id).password =
                type.password
        },
        updateAgency(state, type) {
            state.registerAgency.find((item) => item.id === type.id).eaabeffcNumber =
                type.eaabeffcNumber
            state.registerAgency.find(
                (item) => item.id === type.id,
            ).companyRegNumber = type.companyRegNumber
        },
        clearIndividual(state) {
            state.registerIndividual = {
                name: '',
                surname: '',
                email: '',
                cellNumber: '',
                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) => {
                    //console.log('Agents')
                    //console.log(result)
                    commit('setAgents', result.data)
                })
                .catch(console.error)
        },
        async getAgentById({ commit }, userId) {
            await axios
                .get('/api/agent/single/' + userId)
                .then((result) => {
                    console.log(result)
                    commit('setSingleAgent', result.data)
                })
                .catch((e) => {
                    console.log(e)
                })
        },
        getAgencies({ commit }) {
            axios
                .get('/api/agency')
                .then((result) => {
                    //console.log('Agencies')
                    //console.log(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) => {
                    //console.log(ex.response)
                    return promise.reject(ex.response)
                        //console.error;
                })
        },
        async saveAgency({ commit }, item) {
            await axios
                .post('/api/register/registeragency', item)
                .then((res) => {
                    commit('addAgency', res.data)
                    console.log(promise.resolve())
                    console.log('Agency Added')
                    return promise.resolve()
                })
                .catch((ex) => {
                    console.log('regAgency API failed')
                        //console.log(ex.response)
                    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) => console.log(result))
                .catch(console.error)
        },
        updateAgency({ commit }, item) {
            axios
                .put('/api/agency', item)
                .then((result) => commit('updateAgency', item))
                .catch(console.error)
        },
        // 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)
        },
    },
}