/* eslint-disable */
import axios from "axios";
import { Object } from "core-js";
import promise from "core-js/fn/promise";

export default {
    namespaced: true,
    state: {
        campaign: Object,
        campaigns: [],
        campaignDTO: {},
        campaignHtml: '',
        campaignPlaceHolders: [],
        campaignItems: [],
        uploadedCampaigns: []
    },
    mutations: {
        setCampaign(state, campaign) {
            state.campaign = campaign;
        },
        setCampaigns(state, campaign) {
            state.campaigns = campaign;
        },
        setCampaignDTO(state, campaignDTO) {
            state.campaignDTO = campaignDTO;
        },
        setCampaignItems(state, items) {
            state.campaignItems = items;
        },
        setCampaignPlaceHolders(state, items) {
            state.campaignPlaceHolders = items;
        },
        setCampaignsUpdate(state, item) {
            state.campaigns.pop(state.campaigns.find(p => p.id === item.id));
            state.campaigns.push(item);
        },
        removeCampaign(state, id) {
            state.campaigns.pop(state.campaigns.find(p => p.id === id));
        },
        setCampaignHTML(state, campaign) {
            state.campaignHtml = campaign;
        },
        setUploadedCampaigns(state, campaigns){
            state.uploadedCampaigns = campaigns
        },
        deleteUploadedById(state, id){
            var index = state.uploadedCampaigns.findIndex((camp) => camp.id == id)
            state.uploadedCampaigns.splice(index, 1)
        }
    },
    getters: {},
    actions: {
        getCampaign({ commit }, id) {
            axios
                .get(`/api/campaign/${id}`)
                .then(result => commit("setCampaign", result.data))
                .catch(console.error);
        },
        getCampaignDTO({ commit }, id) {
            axios
                .get(`/api/campaign/GetDTO/${id}`)
                .then(result => commit("setCampaignDTO", result.data))
                .catch(console.error);
        },
        getCampaignHTML({ commit }, id) {
            axios
                .get(`/api/campaign/GetLandingPage/${id}`)
                .then(result => commit("setCampaignHTML", result.data))
                .catch(console.error);
        },
        getCampaignItems({ commit }, id) {
            axios
                .get(`/api/campaign/GetCampaignItems/${id}`)
                .then(result => commit("setCampaignItems", result.data))
                .catch(console.error);
        },
        getCampaignPlaceHolders({ commit }, id) {
            axios
                .get(`/api/campaign/GetCampaignPlaceHolders/${id}`)
                .then(result => commit("setCampaignPlaceHolders", result.data))
                .catch(console.error);
        },
        getCampaigns({ commit }) {
            axios
                .get("/api/campaign")
                .then(result => commit("setCampaigns", result.data))
                .catch(console.error);
        },
        saveCampaign({ commit }, item) {
            axios
                .post("/api/campaign", item)
                .then(result => commit("setCampaignsUpdate", result.data))
                .catch(console.error);
        },
        updateCampaign({ commit }, item) {
            axios
                .put("/api/campaign", item)
                .then(result => commit("setCampaignsUpdate", item))
                .catch(console.error);
        },
        deleteCampaign({ commit }, id) {
            axios
                .delete(`/api/campaign/${id}`)
                .then(result => commit("removeCampaign", id))
                .catch(console.error);
        },
        async uploadCampaign({commit}, file){
            return await new Promise((resolve, reject) => {
                axios({
                    method: 'post',
                    url: '/api/campaign/FileUpload',
                    data: file,
                    headers: { 'Content-Type': 'multipart/form-data' },
                  }).then((resp) => {
                    resolve(resp.data)
                  }).catch((ex) => {
                    reject(ex.response)
                  })
            })
            
        },
        async getUploadedCampaign({commit}){
            return await new Promise((resolve, reject) => {
                 axios.get(`/api/campaign/GetUploaded`)
                .then((resp) => {
                    resolve(resp)
                    commit("setUploadedCampaigns", resp.data)
                })
                .catch((ex) => {
                    reject(ex)
                    console.log(ex.response);
                })
            })
            
        },
        async deleteUploadedCampaign({commit}, id){
            return await new Promise((resolve, reject) => {
                axios.delete(`api/campaign/DeleteUploaded/`+ id)
                .then((resp) => {
                    commit("deleteUploadedById", id)
                    resolve(resp)
                })
                .catch((ex) => {
                    console.log(ex.response)
                    reject(ex.response)
                })
            })
        }
    }
};
