/**
 * [OElove] (C)2010-2099 oelove.com biz
 * Email: service@phpcoo.com ,phpcoo@qq.com
 * This is NOT a freeware, use is subject to license terms
 * $ Last update 2020/03/11 by CL $
*/
var _inst_oedetail = null;
var _inst_oecomm = null;
$(function () {
    
    //记录来源缓存
    var $frompre = $("#frompre").val();
    if (typeof($frompre) == 'undefined') {
        $frompre = '';
    }
    oeui_cache.jumpSetCacheRouter($frompre);
    
    //初始化
    function OEComment() {
        this.data_id = "comment_data";
        this.page = 1;
        this._this_dialog = null;
        this.slide_time = 300;
        this.page = 1; //默认第一页
        
        this.obj_id = '';
        this.id = '';
        this.comment_content = ''; 
        
        this.wake();
    }
    
    OEComment.prototype = {
        
        wake:function() {
            var _this = this;
            _this.getList(1); //默认加载第一页
            
            //加载更多评论
            $(document).on("click", "[f='but_more_comment']", function(){
                $("#more_comment_wrap").find("img").show();
                _this.getList();
            });
            
            
            //评论按钮
            $(document).on("click", "[f='but_submit_comment']", function(){
                _this.obj_id = $(this).attr("id");
                _this.id = $(this).attr("data-id");
                
                //需登录
                if (_LOGIN_STATUS == '0') {
                    var $back_url = WAP_APPFILE+'&c=story&a=detail&id='+_this.id+'&nofollow=1';
                    window.location.href = WAP_APPFILE+'&c=passport&a=login&forward='+encodeURIComponent($back_url);
                    return;
                }
                
                _this.comment_content = $("#comment_content").val();
                if (_this.comment_content == '') {
                    OEUI.toast({
                        type: 'bottom', 
                        text: '请填写发表内容'
                    });
                    return;
                }
                var $send_data = {
                    'a':'savecomm', 'storyid':_this.id, 'content':_this.comment_content
                };
                _this.sendComment($send_data);
                
            });
        },
        
        //获取评论
        getList:function($page) {
            var _this = this;
            
            if (typeof($page) == 'undefined') {
                $page = '';
            }
            if ($page == '') {
                $page = $("#page").val();
            }
            var $reqs_url = $("#reqs_url").val();
            
            var $id = $("#id").val();
            $reqs_url += "&storyid="+$id;
            
            $.ajax({
                type: "POST",
                url: $reqs_url,
                cache: false,
                data: {
                    page:$page
                },
                dataType: "json",
                success: function($data) {
                    var $json = eval($data);
                    var $nextpage = $json.nextpage; //下一页
                    var $result = $json.result;
                    var $total = $json.total;
                    $("#page").val($nextpage); //重新赋值
                    if ($result.length > 0) {
                        $("#comment_nums").html("("+$total+")");
                        //载入数据
                        if ($page == 1) {
                            $("#"+_this.data_id).html($result);
                        }
                        else {
                            $("#"+_this.data_id).append($result);
                        }

                        if ($nextpage > 0) { //还有下一页
                            $("#more_comment_wrap").show();
                            $("#more_comment_wrap").find("img").hide();
                        }
                        else { //没有下一页
                            $("#more_comment_wrap").hide();
                            $("#more_comment_wrap").find("img").hide();
                        }
                        
                    }
                    else {
                        $("#"+_this.data_id).html($result);
                        $("#comment_nums").html("0");
                    }
                },
                error: function() {
                
                }
            });
        
        }, 
        
        //发表评论
        sendComment:function($send_data) {
            var _this = this;
            
            if (!$("#"+_this.obj_id).hasClass("forbid_submit")) {
                var loading = null;
                var timer = setTimeout(function () {
                    loading = OEUI.showLoading({
                        type:2
                    });
                }, 200);
                $("#"+_this.obj_id).addClass("forbid_submit");
                
                $.ajax({
                    type: "POST",
                    url: WAP_APPFILE + '&c=story',
                    cache: false,
                    data: $send_data,
                    dataType: "json",
                    success: function($data) {
                        var $json = eval($data);
                        var $response = $json.response;
                        var $result = $json.result;
                        
                        clearTimeout(timer);
                        loading && loading.hideLoading();
                        timer = null;
                        
                        if ($response == "1") {
                            OEUI.toast({
                                type: 'bottom',
                                text: '发表成功'
                            });
                            setTimeout(function(){
                                window.location.reload();
                            }, 800);
                        }
                        else if ($response == '2') {
                            OEUI.toast({
                                type: 'bottom',
                                text: '发表成功，请等待网站审核！'
                            });
                            setTimeout(function(){
                                $("#comment_content").val("");
                            }, 800);
                        }
                        else {
                            if ($result.length == 0 || $result == '') {
                                $result = '发表失败，请检查！';
                            }
                            OEUI.toast({
                                type: 'bottom',
                                text: $result
                            }); 
                        }
                        $("#"+_this.obj_id).removeClass("forbid_submit");
                    },
                    error: function() {
                        $("#"+_this.obj_id).removeClass("forbid_submit");
                        clearTimeout(timer);
                        loading && loading.hideLoading();
                        timer = null;
                        OEUI.toast({
                            type: 'bottom',
                            text: '网络繁忙，请稍后再试！'
                        });
                    }
                });
                
            } 
        }
    }
    _inst_oecomm = new OEComment();
    
});