/**
 * [OElove] (C)2010-2099 oelove.com Inc.biz
 * Email: service@phpcoo.com ,phpcoo@qq.com
 * This is NOT a freeware, use is subject to license terms
 * $ Last update 2018/06/05 by CL $
*/
$(function(){
    function StoryDetail() {
        this.page = 1;
        this.loadComment();
        this.clickLoad();
        this.commitComment();
    }
    StoryDetail.prototype = {
        //加载评论
        loadComment: function () {
            var _this = this;
            var storyid = $("#storyid").val();
            $.ajax({
                type: "GET",
                url: _ROOT_PATH + "index.php?c=story&a=comm",
                cache: false,
                data: {
                    storyid: storyid, page: _this.page, r:get_rndnum(8)
                },
                dataType: "json",
                success: function(data) {
                    var nextpage = data.nextpage; //下一页
                    var result = data.result;
                    _this.page = nextpage; //重新赋值
                    if (result.length > 0) {
                        $("#list").append(result);
                        if (nextpage > 0) { //还有下一页
                            $("#comment_btn_more").show();
                        }
                        else { //没有下一页
                            $("#comment_btn_more").hide();
                        }
                        $("#comment_btn_loading").hide();
                    }
                },
                error: function() {

                }
            });
        },
        //点击执行加载
        clickLoad: function () {
            var _this = this;
            $(document).on("click", "#comment_btn_loading", function () {
                if ($("#comment_btn_loading").is(":visible")){
                    _this.firstComment();
                    $("#comment_btn_loading").show();
                    $("#comment_btn_more").hide();
                }
            })
        },
        //发表评论
        commitComment: function () {
            $("#submit_comment_btn").on("click", function () {
                if (_LOGIN_STATUS == 0) {
                    location.href = _ROOT_PATH + "index.php?c=passport&a=login"
                }
                var storyid = $("#storyid").val();
                var _content = $("#comm_content").val();
                if (_content.length == 0) {
                    OEUI.message.msg({
                        text: "内容不能为空"
                    });
                    return false;
                }
                if (storyid.length == 0) {
                    OEUI.message.msg({
                        text: "参数错误"
                    });
                    return false;
                }
                $.ajax({
                    type: "POST",
                    url: _ROOT_PATH + "index.php?c=story",
                    cache: false,
                    data: {
                        a: "savecomm", storyid: storyid, content: _content, r: get_rndnum(8)
                    },
                    dataType: "json",
                    success: function (data) {
                        var response = data.response;
                        var result = data.result;
                        if (response == '1') {
                            $("#comm_content").val("");
                            if (result.length>0) {
                                OEUI.message.msg({text: "祝福送达成功"});
                                $("#list").prepend(result)
                                if ($("#no_data").length>0) {
                                    $("#no_data").remove();
                                }
                            } else {
                                OEUI.message.msg({text: "祝福送达成功,审核通过后将公开展示"});
                            }
                        } else {
                            OEUI.message.msg({text: "网络繁忙，请稍后再试"});
                        }
                    },
                    error: function () {
                        OEUI.message.msg({text: "网络繁忙，请稍后再试"});
                    }
                })
            });
        },
    };
    new StoryDetail();
    
});