﻿function getNewInfo(gidkey) {
    function GetCurrentEvents_success(data) {
        requeststatus = false;
        if (data.length == 0) {
            FailHandle();
        }
        else {
            bindDate(data);
        }
    }

    function GetCurrentEvents_error() {
        requeststatus = false;
        FailHandle();
    }

    function GetCurrentEvents_before() {
        requeststatus = true;
    }

    ajax_json_getV1('Services/GetSnapActive.ashx?gid=' + gidkey + '&ns=' + ns + '&r=' + Math.random(), 10000, true, GetCurrentEvents_success, GetCurrentEvents_error, GetCurrentEvents_before);
}

function bindDate(data) {

    snaps.sysT = data.sysT;
    if (data.snapRecords != null && data.snapRecords != "undefined") {
        for (i = 0; i < data.snapRecords.length; i++) {
            //更新snaps数据
            if (updateSnaps(data.snapRecords[i])) {
                if (pageArea == "goods") {
                    //有更新，获取抢购记录
                    getRecordsList(data.snapRecords[i].gid);
                }
                if (pageArea == "seat") {
                    //有更新，获取抢购记录
                    getSeatSnapRecordsList(data.snapRecords[i].gid);
                }
            }
        }
    }
}
function showSnapGoods() {
    for (i = 0; i < snaps.snapRecords.length; i++) {
        updateShowDiv(snaps.snapRecords[i]);
    }
}
function updateSnaps(item) {
    for (n = 0; n < snaps.snapRecords.length; n++) {
        if (snaps.snapRecords[n].gid == item.gid && (snaps.snapRecords[n].lti < item.lti || snaps.snapRecords[n].st != item.st || snaps.snapRecords[n].rse != item.rse)) {
            snaps.snapRecords[n].uid = item.uid;
            snaps.snapRecords[n].atp = item.atp;
            snaps.snapRecords[n].rse = item.rse;
            snaps.snapRecords[n].price = item.price;
            snaps.snapRecords[n].una = item.una;
            snaps.snapRecords[n].lv = item.lv;
            snaps.snapRecords[n].urs = item.urs;
            snaps.snapRecords[n].lti = item.lti;
            snaps.snapRecords[n].cti = item.cti;
            snaps.snapRecords[n].sbti = item.sbti;
            snaps.snapRecords[n].seti = item.seti;
            snaps.snapRecords[n].st = item.st;
            return true;
        }
    }
    ns = false;
    return false;
}

function updateShowDiv(item) {
    cdshow(item, true);
    if (pageArea == "list") {
        $("#sprice" + item.gid).html("¥" + item.price.toFixed(2).toString());
        if (item.una == "") {
            $("#suser" + item.gid).html("------");
        }
        else {
            $("#suser" + item.gid).html(item.una);
            if (item.lv != null && item.lv != "") {
                $("#suser" + item.gid).removeClass().addClass("zi_12" + getSnapUserStyle(item.lv));
            }
        }

        if (item.urs != null && item.urs != "") {
            $("#m_g_rtimes_" + item.gid).html(item.urs); 
        }
        
    }
    if (pageArea == "goods") {
        $("#sprice" + item.gid).html("¥" + item.price.toFixed(2).toString());
        if (item.una == null || item.una == "") {
            $("#a_suser" + item.gid).html("----------");
            $("#a_suser" + item.gid).attr("href", "#");
        }
        else {
            $("#a_suser" + item.gid).html(item.una);
            $("#a_suser" + item.gid).attr("href", GlobalHref.UserShow(item.uid));
            if (item.lv != null && item.lv != "") {
                $("#a_suser" + item.gid).removeClass().addClass("zi_12" + getSnapUserStyle(item.lv) + "_x");
            }
        }

        $("#face" + item.gid).attr("src", "http://image.hudou.com//Face/s_" + item.uid + ".jpg")
        $("#a_face" + item.gid).attr("href", GlobalHref.UserShow(item.uid));

        var pricetemp = item.price;
        $("#curprice" + item.gid).html(item.price.toFixed(2).toString());
        var traprice = parseFloat($("#traprice" + item.gid).html());
        var totalprice = pricetemp + traprice;
        $("#totalprice" + item.gid).html(totalprice.toFixed(2).toString());
       
    }
    if (pageArea == "seat") {
        $("#sprice" + item.gid).html("¥" + item.price.toFixed(2).toString());
        if (item.una == null || item.una == "") {
            $("#a_suser" + item.gid).html("----------");
            $("#a_suser" + item.gid).attr("href", "#");
        }
        else {
            $("#a_suser" + item.gid).html(item.una);
            $("#a_suser" + item.gid).attr("href", GlobalHref.UserShow(item.uid));
            if (item.lv != null && item.lv != "") {
                $("#a_suser" + item.gid).removeClass().addClass("zi_12" + getSnapUserStyle(item.lv) + "_x");
            }
        }
        $("#face" + item.gid).attr("src", "http://image.hudou.com//Face/s_" + item.uid + ".jpg")
        $("#a_face" + item.gid).attr("href", GlobalHref.UserShow(item.uid));

        var pricetemp = item.price;
        $("#curprice" + item.gid).html(item.price.toFixed(2).toString());
        var traprice = parseFloat($("#traprice" + item.gid).html());
        var totalprice = pricetemp + traprice;
        $("#totalprice" + item.gid).html(totalprice.toFixed(2).toString());

    }


}

