var flowID = [];
var flowname = [];
var periodID = [];
var periodname = [];

getUserId();

function page_document_load() {
    new_data_group("denglu",function(gp){
        if(gp.counter === 2){
            showEachSpot();
            showEachSpot1();
        }
    });
    checkIfExists();
    firsttime_load_spots_data();
    secondtime_load_spots_data();
}

function firsttime_load_spots_data() {
    group_get_data("denglu","fcFlow/list", first_success)
}

function first_success(obj, sta) {
    let output = first_parse(this, obj);
    //在这里排序 spo\
    console.log(" output : ");
    console.log(output);
    let i, j, k;
    j = 0;
    k = 0;
    for (i = 0; i < output.list.length; i++) {
        //if (userId+"" === ""+output.list[i].masterStaffNo) {
            flowID[j] = output.list[i].flowId;
            flowname[j] = output.list[i].flowName + "(产线)";
            j++;
        //} else {
        //    flowID[j] = 0;
        //    flowname[j] = output.list[i].flowName + "(无权限)";
        //    j++;
        //    k++;
        //}
    }
    if (k === output.list.length) {
        send_alert("请注意：您不是任一产线负责人<br>因此，您不得开始班次<br>如果有误，请修改产线的负责人信息 ");
    }
}

function secondtime_load_spots_data() {
    group_get_data("denglu","fcWorkPeriod/list", second_success);
}

function second_success(obj, sta) {
    var history = first_parse(this, obj).list;
    var i, j;
    console.log(" history : ");
    console.log(history);
    for (i = 0; i < history.length; i++) {
        periodID[i] = history[i].periodId;
        periodname[i] = history[i].periodName;
    }
    showEachSpot1();
}

function showEachSpot1() {
    var htmlbody = "", one_person = "";
    var i = 0;
    var triGap = periodID.length;
    htmlbody = "";
    for (i = 0; i < triGap; i++) {
        htmlbody = getSpotHtml1(htmlbody, i);
    }
    $("#period_ID").html(htmlbody);
    //console.log(htmlbody);
}

function getSpotHtml1(htmlbody, i) {
    htmlbody += "";
    htmlbody += `<option value="${periodID[i]}">${periodname[i]}</option>`;
    htmlbody += "";
    return htmlbody;
}

function showEachSpot() {
    var htmlbody = "", one_person = "";
    var i = 0;
    var triGap = flowID.length;
    htmlbody = "";
    for (i = 0; i < triGap; i++) {
        htmlbody = getSpotHtml(htmlbody, i);
    }
    $("#pro_areaCode").html(htmlbody);
    //console.log(htmlbody);
}

function getSpotHtml(htmlbody, i) {
    htmlbody += "";
    htmlbody += `<option value="${flowID[i]}">${flowname[i]}</option>`;
    htmlbody += "";
    return htmlbody;
}

function moveToStart(btn) {
    $(btn).attr("disabled", "disabled");
    var proAreaCode = $("#pro_areaCode").val();
    if(""+proAreaCode === "0"){
        send_alert("请确认您拥有本产线的权限");
        $(btn).attr("disabled","");
        return;
    }
    var periodnum = $("#period_ID").val();
    get_data("/fcWorkSchedule/startSchedule?flowId=" + proAreaCode + "&periodId=" + periodnum + "&userId="+userId, startToSchedule);
}

function startToSchedule(obj) {
    let output = first_parse(this, obj);
    if(output != undefined){
        var proAreaCode = $("#pro_areaCode").val();
        send_alert("即将跳转 拉长页面: scheduleId:"+output.scheduleId);
        location.href = 'lazhang.html?scheduleId=' + output.scheduleId + '&flowId=' + proAreaCode;
    }else{
        send_alert("开始失败！");
    }
}

function checkIfExists(){
    get_data("/fcWorkSchedule/remaining?status=100",on_receive_exist);
}

function getRemainHTML(obj) {
    var pdName = "";
    for(i=0;i<periodID.length;i++){
        if(periodID[i] == obj.periodId){
            pdName = periodname[i];
            break;
        }
    }
    return `<a href='lazhang.html?scheduleId=${obj.scheduleId}&flowId=${obj.flowId}&userId=${userId}' style="color:#d3a60c"> ${obj.flowName} ${obj.workDay} ${pdName}</a><br> `
}


function send_remain(a, tag) {
    $("#"+tag).html(a);
    $("#"+tag).show();
    return;
}

function on_receive_exist(objs, sta){
    let obj = first_parse(this, objs);
    console.log(obj);
    let wholehtml = "";
    if(obj != null && obj != undefined && obj.list != null){
        if(obj.list.length === 0){
            wholehtml = "当前没有正在进行的班次<br>请开始班次！";
            send_remain(wholehtml, "mpRemain");
            return;
        }
        let i = 0;
        wholehtml = "正在进行的班次： <br>";
        for(i=0;i<obj.list.length;i++){
            wholehtml += getRemainHTML(obj.list[i]);
        }
        send_remain(wholehtml, "mpRemain");
        send_remain("请及时关闭已完成班次，否则会影响工人结算、新班次数据统计！", "mpexist")
    }else{
        send_alert("查询失败");
    }
}

function logout(){
    setCookieWithTimeout("doibyUser","",-10);
    console.log(getCookie("doibyUser"));
    location.href="sign-in1.html";
}

