import Axios from 'axios';

const axios = Axios.create({
    baseURL: 'http://' + window.location.host,
    // baseURL: 'http://localhost:3008',
    timeout: 10000,
})

export default {
    getJobs: function(query) {
        return axios.get(this.buildUrl(query));
    },
    buildUrl: function(query) {
        query = Object.assign({
            state: 'inactive',
            order: 'asc',
            page_size: 10,
            page_num: 1
        }, query);
        let url = '';
        if(query.type) {
            url = 'jobs/' + query.type + '/' + query.state + '/' + (query.page_num - 1) * query.page_size + '..' + query.page_num * query.page_size + '/' + query.order;
        }
        else {
            url = 'jobs/' + query.state + '/' + (query.page_num - 1) * query.page_size + '..' + query.page_num * query.page_size + '/' + query.order;
        }

        return url;
    },
    getStats: function(query) {
        if(query.type) {
            return axios.get('/jobs/' + query.type + '/' + query.state + '/stats')
            .then(data => {
                data.data =  {
                    [query.state + 'Count']: data.data.count
                }
                return data;
            })
        }
        return axios.get('stats')
    },
    delete: function(id) {
        return axios.delete('job/' + id);
    },
    retry: function (id) {
        return axios.get('inactive/' + id);
    },
    getTypes: function() {
        
    },
    getLogs: function(id) {
        return axios.get('job/' + id + '/log');
    },
    search: function(q) {
        if(!q) {
            return null;
        }
        return axios.get('job/search?q=' + q);
    },
    types: function() {
        return axios.get('job/types');
    }
}
