$(function () {

    //操作筛选
    $("#messageListSerach_Btn").click(function () {

        var para = "";
        if ($("#message_select").val() != "-1") para += "&reply=" + $("#message_select").val();
        if ($("#s_key").val() != "-1") para += "&key=" + $("#s_key").val();
        location.href = $(this).attr("href") + para + "";
        return false;
    })

    //回复按钮
    $("a[rel='reply_btn']").click(function () {
        $(this).next("div").toggle();
        $(this).next("div").find("textarea").val("");
        return false;
    })

    //加载全部
    function allMessageRelpy() {
        $(".item-list").each(function () {
            showMessageReply($(this));
        })
    }
    allMessageRelpy();

    //加载留言回复
    function showMessageReply(obj) {
        $.ajax({
            type: "POST",
            url: "Ajax/Message.ashx",
            data: { 'com': 'getmessagereply', 'mid': obj.attr("rel") },
            datatype: "json",
            beforeSend: function (data, textStatus) {
                obj.html("loading...");
            },
            success: function (data, textStatus) {
                var ShowHtml = "";
                var JosnData = eval(data);
                if (JosnData != null) {
                    for (var i = 0; i < JosnData.length; i++) {
                        ShowHtml += "<li>" + JosnData[i].content + "<span class=\"V\"> [" + JosnData[i].username + "]</span> <span class=\"X Size9\">( " + JosnData[i].addtime + " )</span></li>";
                    }
                }
                obj.html(ShowHtml);
            },
            error: function (e) { alert("连接失败,请向Eoyoo官方技术人员反馈!"); }
        })
    }

    //点击回复留言事件
    $("input[rel='reply_btn']").click(function () {
        var $object = $(this);
        if ($object.parent().find("textarea").val() != "") {
            $.ajax({
                type: "POST",
                url: "Ajax/Message.ashx",
                data: { 'com': 'addmessagereply', 'mid': $object.attr("rev"), 'replycontent': $object.parent().find("textarea").val() },
                datatype: "json",
                beforeSend: function (data, textStatus) {
                    $object.attr("disabled", true);
                    $object.val("提交中...");
                },
                success: function (data, textStatus) {
                    if (data == "1") {
                        showMessageReply($object.parent().parent().find("ul"));
                        $object.parent().find("textarea").val("");
                        $object.parent().hide();
                        $object.attr("disabled", false);
                        $object.val("提交回复");
                    } else {
                        alert("回复失败！");
                    }
                },
                error: function (e) { alert("连接失败,请向Eoyoo官方技术人员反馈!"); }
            })
        } else {
            alert("请输入回复内容！");
        }
    })

    //显示全部留言
    $("a[rel='showAllContent']").click(function () {
        $(this).prev().text($(this).attr("rev"));
        $(this).remove();
        return false;
    })

    //显示隐藏留言
    $("a[rel='hide']").click(function () {
        var $object = $(this);
        $.ajax({
            type: "POST",
            url: "Ajax/Message.ashx",
            data: { 'com': 'hidemessage', 'mid': $object.attr("rev"), 'now': $object.attr("href"), 'membername': $object.attr("title") },
            datatype: "json",
            beforeSend: function (data, textStatus) {
                $object.text("...");
            },
            success: function (data, textStatus) {
                if (data == "1") {
                    if ($object.attr("href") == "0") {
                        $object.text("[显示]");
                        $object.attr("href", "1");
                        $object.attr("class", "F underline");
                    } else {
                        $object.attr("href", "0");
                        $object.text("[取消显示]");
                        $object.attr("class", "Q underline");
                    }
                } else {
                    alert("审核失败!");
                    $object.text("[审核]");
                }
            },
            error: function (e) { alert("连接失败,请向Eoyoo官方技术人员反馈!"); }
        })

        return false;
    });

    //审核留言
    $("a[rel='audit']").click(function () {
        var $object = $(this);
        $.ajax({
            type: "POST",
            url: "Ajax/Message.ashx",
            data: { 'com': 'auditmessage', 'mid': $object.attr("rev"), 'now': $object.attr("href"), 'membername': $object.attr("title") },
            datatype: "json",
            beforeSend: function (data, textStatus) {
                $object.text("...");
            },
            success: function (data, textStatus) {
                if (data == "1") {
                    if ($object.attr("href") == "0") {
                        $object.text("[审核]");
                        $object.attr("href", "1");
                        $object.attr("class", "F underline");
                    } else {
                        $object.attr("href", "0");
                        $object.text("[取消审核]");
                        $object.attr("class", "Q underline");
                    }
                } else {
                    alert("审核失败!");
                    $object.text("[审核]");
                }
            },
            error: function (e) { alert("连接失败,请向Eoyoo官方技术人员反馈!"); }
        })

        return false;
    });

    //删除留言
    $("a[rel='del']").click(function () {
        var $delObject = $(this);
        if (confirm("确定要删除 [ " + $delObject.attr("title") + " ] 的留言吗？")) {
            $.ajax({
                type: "POST",
                url: "Ajax/Message.ashx",
                data: { 'com': 'delmessage', 'mid': $delObject.attr("href"), 'membername': $delObject.attr("title") },
                datatype: "json",
                beforeSend: function (data, textStatus) {
                    showMsg("[ " + $delObject.attr("title") + " ] 正在删除中 ", true);
                },
                success: function (data, textStatus) {
                    if (data == "1") {
                        $delObject.parent().parent("tr").remove();
                    } else {
                        alert("删除失败!");
                    }
                    showMsg("", false);
                },
                error: function (e) { alert("连接失败,请向Eoyoo官方技术人员反馈!"); }
            })
        }
        return false;
    });

});