///声明常用的变量
var PageIndex = 1;
var PageSize = 10;
var orderfile = "QA_CreateDate";
var ordertype = "desc";
var count = 0;
var QAobj = "";
var sqlwhere = "";
var parentid = ""; //父级id
var childid = "";
var take = "";

///二级菜单显示隐藏 id,对象
function childShow(id) {
//    $("#ParntType").each(
//    function () {
//        $(this).find('a').each(function () { $(this).attr("class", ""); }); //取消所有a标签的样式
//        $(this).find('div').each(function () { $(this).hide(); }); //给所有子集隐藏
//    }
//    );
    //$("#Aparnt_" + id).attr("class", "active"); //所选A标签添加样式
    //$("#child_" + id).show(); //所选子集显示
    parentid = " and (QA_TypeId='" + id + "' or QA_TypeId IN(select QAType_Id from TKS_QAType where QAType_PrantId='" + id + "'  ))"
    childid = "";
    PageIndex = 1;
    TakeStatus(2);
}
///二级类型 标签选中样式
//function childAcss(fid, id) {
//    sqlwhere = "";
//    $("#" + fid).find('a').each(function () {
//        $(this).attr("class", "");
//    });
//    $("#Achild_" + id).attr("class", "active");

//    childid = " and (QA_TypeId='" + id + "')";
//    PageIndex = 1;
//    TakeStatus(2);
//}

///获取集合
function GetQA() {
    if (childid != "") {
        sqlwhere = childid;
    } else {
        sqlwhere = parentid;
    }
    sqlwhere += take;
    var param = "PageIndex=" + PageIndex + "&&PageSize=" + PageSize + "&&orderfile=" + orderfile + "&&ordertype=" + ordertype + "&&sqlwhere=" + sqlwhere;
    getAjax("GetQA.ashx", param, function (data) {
        if (data != "1") {
            var json = eval('(' + data + ')')
            // var json = strToJson();
            count = json.count;
            QAobj = eval('(' + json.str + ')')
            context();
        } else {
            alert("网络异常");
        }
    });
}
///集合赋值到页面
function context() {
    var html = "";
    for (var i = 0; i < QAobj.length; i++) {
        //html += "<tr> <td class='tuxiang'><img src='/Mall/User_Image.ashx?id=" + QAobj[i].QA_UserID + "' width='59' height='59' /></td><td class='neirong'><a href='/Mall/QA/QAInfo.aspx?qaid=" + QAobj[i].QA_ID + "' target='_blank'>" + QAobj[i].QA_Title + "</a><br />" + QAobj[i].LastUserName + " 回复于" + JsonDate(QAobj[i].LastDataTime) + "</td><td>" + QAobj[i].LastCount + "/" + QAobj[i].QA_Count + "</td></tr>";
        html += "<li><aside class=\"q-head-pic\"><img src=\"/Mall/User_Image.ashx?id=" + QAobj[i].QA_UserID + "' alt=\"\" /><p class=\"hLh30 txtOf\"><span class=\"c-999\">"+QAobj[i].QA_UserName+"</span></p></aside><div class=\"q-txt-box\"><a class=\"replyBrowseNum\" href=\"#\" title=\"\"><div class=\"replyNum\"><span class=\"r-b-num\">"+QAobj[i].LastCount+"</span><p class=\"hLh30\"><span class=\"c-999 f-fA\">回答数</span></p></div><div class=\"browseNum\"><span class=\"r-b-num\">"+QAobj[i].QA_Count+"</span><p class=\"hLh30\"><span class=\"c-999 f-fA\">浏览数</span></p></div></a>";
        html += "<h3 class=\"hLh30 txtOf\"><em class=\"icon16 q-tw\">&nbsp;</em><a href=\"/Mall/QA/QAInfo.aspx?qaid="+QAobj[i].QA_ID+" title=\"\" class=\"fsize16 c-333 vam\">"+QAobj[i].QA_Title+"</a></h3><h3 class=\"hLh30 txtOf mt5\"><em class=\"icon16 q-hd\">&nbsp;</em>";
        if(QAobj[i].LastUserName!= "匿名用户")
        {
            html += "<span class=\"fsize12 c-999 vam\"><tt class=\"c-ccc f-fM mr5\">[最新回答]</tt>"+QAobj[i].LastExplain+"</span>";
        }else
        {
            html += "<span class=\"fsize12 c-999 vam\">哈~~~ 此问题大家还有苦思冥想中...</span>";
        }
        html += "</h3><div class=\"mt15\"><span class=\"c-ccc fl vam\">" + JsonDate(QAobj[i].QA_CreateDate) + "</span><div class=\"fl ml20 pt10\"><div class=\"taglist clearfix\"><a title=\"Premiere\" data-id=\"5\" class=\"list-tag\" href=\"#\">" + QAobj[i].QA_TypeName + "</a></div></div><div class=\"clear\"></div></div></div></li>";
    }
    if (html == "") {
        html == "没有信息咯";
    }
    $("#center").html(html);
    $("#page_input").val(PageIndex);
}
function getAjax(url, parm, callBack) {
    $.ajax({
        type: 'post',
        dataType: "text",
        url: url,
        data: parm,
        cache: false,
        async: false,
        success: function (msg) {
            callBack(msg);
        }
    });
}
///上一页
function PageUp() {
    if (PageIndex > 1) {
        PageIndex--;
    }
    GetQA();
}
///下一页
function PageDown() {
    var PageCount = count / PageSize;
    if ((count % PageSize) != 0) {
        PageCount++;
    }
    if (PageIndex + 1 > PageCount) {
        alert("已经是最后一页");
        return;
    }
    PageIndex++;
    GetQA();
}
///页面跳转
function goPage() {
    var pagenum = $("#page_input").val();
    if (pagenum <= 0) {
        alert("页码不能小于等于0"); return;
    }
    PageIndex = pagenum;
    GetQA();
}
///查询状态 全部 已解决 未解决
function TakeStatus(obj) {
    if (obj != 2) {
        take = " and QA_Take=" + obj;
    } else {
        take = "";
    }
    //样式
    $("#takediv").find("a").each(
function () { $(this).attr("class", ""); });
    $("#take_" + obj).attr("class", "jj_btn_hover");
    GetQA();
}
///排序
function onchangorder() {
    orderfile = $("#order").val();
    GetQA();
}
///清除全部条件
function GetAll() {
    childid = "";
    parentid = "";
    take = "";
    PageIndex = 1;
    $("#ParntType").each(
    function () {
        $(this).find('a').each(function () { $(this).attr("class", ""); }); //取消所有a标签的样式
        $(this).find('div').each(function () { $(this).hide(); }); //给所有子集隐藏
    }
    );
    $("#GetAll").attr("class", "active"); //所选A标签添加样式
    TakeStatus(2);
}
///时间转换日期
function JsonDate(obj) {
    var data = "/Date(1345517741000)/";
    var date = new Date(parseInt(data.replace("/Date(", "").replace(")/", ""), 10));
    return date.getFullYear() + "-" + (date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1) + "-" + (date.getDate() < 10 ? "0" + date.getDate() : date.getDate()) + " " + (date.getHours() < 10 ? "0" + date.getHours() : date.getHours()) + ":" + (date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes());
}
//发表新帖
function AddNewQA() {
    getAjax("AddTKS_QADetail.ashx", "", function (rs) {
        if (rs != "2") {
            location.href = "QA_Form.aspx";
        } else {
            //location.href = "/Frame/Login.aspx";
		denglu();
        }
    });
}

