class Ajax{
    constructor(url, data, headers, success, error, type='default'){
       this.url = url;
       this.data = data;
       this.headers = headers;
       this.success = success;
       this.error = error;
       this.type = type;
       this.jqXHR = null;
    } 
    post(){
        (this.type == 'default') ? this.ajax('POST'): this.ajaxForm('POST');
    }
    get(){
        this.ajax('GET');
    }
    put(){
        (this.type == 'default') ? this.ajax('PUT'): this.ajaxForm('PUT');
    }
    delete(){
        (this.type == 'default') ? this.ajax('DELETE'): this.ajaxForm('DELETE');
    }
    abort() {
        if (this.jqXHR) {
            this.jqXHR.abort();
            this.jqXHR = null;  // Limpiamos la referencia a la petición
        }
    }
    setData(data){
        this.data = data;
    }
    ajax(method){
        if (this.jqXHR) {
            this.jqXHR.abort();
        }

        this.jqXHR = $.ajax({
            url : this.url,
            type : method,
            data: this.data,
            headers: this.headers,
            success: this.success,
            error: this.error
        })
    }
    ajaxForm(method){
        if (this.jqXHR) {
            this.jqXHR.abort();
        }

        this.jqXHR = $.ajax({
            url : this.url,
            type : method,
            data: this.data,
            processData: false,
            contentType: false,
            headers: this.headers,
            success: this.success,
            error: this.error
        })
    }
}

export default Ajax