this.HistoryData = (function (){
    this.update = function () {
        // 请注意type必须在结尾
        get_data("fcBiFlowDaily/list?flowId=" + flowId + "&type=1", history_success);
    };

    this.loadHistroyOnce = function (nnm) {
        // 请注意type必须在结尾
        get_data(HistoryBaseURL + "&type=" + nnm, history_success);
    };

    this.loadMultiHistory = function(){
        for (i = 1; i <= 3; i++) loadHistroyOnce(i);
    };

    this.add_success = function(obj, sta) {
        var history1 = first_parse(this, obj);
        var i, j;
        console.log(" history1 : ");
        console.log(history1);
        hispass[t] = 0;
        hisfail[t] = 0;
        hisstack[t] = 0;
        hisrate[t] = 0;
        var lengthnum = history1.length - 1;
        for (i = lengthnum; i < history1.length; i++) {
            hispass[t] = history1[i].passNum + hispass[t];
            hisfail[t] = history1[i].failNum + hisfail[t];
            hisstack[t] = history1[i].avgDue + hisstack[t];
        }
        if (hispass[t] + hisfail[t] == 0) {
            hisrate[t] = 0;
        }
        t++;
    }
})();

function realtime_success(obj, sta) {
    var output = first_parse(this, obj);
    //在这里排序 spo\
    console.log(" output : ");
    console.log(output);

    var i, j, k, z;
    failnum = 0;
    var x = 0;
    var worksum = [];
    var hhp = 0, hhf = 0, hhs = 0;


    if (output.list.length > 0) {
        sectorID[0] = getSectorId(output.list[0].spotId);
        passNumber[0] = 0;
        failNumber[0] = 0;
        stackeverynum[0] = 0;
        z = 0;
        for (i = 1; i < output.list.length; i++) {
            var thisUid = getSectorId(output.list[i].spotId);
            x = 0;
            for (j = 0; j < sectorID.length; j++) {
                if (sectorID[j] === thisUid) {
                    x = 1;
                    break;
                }
            }
            if (x === 0) {
                sectorID.push(thisUid);
            }

        }
        j = 0;
        for (k = 0; k < sectorID.length; k++) {
            var uid = sectorID[k];
            z = 0;
            passNumber[k] = 0;
            failNumber[k] = 0;
            stackeverynum[k] = 0;
            for (i = 0; i < output.list.length; i++) {
                if (uid === getSectorId(output.list[i].spotId)) {
                    passNumber[k] = output.list[i].passNum + passNumber[k];
                    failNumber[k] = output.list[i].failNum + failNumber[k];
                    stackeverynum[k] = output.list[i].due + stackeverynum[k];
                    z++;
                }
            }
            failrate[k] = get_factor(passNumber[k], failNumber[k]);
        }
        output.list.forEach(function (val, fid, arr) {
            hhp += val.passNum;
            hhf += val.failNum;
            hhs += val.due;
        })
    }

    schedulepass = hhp;
    schedulefail = hhf;
    if (output.list.length > 0) {
        schedulestack = hhs / output.list.length;
    } else {
        schedulestack = 0;
    }
    update_all_history();
    updateChart();

};


