import projectApi from '../api/projectApi'

const state = {
    projects: []
}
const mutations = {
    setProjects(state, projects) {
        state.projects = projects
    },
    delProjects(state, projects) {
        for (let i = 0; i < state.projects.length; i++) {
            for (let j = 0; j < projects.length; j++) {
                if (state.projects[i].ipPort === projects[j].ipPort && state.projects[i].projectName === projects[j].projectName) {
                    state.projects.splice(i, 1)
                    i--;
                    break;
                }
            }
        }
    }
}
const actions = {
    getProjects({commit}, page) {
        projectApi.getProjects(page.index, page.size, (response) => {
            commit('setProjects', response.data.content);
        })
    },
    delProjects({commit}, projects) {
        projectApi.delProjects(projects, (response) => {
            if (response.data.code === 200) {
                commit('delProjects', projects);
            }
        })
    },
    deployProject(project) {
        return projectApi.deployProject(project);
    }
}
export default {
    namespaced: true,
    state,
    actions,
    mutations
}