getUserId();

function load_table_red_rank() {
  //  get_data("fcBiWorkerDaily/get?type=1&limit=3", on_red_rank_success);
}

function load_table_red_rank1() {
  //  get_data("fcBiWorkerDaily/get?type=2&limit=3", on_red_rank_success1);
}

function load_table_black_rank1() {
  //  get_data("fcBiWorkerDaily/get?type=3&limit=3", on_black_rank_success1);
}

function load_table_black_rank() {
 //   get_data("fcBiWorkerDaily/get?type=0&limit=3", on_black_rank_success);
}

var RANK_TBL_ROW_COUNT = 4;
var goodColor = [];
var badColor = [];
var goodColor1 = [];
var badColor1 = [];
var userIDs = [];
var schedulepass = 0, schedulefail = 0, schedulestack = 0;
var todayotherpass = 0, todayotherfail = 0, todayotherstack = 0;
var lastTimeHistory = "";
var histoires = [];

var hispass = [];
var hisfail = [];
var hisstack = [];
var hisrate = [];
var goodpass = [];
var goodid = [];
var goodfailrate = [];
var goodpass1 = [];
var goodid1 = [];
var goodfailrate1 = [];
var badpass = [];
var badid = [];
var badfailrate = [];
var badpass1 = [];
var badid1 = [];
var badfailrate1 = [];

function history_success(obj, sta) {
    var histoire = first_parse(this, obj);
    var i, j, t;
    console.log(" histoire : ");
    console.log(histoire);
    if (this.url.indexOf("type=1") >= 0) {
        t = 1;
    } else if (this.url.indexOf("type=2") >= 0) {
        t = 2;
    } else if (this.url.indexOf("type=3") >= 0) {
        t = 3;
    } else {
        t = 0;
        return;
    }
    histoires[t] = histoire;
    if (t === 2 || t === 3) {
        hispass[t] = 0;
        hisfail[t] = 0;
        hisstack[t] = 0;
        hisrate[t] = 0;
        for (i = 0; i < histoire.length - 1; i++) { //除了今日
            hispass[t] = histoire[i].passNum + hispass[t];
            hisfail[t] = histoire[i].failNum + hisfail[t];
            hisstack[t] = histoire[i].avgDue + hisstack[t];
        }
        orighispass[t] = hispass[t];
        orighisfail[t] = hisfail[t];
        orighisstack[t] = hisstack[t];
        hispass[t] += hispass[0];
        hisfail[t] += hisfail[0];
        hisstack[t] += hisstack[0];
        hisrate[t] = get_factor(hispass[t], hisfail[t]);
    } else if (t === 1) {
        hispass[1] = histoire[0].passNum;
        hisfail[1] = histoire[0].failNum;
        hisstack[1] = histoire[0].avgDue;
        hisrate[t] = get_factor(hispass[1], hisfail[1]);
    }

    todayotherpass = histoire[histoire.length - 1].passNum;
    todayotherfail = histoire[histoire.length - 1].failNum;
    todayotherstack = histoire[histoire.length - 1].avgDue;

    update_all_history();
}

function update_all_history() {
    hispass[0] = todayotherpass + schedulepass;
    hisfail[0] = todayotherfail + schedulefail;
    hisstack[0] = todayotherstack + schedulestack;
    hisrate[0] = get_factor(hispass[0], hisfail[0]);

    if (typeof hisrate[0] == "undefined" || isNaN(hisrate[0])) {
        send_alert("请重试！");
    }

    for (i = 2; i <= 3; i++) {
        hispass[i] = orighispass[i] + hispass[0];
        hisfail[i] = orighisfail[i] + hisfail[0];
        hisstack[i] = orighisstack[i] + hisstack[0];
        hisrate[i] = get_factor(hispass[i], hisfail[i]);
    }
}

var todayotherpass, todayotherfail, todayotherstack;
var orighisfail = [], orighispass = [], orighisstack = [];

function isUserOfThisFlow(uid) {
    var isExist = false;
    userIDs.forEach(function (vals) {
        if (vals == uid) {
            isExist = true;
        }
    });
    return isExist;
}

function on_red_rank_success(obj, sta) {
    var redchart = first_parse(this, obj);
    var i, j;
    i = j = 0;
    console.log(" redchart : ");
    console.log(redchart);
    for (i = 0; i < redchart.list.length; i++) {
        goodid[i] = getUserName(redchart.list[i].workerId);
        goodpass[i] = redchart.list[i].passNum;
        if (redchart.list[i].passNum + redchart.list[i].failNum > 0) {
            goodfailrate[i] = Math.floor(redchart.list[i].failNum * 10000 / (redchart.list[i].passNum + redchart.list[i].failNum)) / 100;
        } else {
            goodfailrate[i] = 0;
        }
        if (isUserOfThisFlow(redchart.list[i].workerId)) {
            goodColor[i] = 'red';
        } else {
            goodColor[i] = '';
        }
    }

}

