/**
 * [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/14 by CL $
*/
$(function() {
    //详情页
    function OEDetail() {
        this._this_dialog = null;
        this.slide_time = 300;
        this.api_url = WAP_APPFILE +'&c=dynamic';
        
        this.id = ''; //ID
        this.obj_id = ''; //按钮ID
        this.zantype = ''; //点赞类型 add/cancel
        this.this_dom = '';
        
        this.wake();
    }
    
    
    OEDetail.prototype = {
        
        //唤醒
        wake:function() {
            var _this = this;
            
            //说两句
            $(document).on("click", "[f='but_open_comm']", function(){
                _this.id = $(this).attr("data-id"); //0.07.02
                if (_LOGIN_STATUS == '0') {
                    _this.login();
                    return;
                }
                _this._this_dialog = OEUI.Dialog({
                    el: '#addcomm_wrap',
                    animated: 'slideBottomIn',
                    time:_this.slide_time
                });
            });
            //评论
            $(document).on("click", "[f='but_submit_comment']", function(){
                _this.obj_id = $(this).attr("id");
                _this.id = $(this).attr("data-id");
                
                _this.comment_content = $("#comment_content").val();
                if (_this.comment_content == '') {
                    OEUI.toast({
                        type: 'bottom', 
                        text: '请填写评论内容'
                    });
                    return;
                }
                var $send_data = {
                        'a':'savecomm', 'dyid':_this.id, 'content':_this.comment_content
                };
                _this.sendComment($send_data);
            });
            
            //点赞
            $(document).on("click", "[f='but_dianzan']", function(){
                _this.id = $(this).attr("data-id"); //0.07.02
                if (_LOGIN_STATUS == '0') {
                    _this.login();
                    return;
                }
                
                if ($(this).hasClass('color_red')) {
                    _this.zantype = 'cancel';
                }
                else {
                    _this.zantype = 'add';
                }
                _this.this_dom = this;
                _this.submitZan();
            });
        },
        
        //跳到登录页
        login:function() {
            var _this = this;
            var $back_url = _this.api_url+'&a=detail&id='+_this.id+'&nofollow=1';
            window.location.href = WAP_APPFILE+'&c=passport&a=login&forward='+encodeURIComponent($back_url);
        },
        
        
        //保存评价
        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: _this.api_url,
                    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: '评论成功'
                            });
                            if (_this._this_dialog) {
                                _this._this_dialog.hide();
                            }
                            _this.sendWxNotify(_this.id, 2);
                            setTimeout(function(){
                                $("#comment_content").val("");
                                window.location.reload();
                            }, 800);
                        }
                        else if ($response == '2') {
                            OEUI.toast({
                                type: 'bottom',
                                text: '评论成功，请等待网站审核！'
                            });
                            if (_this._this_dialog) {
                                _this._this_dialog.hide();
                            }
                            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: '网络繁忙，请稍后再试！'
                        });
                    }
                });
            }
            
        },
        
        //发送通知 1-点赞，2-评论
        sendWxNotify:function($id, $type) {
            var _this = this;
            $.ajax({
                type: "POST",
                url: _this.api_url,
                cache: false,
                data: {
                    a:'sendwxtip', id:$id, tiptype:$type
                },
                dataType: "json",
                success: function($data) {
                    var $json = eval($data);
                    var $response = $json.response;
                    var $result = $json.result;
                    if ($response == "1") { 
                    
                    }
                },
                error: function() {
                    
                }
            });
            
        }, 
        
        //点赞
        submitZan:function() {
            var _this = this;
            $.ajax({
                type: "POST",
                url: _this.api_url,
                cache: false,
                data:{
                    a:'zans', id:_this.id
                },
                dataType: "json",
                success: function($data) {
                    var $json = eval($data);
                    var $response = $json.response;
                    var $result = $json.result;
                    if ($response == "1") { 
                        var $zans = parseInt($(_this.this_dom).find("em").text());
                        if (_this.zantype == 'add') {
                            $(_this.this_dom).addClass('color_red');
                            $(_this.this_dom).find("em").text($zans+1);
                            _this.sendWxNotify(_this.id, 1);
                        }
                        else {
                            $(_this.this_dom).removeClass('color_red');
                            $(_this.this_dom).find("em").text($zans-1);
                        }
                    }
                    else {
                        if ($result != '') {
                            OEUI.toast({
                                type: 'bottom',
                                text: $result
                            });
                        }
                    }
                },
                error: function() {
                }
            });
        }
        
    }
    
    new OEDetail();
});