var passNumber = [];
var failNumber = [];
var failrate = [];
var stackeverynum = [];
var failnum = 0;
var num = 0;
var n = 0;
var sectorID = [];
var namedata = [];
var lazhang = {};
var scheduleId = utils_get_param("scheduleId");
var flowId = utils_get_param("flowId");
var name1,name2;

function first_success(obj, sta) {
    let output = first_parse(this, obj);
    let i, j, k, z;
    failnum = 0;
    let x = 0;
    let worksum = [];
    z = 0;

    if (output.list == undefined || output.list.length == 0) {
        send_alert("没有数据");
    }

    output.list.forEach(function(val,it,arr){
        var isExist = false;
        userIDs.forEach(function(vals){
            if(vals == val.userId){
                isExist = true;
            }
        });
        if(!isExist) userIDs.push(val.userId);
    });

    if (output.list.length > 0) {
        sectorID[0] = getSectorId(output.list[0].spotId);

        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[j];
                    failNumber[k] = output.list[i].failNum + failNumber[j];
                    stackeverynum[k] = output.list[i].due + stackeverynum[j];
                    z++;
                }
            }
            if (failNumber[k] + passNumber[k] == 0) {
                failrate[k] = 0;
            }
            else {
                failrate[k] = failNumber[k] / (failNumber[k] + passNumber[k]) * 100;

            }
            /*stackeverynum[k]=stackeverynum[k]/z;*/

            ///

        }
    }
    get_data("fcFlow/list?flowId="+ flowId,name_Success);


    for (i = 1; i <= 3; i++) {
        load_flows_history_data(i);
    }
    showChart();
    lazhang.timer = setInterval(request_data_realtime, 1000);

}
function name_Success(obj, sta) {
    var factory = first_parse(this, obj);
    var i, j, t;
    console.log(" factory : ");
    console.log(factory);

    name1=factory.list[0].flowPlace;
    name2=factory.list[0].flowName;
    document.getElementById("flow_title").innerHTML = name1+"-"+name2 + " 度彼科技";
    document.title =  name1 + " " + name2 + " | 流水线管理 | 度彼科技";

}

function request_data_realtime() {
    get_data("fcWorkScheduleLoad/list?scheduleId=" + scheduleId, realtime_success);
    //load_flows_history_realtime_data();
    load_table_red_rank();
    load_table_red_rank1();
    load_table_black_rank();
    load_table_black_rank1();

}

function load_flows_history_data(nnm) {
    // 请注意type必须在结尾
    get_data("fcBiFlowDaily/list?scheduleId="+scheduleId+ "&flowId=" + flowId+"&type=" + nnm , history_success);
}

function load_flows_history_realtime_data() {
    // 请注意type必须在结尾
    get_data("fcBiFlowDaily/list?flowId=" + flowId +"&type=1", history_success);
}
function get_stop_scheudle1() {
    var out = confirm("真的要下班吗？");
    if(out == true){
        get_stop_scheudle();

    }
    else{

    }
}

function show_clear_panel(){
    $("#clearModal").modal('show');
}

var executed = false;
var finished = true;
var directCount = -1;

function clear_one_data(){
    executed = false;
    directCount = -1;
    finished = true;
    var hand = $("#handInput").val();
    var hd = 0;
    if(hand.length > 0){
       hd = parseInt(hand);
    }
    get_data("endpoint/clearBoard?flowId="+flowId+"&handheldId=" + hd, clearSuccess);
}

function clearSuccess(obj, sta){
    console.warn("-------  手持设备清空  ----------");
    console.warn(this);
    send_alert("发送清空请求成功, 正在等待采集端进行处理...");
    finished = false;
    setTimeout(on_timeout_clear, 8000);
    setTimeout(recurseget,300);
    //var output = first_success(this, obj);
}

function recurseget(){
    if(finished) return;
    get_data("endpoint/heartbeat?flowId=" + flowId, onheart);
    setTimeout(recurseget,100);
}

function onheart(obj,sta){
    var ouy = {};
    if(typeof(obj) === "string"){
        ouy = JSON.parse(obj);
    }else{
        ouy = obj;
    }


    if(typeof(ouy) === "object"){
        if(ouy.ret === "10000"){
            //ok
            if(ouy.model == null || ouy.model === undefined){
                executed = true;
                finished = true;
            }else if(typeof(obj.model) === "object"){
                // 没有拿到，继续
                if(obj.model.list != null && obj.model.list.length > 0){
                    var direct = obj.model.list[0];
                    if(direct.eventType+"" !== "500"){
                        // OK
                        executed = true;
                        finished = true;
                    }else if(direct.arg2+"" !== ""+$("#handInput").val()){
                        // OK
                        executed = true;
                        finished = true;
                    }else{
                        directCount = direct.counter;
                    }
                }
            }
        }
    }
}

function on_timeout_clear(){
    finished = true;
    if(executed){
        // 执行成功
    }else{
        if(directCount >= 0){
            get_data("endpoint/ack?counter="+directCount,function(){console.log("撤销操作成功");console.log(this);});
            directCount = -1;
            send_alert("请求已经超时！请注意，采集端可能没有执行成功。即将撤销您的请求。");
        }else{
            send_alert("请求已经超时！请注意，采集端可能没有执行成功。");
        }
    }
}


function get_stop_scheudle() {
    get_data("fcWorkSchedule/stopSchedule?userId="+userId+"&scheduleId=" + scheduleId, stopSuccess);

}

function stopSuccess(obj, sta) {
    send_alert("下班操作成功！ <a href='denglu.html'> 点击这里返回控制主页 </a>");
}


function add_success(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();

}