var RealTimeCharts = (function() {

    var timeday = ["今日", "昨日", "本周", "本月"];

    if (flowId == null || scheduleId == null) {
        send_alert("出错啦！请检查参数完整性");
    }

    function page_document_load() {
        get_data("fcWorkScheduleLoad/list?scheduleId=" + scheduleId, onFirstTimeSuccess);
    }

    var compAxis = {
        categories: namedata,
        crosshair: true,
        title: {
            text: '工位',
            style: {
                fontSize: '1.5em'
            }
        },
        labels: {
            style: {
                fontSize: '1.5em'
            }
        }
    };

    var getSeperateArrays = function (sectorLoadData){
        this.data = [];
        this.len = sectorLoadData.len();
        this.stackmax = 0;
        this.failmax = 0;
        this.passmax = 0;
        this.failratemax = 0;
        this.ratedata = [];
        this.passdata = [];
        this.faildata = [];
        for (i = 0; i < len; i++) {
            var one = sectorLoadData.getRaw(i);
            if (stackmax < one[i].getVal("stack")) {
                stackmax = one[i].getVal("stack")
            }
            if (failmax < one[i].getVal("failNum")) {
                failmax = one[i].getVal("failNum");
            }
            if (failratemax < one[i].getVal("failRate")) {
                failratemax = one[i].getVal("failRate");
            }
            if (passmax < one[i].getVal("passNum")) {
                passmax = one[i].getVal("passNum");
            }

            data.push(one[i].getVal("stack"));
            this.ratedata.push(Math.floor(one[i].getVal("failRate")));
            this.passdata.push(one[i].getVal("passNum"));
            this.faildata.push(one[i].getVal("failNum"));
        }

        failratemax = Math.max(failratemax, 5);
        stackmax = Math.max(stackmax, 5);
        failmax = Math.max(failmax, 5);
        passmax = Math.max(passmax, failmax, stackmax);
    };

    this.dataArrays = null;

    function showChart(sectorLoadData) {
        dataArrays = new getSeperateArrays(sectorLoadData);

        stackAndFailChart = Highcharts.chart('graph-1', {
            chart: {
                zoomType: 'xy',
                //height: (4 / 16 * 100) + '%' // 16:9 ratio
            },
            title: {text: '各工位今日产能'},
            credits: {enabled: false},
            exporting: {enabled: false},
            xAxis: [compAxis],
            yAxis: [{
                labels: {
                    format: '{value}',
                    style: {
                        color: "#ffffff",
                        fontSize: '1.5em'
                    }
                },
                min: 0,
                max: passmax * 1.5,
                title: {
                    text: '数量',
                    style: {
                        color: '#ffffff',
                        fontSize: '1em'
                    },
                    rotation: 270,
                }
            },],
            tooltip: {shared: true},
            legend: {
                layout: 'vertical',
                align: 'right',
                x: 0,
                verticalAlign: 'top',
                y: 0,
                floating: true,
                backgroundColor: (Highcharts.theme && Highcharts.theme.legendBackgroundColor) || '#FFFFFF'
            },
            series: [{
                name: '良品数',
                type: 'column',
                data: passdata,

            }, {
                name: '不良品数',
                type: 'column',
                data: faildata,

            },
                {
                    name: '堆积数',
                    type: 'column',
                    data: data,
                }]
        });

        FailChart = Highcharts.chart('graph-2', {
            chart: {
                //height: (4 / 16 * 100) + '%' // 16:9 ratio
            },
            title: {
                text: '各工位今日不良率'
            },
            credits: {
                enabled: false // 禁用版权信息
            },
            exporting: {
                enabled: false
            },
            xAxis: [compAxis],
            yAxis: [{ // Secondary yAxis
                title: {
                    text: '不良率',
                    style: {
                        color: '#ffffff',
                        fontSize: '1em'
                    }
                },
                min: 0,
                max: 1.5 * failratemax,
                labels: {
                    formatter: function () {
                        return Math.floor(this.value) + "%";
                    },
                    style: {
                        color: '#ffffff',
                        fontSize: '20px'
                    },
                    rotation: 270,
                },

            }],
            tooltip: {
                shared: true
            },
            legend: {
                layout: 'vertical',
                align: 'right',
                x: 0,
                verticalAlign: 'top',
                y: 0,
                floating: true,
                backgroundColor: (Highcharts.theme && Highcharts.theme.legendBackgroundColor) || '#FFFFFF'
            },
            series: [{
                name: '不良率',
                type: 'spline',
                data: ratedata,
                color: 'red',
                tooltip: {
                    valueSuffix: '%'
                }
            }]
        });

    }

    function updateChart(sectorLoad) {
        var data = [];
        var ratedata = [];
        var stackmax = 0;
        var failmax = 0;
        var passdata = [];
        var faildata = [];
        var passmax = 0;
        var failratemax = 0;
        for (i = 0; i < passNumber.length; i++) {
            if (stackmax < stackeverynum[i]) {
                stackmax = stackeverynum[i];
            }
            if (failmax < failNumber[i]) {
                failmax = failNumber[i];
            }
            if (failratemax < failrate[i]) {
                failratemax = failrate[i];
            }
            if (passmax < passNumber[i]) {
                passmax = passNumber[i];
            }
            data.push(stackeverynum[i]);
            ratedata.push(Math.floor(failrate[i]));
            passdata.push(passNumber[i]);
            faildata.push(failNumber[i]);
        }
        failratemax = Math.max(failratemax, 5);
        stackmax = Math.max(stackmax, 5);
        failmax = Math.max(failmax, 5);
        passmax = Math.max(passmax, failmax, stackmax);


        // https://api.hcharts.cn/highcharts#Series.addPoint;

        stackAndFailChart.series[0].setData(passdata);
        stackAndFailChart.series[1].setData(faildata);
        stackAndFailChart.series[2].setData(data);
        FailChart.series[0].setData(ratedata);

        if (failratemax > FailChart.yAxis[0].getExtremes().max * 1.3 || failratemax < FailChart.yAxis[0].getExtremes().max * 0.5) {
            FailChart.yAxis[0].setExtremes(0, failratemax * 1.5);
        }
        if (passmax > stackAndFailChart.yAxis[0].getExtremes().max * 1.3 || passmax < stackAndFailChart.yAxis[0].getExtremes().max * 0.5) {
            stackAndFailChart.yAxis[0].setExtremes(0, passmax * 1.5);
        }

        showHistoryData();
        showRedRanking();
        showBlackRanking();


        var secNames = [];

        sectorID.forEach(function (val) {
            secNames.push(getSectorName(val));
        });

        if (JSON.stringify(secNames) !== lastTimeSecNames) {
            lastTimeSecNames = JSON.stringify(secNames);
            FailChart.xAxis[0].setCategories(secNames);
            stackAndFailChart.xAxis[0].setCategories(secNames);
        }
    }

    var lastTimeSecNames = "";

    var stackAndFailChart = null;
    var FailChart = null;

})();


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;
}


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 = [];
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;
}

var RedRank = (function() {
    this.fetchPos = 0;
    this.multipleData = [];
    this.loader = (function () {
        this.load = function(pos) {
            this.multipleData = new Array();
            for(let i = 0; i < 8; i++){
                this.multipleData[i] = new DataObjectService("fcBiWorkderDaily", AUTO & FAST, "?type="+i);
            }
            get_data(`fcBiWorkerDaily/get?type=${RedRank.fetchPos}`, RedRank.loader.onSuccess);
        };
        this.consecutiveLoad = function(){
            RedRank.fetchPos = (RedRank.fetchPos + 1) % 8;
            load(RedRank.fetchPos);
        };
        this.onSuccess = function(res, code){

        };
    })();
})();

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] = '';
        }
    }


}*/