﻿
var tempCache = {
    repeatTemp: []
}

var COM = {
    repeat: function (s, t) {//限制执行频率，默认为15秒 允许执行时返回false
        t = t ? t * 1000 : 15000;//毫秒
        var time = microtime();
        if (!tempCache.repeatTemp[s]) {
            tempCache.repeatTemp[s] = time;
            return false;//允许
        } else {
            var ts = t - (time - tempCache.repeatTemp[s]);
            ts = parseInt(ts / 1000);
            if (ts > 0) {
                alert("评论限制：还有" + ts + "秒才可以再评论！");
                return true;//禁止执行
            } else {
                tempCache.repeatTemp[s] = time;//更新时间
                return false;//允许
            }
        }
    }
}
//获取毫秒级时间戳
function microtime() {
    return new Date().getTime();
}


function seoFeedBackClick(answerId, feedType, callback) {
    var btn = COM.repeat('seoFeedBackClick');
    if (!btn) {
        var postUrl = "/QuestionSeo/AddFeedBackSeoPost/";
        if (typeof (webRootUrl) != 'undefined') {
            postUrl = webRootUrl + postUrl;
        }
        $.ajax({
            type: "POST",
            url: postUrl,
            data: { "answerId": answerId, "type": feedType },
            cache: false,
            success: function (result) {
                var resultData = JSON.parse(result);
                if (resultData.Bool) {
                    callback(true);
                } else {
                    alert(resultData.Error.ErrorMessage);
                    callback(false);
                }

            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert("发生错误，请稍候重试");
            }
        });
    } else { return; }

}
function seoAppendFeedBackClick(answerId, appendId, feedType, callback) {
    var btn = COM.repeat('seoAppendFeedBackClick');
    if (!btn) {
        var postUrl = "/QuestionSeo/AppendFeedBackSeoPost/";
        if (typeof (webRootUrl) != 'undefined') {
            postUrl = webRootUrl + postUrl;
        }
        $.ajax({
            type: "POST",
            url: postUrl,
            data: { "answerId": answerId, "appendId": appendId, "type": feedType },
            cache: false,
            success: function (result) {
                var resultData = JSON.parse(result);
                if (resultData.Bool) {
                    callback(true);
                } else {
                    alert(resultData.Error.ErrorMessage);
                    callback(false);
                }

            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert("发生错误，请稍候重试");
            }
        });
    } else { return; }
}

function feedBackClick(answerId, feedType, callback) {
    var btn = COM.repeat('feedBackClick');
    if (!btn) {
        var postUrl = "/Question/AddFeedBackPost/";
        if (typeof (webRootUrl) != 'undefined') {
            postUrl = webRootUrl + postUrl;
        }
        $.ajax({
            type: "POST",
            url: postUrl,
            data: { "answerId": answerId, "type": feedType },
            cache: false,
            success: function (result) {
                var resultData = JSON.parse(result);
                if (resultData.Bool) {
                    callback(true);
                } else {
                    alert(resultData.Error.ErrorMessage);
                    callback(false);
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert("发生错误，请稍候重试");
            }
        });
    } else { return; }
}

function appendFeedBackClick(answerId, appendId, feedType, callback) {
    var btn = COM.repeat('appendFeedBackClick');
    if (!btn) {
        var postUrl = "/Question/AppendFeedBackPost/";
        console.log(typeof (webRootUrl));
        if (typeof (webRootUrl) != 'undefined') {
            postUrl = webRootUrl + postUrl;
        }
        $.ajax({
            type: "POST",
            url: postUrl,
            data: { "answerId": answerId, "appendId": appendId, "type": feedType },
            cache: false,
            success: function (result) {
                var resultData = JSON.parse(result);
                if (resultData.Bool) {
                    callback(true);
                } else {
                    alert(resultData.Error.ErrorMessage);
                    callback(false);
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert("发生错误，请稍候重试");
            }
        });
    } else { return; }
}

function pcAppend(appendUrl, askUrl) {
    debugger;
    var html = '';
    html += '<div class="add-ask-dialog">';
    html += '<p class="main-tip main-tip-small">您正在针对上述答案进行追问，<br/>如果您不是追问该答案，请提问新问题。</p>';
    html += '<div class="btns">';
    html += '<a href="javascript:;" class="btn btn-green confirm">继续追问</a>';
    html += '<a href="javascript:;" class="btn btn-blue cancel">提问新问题</a>';
    html += '</div></div>';

    var dialog = new FD.Dialog({
        width: "450px",
        title: "提示",
        content: html,
        onOpened: function (dia) {
            var $box = $(dia.box);
            $box.find(".confirm").click(function () {
                location.href = appendUrl;
                dialog.close();
            });
            $box.find(".cancel").click(function () {
                location.href = askUrl;
                dialog.close();
            });
        }
    });
}


function touchAppend(appendUrl, askUrl) {
    FD.showConfirm({
        msg: "您正在针对上述答案进行追问，如果您不是追问答案，请提问新问题。",
        btnText: ["继续追问", "提交新问题"],
        onSubmit: function () {
            location.href = appendUrl;
        },
        onCancel: function () {
            location.href = askUrl;
        }
    });

}