/**
 * Created by Mai on 2017/3/10.
 */

var PullData = function (url, data, successCallback, errorCallback) {
    $.ajax({
        type:"POST",
        url: url,
        data: {'data': JSON.stringify(data)},
        dataType: 'json',
        cache: false,
        timeout: 50000,
        success: function(result){
            if (result.error === 0) {
                successCallback(result.data);
            } else {
                alert('error: ' + result.message);
                errorCallback();
            }
        },
        error: function(jqXHR, textStatus, errorThrown){
            alert('error ' + textStatus + " " + errorThrown);
            errorCallback();
        }
    });
};

var GetProject = function (proj_id, callback) {
    $.ajax({
        type:"POST",
        url: '/pm/api/getProject',
        data: {'data': JSON.stringify({"user_id": userID, "proj_id": proj_id})},
        dataType: 'json',
        cache: false,
        timeout: 50000,
        success: function(result){
            if (result.error === 0) {
                callback(result.data);
            } else {
                alert('error: ' + result.message);
            }
        },
        error: function(jqXHR, textStatus, errorThrown){
            alert('error ' + textStatus + " " + errorThrown);
        }
    });
};

var GetBills = function (proj_id, callback) {
    $.ajax({
        type: "POST",
        url: '/getBills',
        data: {'data': JSON.stringify({"user_id": userID, "proj_id": proj_id})},
        dataType: 'json',
        cache: false,
        timeout: 50000,
        success: function (result) {
            if (result.error === 0) {
                callback(result.data);
            } else {
                alert('error: ' + result.message);
            }
        },
        error: function(jqXHR, textStatus, errorThrown){
            alert('error ' + textStatus + " " + errorThrown);
        }
    });
};

var GetRations = function (proj_id, callback) {
    $.ajax({
        type: "POST",
        url: '/getRations',
        data: {'data': JSON.stringify({"user_id": userID, "proj_id": proj_id})},
        dataType: 'json',
        cache: false,
        timeout: 50000,
        success: function (result) {
            if (result.error === 0) {
                callback(result.data);
            } else {
                alert('error: ' + result.message);
            }
        },
        error: function(jqXHR, textStatus, errorThrown){
            alert('error ' + textStatus + " " + errorThrown);
        }
    });
};