function AddTKS_QA() {
    var key = $("#key").val();
    var tit = $("#tit").val();
    var type = $("#type").val();
    editor.sync();
    var Content = $("#QA_Content").val();
    var obj = document.getElementById("type");
    var typename = obj.options[obj.selectedIndex].text

    if (tit.length <= 0) {
        return alert("标题不能为空");
    }
    if (Content.length <= 0) {
        return alert("内容不能为空");
    }
    var param = "tit=" + tit + "&type=" + type + "&Content=" + Content + "&typename=" + typename + "&key=" + key;
    getAjax("AddTKS_QA.ashx", param, function (rs) {
        if (rs == "1") {
            alert("发表成功");
            location.href = "/Mall/QA/QA_Index.aspx";
        } else if (rs = "3") {
            alert("操作成功");
        } else if (rs == "0") {
            alert("发表失败");
        } else if (rs == "2") {
            location.href = "/Frame/Login.aspx";
        }
    });
}
function AddTKS_QADetail() {
    //editor.sync();
    var content = $("#QADetail_Content").val();
    var qaid = $("#key").val();
    if (content.length < 5) {
        alert("回复必须大于5个字");
        return false;
    }
    getAjax("AddTKS_QADetail.ashx", "content=" + content + "&qaid=" + qaid, function (rs) {
        if (rs == "1") {
            alert("发表评论成功!");
            location.reload();
        } else if (rs == "0") {
            alert("发表评论失败");
        } else if (rs == "2") {
            //未登录
            location.href = "/Frame/Login.aspx";
        }
    });
}
//采纳评论
function onbindTake(qadeid, qaid) {
    var parm = "qadeid=" + qadeid + "&qaid=" + qaid;
    getAjax("AddTKS_QA.ashx", parm, function (rs) {
        if (rs == "1") {
            alert("采纳成功");
            location.reload();
        } else if (rs == "0") {
            return alert("操作失败");
        } else {
            alert('与服务器断开链接,请重新登录');
        }
    });
}
function DeleteQAD(obj) {
    getAjax("/Mall/QA/DeleteQA.ashx", "key=" + obj, function (rs) {
        if (rs == 1) {
            alert("操作成功");
            location.reload();
        } else {
            alert("网络繁忙");
        }
    })
}