function on_red_rank_success1(obj, sta) {
    var redchart = first_parse(this, obj);
    var i, j;
    i = j = 0;
    console.log(" redchart : ");
    console.log(redchart);
    for (i = 0; i < redchart.list.length; i++) {
        goodid1[i] = getUserName(redchart.list[i].workerId);
        goodpass1[i] = redchart.list[i].passNum;
        if (redchart.list[i].passNum + redchart.list[i].failNum > 0) {
            goodfailrate1[i] = Math.floor(redchart.list[i].failNum * 10000 / (redchart.list[i].passNum + redchart.list[i].failNum)) / 100;
        } else {
            goodfailrate1[i] = 0;
        }
        if (isUserOfThisFlow(redchart.list[i].workerId)) {
            goodColor1[i] = 'red';
        } else {
            goodColor1[i] = '';
        }
    }

}

function on_black_rank_success(obj, sta) {
    var blackchart = first_parse(this, obj);
    var i, j;
    i = j = 0;
    console.log(" black chart : ");
    console.log(blackchart);
    for (i = 0; i < blackchart.list.length; i++) {
        badid[i] = getUserName(blackchart.list[i].workerId);
        badpass[i] = blackchart.list[i].passNum;
        //badfailrate[i] = blackchart.list[i].failRate / 100;
        if (blackchart.list[i].passNum + blackchart.list[i].failNum > 0) {
            badfailrate[i] = Math.floor(blackchart.list[i].failNum * 10000 / (blackchart.list[i].passNum + blackchart.list[i].failNum)) / 100;
        } else {
            badfailrate[i] = 0;
        }
        if (isUserOfThisFlow(blackchart.list[i].workerId)) {
            badColor[i] = 'green';
        } else {
            badColor[i] = '';
        }
    }


}

function on_black_rank_success1(obj, sta) {
    var blackchart = first_parse(this, obj);
    var i, j;
    i = j = 0;
    console.log(" black chart : ");
    console.log(blackchart);
    for (i = 0; i < blackchart.list.length; i++) {
        badid1[i] = getUserName(blackchart.list[i].workerId);
        badpass1[i] = blackchart.list[i].passNum;
        //badfailrate[i] = blackchart.list[i].failRate / 100;
        if (blackchart.list[i].passNum + blackchart.list[i].failNum > 0) {
            badfailrate1[i] = Math.floor(blackchart.list[i].failNum * 10000 / (blackchart.list[i].passNum + blackchart.list[i].failNum)) / 100;
        } else {
            badfailrate1[i] = 0;
        }
        if (isUserOfThisFlow(blackchart.list[i].workerId)) {
            badColor1[i] = 'green';
        } else {
            badColor1[i] = '';
        }
    }


}

function getHistoryTableBody(htmlbody, i) {
    if (typeof hispass[i] === "undefined") {
        htmlbody += "<tr>";
        htmlbody += `<tr>
                    <td> - </td>
                    <td> - </td>
                    <td> - </td>
                    <td> - </td>
                    <td> - </td>
                </tr>`;
        htmlbody += "</tr>";
        return htmlbody;
    }
    htmlbody += "<tr>";
    htmlbody += `<tr>
                    <td>${timeday[i]}</td>
                    <td>${hispass[i]}</td>
                    <td>${hisfail[i]}</td>
                    <td>${hisstack[i]}</td>
                    <td>${hisrate[i] + '%'}</td>
                </tr>`;
    htmlbody += "</tr>";
    return htmlbody;
}

function getRankingTableBody(htmlbody, i) {
    if (typeof goodRank === "undefined" || typeof goodRank[i] === "undefined") {
        htmlbody += "<tr>";
        htmlbody += `<tr>
                    <td> - </td>
                    <td> 0 </td>
                    <td> 0 </td>
                    <td> 0 </td>
                </tr>`;
        htmlbody += "</tr>";
        return htmlbody;
    }
    htmlbody += "<tr>";
    htmlbody += `<tr ">
                    <td style="background-color: ${goodRank[i].color}">${goodRank[i].id}</td>
                    <td style="background-color: ${goodRank[i].color}">${goodRank[i].pass}</td>
                    <td style="background-color: ${goodRank[i].color}">${goodRank[i].fail}</td>
                    <td style="background-color: ${goodRank[i].color}">${goodRank[i].failRate + '%'}</td>
                </tr>`;
    htmlbody += "</tr>";
    return htmlbody;
}

var lastTimeRank0 = "";
var lastTimeRank1 = "";
var lastTimeRank2 = "";
var lastTimeRank3 = "";
var lastTimeRank4 = "";

function showHistoryData() {
    var htmlbody = "", one_person = "";
    var i = 0;
    var triGap = 4;
    htmlbody = "";
    for (i = 0; i < triGap; i++) {

        htmlbody = getHistoryTableBody(htmlbody, i);
    }
    if (lastTimeRank0 !== htmlbody) {
        $("#rank-one").html(htmlbody);
        lastTimeRank0 = htmlbody;
    }
    //console.log(htmlbody);
}

function showRedRanking() {
    //console.log(htmlbody);
}


function showBlackRanking() {
    return; // Not Showing Because we now try to scroll pages.
    //
    // var htmlbody = "", one_person = "";
    // var i = 0;
    // var triGap = RANK_TBL_ROW_COUNT;
    // htmlbody = "";
    // for (i = 0; i < triGap; i++) {
    //     htmlbody = getBlackRanking(htmlbody, i);
    // }
    // if (lastTimeRank2 !== htmlbody) {
    //     $("#rank-three").html(htmlbody);
    //     lastTimeRank2 = htmlbody;
    // }
    //console.log(htmlbody);
}