//iscurrent 是否有更新
function cdshow(item, iscurrent) {
    //判断是否是订位抢购
    if (item.atp == 2 && item.rse > 0) {
        //$("#cdtime" + item.gid).html("无剩余席位<span>24</span>小时后开抢");
    }
    else {
        switch (item.st) {
            case 2: //竞拍开放
                {
                    var currentT;
                    //判断是否处于抢购时间内
                    if (snaps.sysT >= item.sbti && snaps.sysT <= item.seti) {
                        //判断是否上次抢购是否本期内
                        if (item.lti < item.sbti) {
                            currentT = DateTimeUtility.getTimeSpanToSeconds(snaps.sysT, item.sbti);
                        }
                        else {

                            currentT = DateTimeUtility.getTimeSpanToSeconds(snaps.sysT, item.lti);
                        }
                        if (currentT < item.cti) {
                            if (item.cti - currentT > 10) {
                                $("#cdtime" + item.gid).removeClass().addClass("wkq1").html("<span>" + (item.cti - currentT) + "</span>秒");
                            }
                            else {
                                $("#cdtime" + item.gid).removeClass().addClass("wkq2").html("<span>" + (item.cti - currentT) + "</span>秒");
                            }
                        }
                        else {
                            //超期，等待产品状态更改
                            ns = true;
                            $("#cdtime" + item.gid).removeClass().addClass("wkq2").html("<span>正在确认</span>");
                        }
                    }
                    else if (snaps.sysT < item.sbti) {
                        var sdate1 = DateTimeUtility.GetTimeFromTciks(item.sbti);
                        var sdate2 = DateTimeUtility.GetTimeFromTciks(snaps.sysT);
                        if (DateTimeUtility.getShortDateString(sdate1) == DateTimeUtility.getShortDateString(sdate2)) {
                            $("#cdtime" + item.gid).removeClass().addClass("wkq").html("今天" + DateTimeUtility.PadTime(sdate1.getHours()) + "点" + DateTimeUtility.PadTime(sdate1.getMinutes()) + "分开抢");
                        }
                        else {
                            $("#cdtime" + item.gid).removeClass().addClass("wkq").html(DateTimeUtility.getLongDateString(sdate1) + "开抢");
                        }
                    }
                    else if (snaps.sysT > item.seti) {

                        var sdate1 = DateTimeUtility.GetTimeFromTciks(item.sbti);
                        $("#cdtime" + item.gid).removeClass().addClass("wkq").html("抢购超时，等待延长"); //
                        //$("#cdtime" + item.gid).removeClass().addClass("wkq").html((sdate1.getMonth() + 1) + "月" + sdate1.getDate() + "日继续"); //TODO:时间判断
                    }
                }
                break;
            case 3: //竞拍停拍
                {
                    $("#cdtime" + item.gid).removeClass().addClass("wkq").html("竞拍关闭");
                }
                break;
            case 4: //抢购结束
                {
                    $("#cdtime" + item.gid).removeClass().addClass("wkq").html("抢购结束");
                }
                break;
            case 5:
                {
                    $("#cdtime" + item.gid).removeClass().addClass("wkq").html("无人参与");
                }
                break;
            default:
                $("#cdtime" + item.gid).removeClass().addClass("wkq").html("无人参与");
                break;
        }
    }
}
//公共函数部分
function refPage() {
    //TODO:ajax刷新载入
    return false;
}
function FailHandle(err) {
    if (errorNum > 300) {
        action_stop(err);
    }
    errorNum += 1;
}
function dzSelect(id) {
    $("#dzselect" + id).removeClass().addClass("rb0");
    $("#hdselect" + id).removeClass().addClass("rb1");
}
function hdSelect(id) {
    $("#dzselect" + id).removeClass().addClass("rb1");
    $("#hdselect" + id).removeClass().addClass("rb0");
}


