/**
 * [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/05/20 by CL $
*/
var _inst_oehome = null;
var _inst_lamp = null;
var _inst_card = null;
$(function(){
    
    function OEHome() {
        this._sharecard_dialog = null;
        this.slide_time = 200;
        this.api_url = WAP_APPFILE;
        this.home_uid = ''; //当前会员UID
        this.home_url = ''; //当前会员URL
        this.obj_dom = ''; //当前对象
        this.obj_id = ''; //按钮ID
        
        this.init();
        this.clickLike(); //喜欢
        this.clickBlack(); //拉黑
        this.clickFriend(); //加好友
        this.clickKefu(); //客服
        this.clickRpzy(); //RPZY查询
        this.clickChat(); //聊天
        this.clickWeixin(); //查看微信
        this.clickCopy(); //复制
        this.clickNav(); //切换导航
        this.clickSendRzTips(); //提醒实名认证
        
    }
    
    OEHome.prototype = {
        
        //初始化
        init:function() {
            var _this = this;
            _this.home_uid = $("#homeuid").val();
            _this.home_url = _this.api_url+"&c=home&a=detail&id="+_this.home_uid+"&nofollow=1";
            
            //点击登录
            $(document).on("click", "[f='but_login']", function(){
                _this.checkLogin();
            });
            
            //初始化数据
            setTimeout(function(){
                _this.getCount();
                _this.likeUser();
            }, 200);
            
        }, 
        
        //检测是否登录
        checkLogin:function() {
            var _this = this;
            if (_LOGIN_STATUS == '0') {
                window.location.href = _this.api_url+"&c=passport&a=login&forward="+encodeURIComponent(_this.home_url);
                return false;
            }
        },
        //获取统计数量
        getCount:function() {
            var _this = this;
            $.ajax({
                type: "POST",
                url: _this.api_url,
                cache: false,
                data:{
                    c:'home', a:'getcount', uid:_this.home_uid
                },
                dataType: "json",
                success: function($data) {
                    var $json = eval($data);
                    var $response = $json.response;
                    var $result = $json.result;
                    if ($response == "1") {
                        var $dynamic_nums = $result.dynamic_nums;
                        var $photo_nums = $result.photo_nums;
                        
                        //0.05.19
                        var $login_hasphoto = $result.login_hasphoto;
                        if (typeof($login_hasphoto) == 'undefined') {
                            $login_hasphoto = '0';
                        }
                        
                        $("#photo_nums").html($photo_nums);
                        //0.05.19
                        if ($("#photo_nums2").length > 0) {
                            if ($photo_nums > 4) {
                                $("#photo_nums2").html("+"+$photo_nums).show();
                            }
                        }
                        if ($("#dynamic_nums").length > 0) {
                            $("#dynamic_nums").html("("+$dynamic_nums+")");
                        }
                        //图库处理 0.05.19
                        if ($photo_nums > 0) {
                            //$("#headimg").removeAttr('preview');
                            //$("#my_photos").show();
                            
                            //0.05.19
                            if ($("#gallery_list").length > 0) {
                                if ($login_hasphoto != '1') {
                                    $("#gallery_list").find("img").addClass('blur');
                                }
                            }
                            //载入js
                            var $photo_js = "<script src='"+_ROOT_PATH +"tpl/static/oeui/js/oeui.photoswipe.min.js?v="+VERSION+"'></script>"+
                            "<script src='"+_ROOT_PATH +"tpl/wbapp/js/home.photo.js?v="+VERSION+"'></script>"; 
                            $("body").append($photo_js);
                        }
                        else {
                            //载入js
                            var $photo_js = "<script src='"+_ROOT_PATH +"tpl/static/oeui/js/oeui.photoswipe.min.js?v="+VERSION+"'></script>"; 
                            $("body").append($photo_js);
                        }
                        
                    }
                    
                },
                error: function() {

                }
            });
        },
        
        
        
        //喜欢&取消喜欢
        clickLike:function() {
            var _this = this;
            $(document).on("click", "[f='but_like']", function(){
                _this.obj_dom = $(this);
                
                //0.04.28
                if (_this.checkLogin() == false) {
                    return;
                }
                var $type = '';
                if ($(this).hasClass('current')) {
                    $type  = 'cancel';
                }
                else {
                    $type = 'add';
                }
                _this.doLike($type);
            });
        },
        doLike:function($type) {
            var _this = this;
            
            if ($type == 'add') {
                var $url = _this.api_url+"&c=home&a=addlisten";
            }
            else {
                var $url = _this.api_url+"&c=home&a=dellisten";
            }
            $.ajax({
                type: "POST",
                url: $url,
                cache: false,
                data:{
                    id:_this.home_uid
                },
                dataType: "json",
                success: function($data) {
                    var $json = eval($data);
                    var $response = $json.response;
                    var $result = $json.result;
                    
                    if ($response == "1") {
                        if ($type == 'add') {
                            $(_this.obj_dom).addClass('current');
                            $(_this.obj_dom).find('i').addClass('heartAnimation');
                            $(_this.obj_dom).find('span').html('已喜欢');
                        }
                        else {
                            $(_this.obj_dom).removeClass('current');
                            $(_this.obj_dom).find('i').removeClass('heartAnimation current');
                            $(_this.obj_dom).find('span').html('喜欢');
                        }
                    }
                    else if ($response == '2') {
                        var $ret_code = $result.ret_code;
                        if (typeof($ret_code) == 'undefined') {
                            $ret_code = '';
                        }
                        var $ret_msg = $result.ret_msg;
                        if (typeof($ret_msg) == 'undefined') {
                            $ret_msg = '';
                        }
                        if ($ret_code != '') {
                            _this.guideDialog($ret_code);
                        }
                    }
                    else {
                        if ($result != '') {
                            OEUI.toast({
                                type: 'bottom',
                                text: $result
                            });
                        }
                    }
                },
                error: function() {
                    OEUI.toast({
                        type: 'bottom',
                        text: '网络繁忙，请稍后再试！'
                    });
                }
            });
            
        },
        
        //屏蔽会员
        clickBlack:function() {
            var _this = this;
            $(document).on("click", "[f='but_black']", function(){
                _this.obj_dom = $(this);
                //0.04.28
                if (_this.checkLogin() == false) {
                    return;
                }
                
                var $type = 'add';
                if ($(this).hasClass('current')) {
                    $type = 'cancel';
                }
                _this.doBlack($type);
            });
        },
        doBlack:function($type) {
            var _this = this;

            
            $.ajax({
                type: "POST",
                url: _this.api_url+"&c=home",
                cache: false,
                data:{
                    a:'doblack', uid:_this.home_uid, type:$type
                },
                dataType: "json",
                success: function($data) {
                    var $json = eval($data);
                    var $response = $json.response;
                    var $result = $json.result;
                    
                    if ($response == "1") {
                        if ($type == 'add') {
                            OEUI.toast({
                                type: 'bottom',
                                text: '已屏蔽拉黑'
                            });
                            $(_this.obj_dom).find("em").html("已屏蔽拉黑");
                        }
                        else {
                            OEUI.toast({
                                type: 'bottom',
                                text: '已取消屏蔽'
                            });
                            $(_this.obj_dom).find("em").html("屏蔽拉黑");
                        }
                    }
                    else {
                        if ($result != '') {
                            OEUI.toast({
                                type: 'bottom',
                                text: $result
                            });
                        }
                    }
                },
                error: function() {
                    OEUI.toast({
                        type: 'bottom',
                        text: '网络繁忙，请稍后再试！'
                    });
                }
            });
        },
        
        //加好友
        clickFriend:function() {
            var _this = this;
            
            $(document).on("click", "[f='but_friend']", function(){
                _this.obj_dom = $(this);
                
                //0.04.28
                if (_this.checkLogin() == false) {
                    return;
                }
                if ($(this).hasClass('current')) {
                    OEUI.toast({
                        type: 'bottom',
                        text: '你已发送申请，请等待对方同意'
                    });
                    return;
                }
                _this.doFriend();
            });
        },
        doFriend:function() {
            var _this = this;
            
            $.ajax({
                type: "POST",
                url: _this.api_url+"&c=home",
                cache: false,
                data:{
                    a:'apply', id:_this.home_uid
                },
                dataType: "json",
                success: function($data) {
                    var $json = eval($data);
                    var $response = $json.response;
                    var $result = $json.result;
                    
                    if ($response == "1") {
                        OEUI.toast({
                            type: 'bottom',
                            text: '好友申请已发送，请等待对方同意'
                        });
                        $(_this.obj_dom).addClass('current');
                        $(_this.obj_dom).find("dd").html("已发申请");
                    }
                    else if ($response == '2') {
                        var $ret_code = $result.ret_code;
                        if (typeof($ret_code) == 'undefined') {
                            $ret_code = '';
                        }
                        var $ret_msg = $result.ret_msg;
                        if (typeof($ret_msg) == 'undefined') {
                            $ret_msg = '';
                        }
                        if ($ret_code != '') {
                            _this.guideDialog($ret_code);
                        }
                    }
                    else {
                        if ($result != '') {
                            OEUI.toast({
                                type: 'bottom',
                                text: $result
                            });
                        }
                    }
                },
                error: function() {
                    OEUI.toast({
                        type: 'bottom',
                        text: '网络繁忙，请稍后再试！'
                    });
                }
            });
        },
        
        //联系客服
        clickKefu:function() {
            var _this = this;
            $(document).on("click", "[f='but_view_kefu']", function(){
                _this.getKefu();
            });
        },
        getKefu:function() {
            var _this = this;
            
            /*
            var loading = null;
            var timer = setTimeout(function () {
                loading = OEUI.showLoading({
                    type:2
                });
            }, 200);
            */
            $.ajax({
                type: "POST",
                url: _this.api_url+"&c=ajax",
                cache: false,
                data:{
                    a:'kefu'
                },
                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") {
                        $("#kefu_data").html($result);
                        _inst_oehome_dialog.openDialog('kefu', 'zoom'); //弹窗
                    }
                    else {
                        OEUI.toast({
                            type: 'bottom',
                            text: '网络繁忙，请稍后再试！'
                        });
                    }
                },
                error: function() {
                    /*
                    clearTimeout(timer);
                    loading && loading.hideLoading();
                    timer = null;
                    */
                    OEUI.toast({
                        type: 'bottom',
                        text: '网络繁忙，请稍后再试！'
                    });
                }
            });
        },
        
        //诚信查询
        clickRpzy:function() {
            var _this = this;
            $(document).on("click", "[f='but_view_rpzy']", function(){
                //0.04.28
                if (_this.checkLogin() == false) {
                    return;
                }
                _this.viewRpzy();
            });
        },
        viewRpzy:function() {
            var _this = this;
            
            var loading = null;
            var timer = setTimeout(function () {
                loading = OEUI.showLoading({
                    type:2
                });
            }, 200);
            $.ajax({
                type: "POST",
                url: _this.api_url+"&c=home",
                cache: false,
                data:{
                    a:'viewcredit', id:_this.home_uid
                },
                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') {
                        $("#rpzy_data").html($result);
                        _inst_oehome_dialog.openDialog('rpzy', 'right'); //弹窗
                    }
                    else if ($response == '2') {
                        var $ret_code = $result.ret_code;
                        if (typeof($ret_code) == 'undefined') {
                            $ret_code = '';
                        }
                        var $ret_msg = $result.ret_msg;
                        if (typeof($ret_msg) == 'undefined') {
                            $ret_msg = '';
                        }
                        
                        if ($ret_code != '') {
                            if ($ret_code == 'nodata') {
                                OEUI.toast({
                                    type: 'bottom',
                                    text: '系统繁忙，请稍后再试！'
                                });
                            }
                            else {
                                _this.guideDialog($ret_code);
                            }
                        }
                    }
                    else {
                        OEUI.toast({
                            type: 'bottom',
                            text: '网络繁忙，请稍后再试！'
                        });
                    }
                },
                error: function() {
                    clearTimeout(timer);
                    loading && loading.hideLoading();
                    timer = null;
                    OEUI.toast({
                        type: 'bottom',
                        text: '网络繁忙，请稍后再试！'
                    });
                }
            });
        },
        
        //聊天
        clickChat:function() {
            var _this = this;
            
            $(document).on("click", "[f='but_chat']", function(){
                _this.obj_dom = $(this);
                
                //0.04.28
                if (_this.checkLogin() == false) {
                    return;
                }
                _this.doChat();
            });
        },
        doChat:function() {
            var _this = this;
            
            $.ajax({
                type: "POST",
                url: _this.api_url+"&c=msgchat",
                cache: false,
                data:{
                    a:'setsid', touid:_this.home_uid
                },
                dataType: "json",
                success: function($data) {
                    var $json = eval($data);
                    var $response = $json.response;
                    var $result = $json.result;
                    
                    if ($response == "1") {
                        window.location.href = _this.api_url+"&c=msgchat&sid="+$result;
                    }
                    else if ($response == '2') {
                        var $ret_code = $result.ret_code;
                        if (typeof($ret_code) == 'undefined') {
                            $ret_code = '';
                        }
                        var $ret_msg = $result.ret_msg;
                        if (typeof($ret_msg) == 'undefined') {
                            $ret_msg = '';
                        }
                        if ($ret_code != '') {
                            _this.guideDialog($ret_code);
                        }
                    }
                    else {
                        if ($result != '') {
                            OEUI.toast({
                                type: 'bottom',
                                text: $result
                            });
                        }
                    }
                },
                error: function() {
                    OEUI.toast({
                        type: 'bottom',
                        text: '网络繁忙，请稍后再试！'
                    });
                }
            });
        },
        
        //查看微信
        clickWeixin:function() {
            var _this = this;
            
            //触发
            $(document).on("click", "[f='but_view_weixin']", function(){
                _this.obj_dom = $(this);
                //0.04.28
                if (_this.checkLogin() == false) {
                    return;
                }
                _this.viewWeixin();
            });
            
            //选择付款方式
            $(document).on("click", "[f='select_weixin_paytype']", function(){
                var $type = $(this).attr("data-type");
                $("[f='select_weixin_paytype']").find('.paytype i').removeClass('color_f40 icon-xuanze1').addClass('color_9 icon-tubiao-xuanze');
                $(this).find('.paytype i').addClass('color_f40 icon-xuanze1');
                $("[f='but_pay_weixin']").attr("data-type", $type);
                
                if ($type == 'usevip') {
                    $("#but_pay_weixin").show();
                    $("#but_pay_weixin2").hide();
                }
                else {
                    $("#but_pay_weixin").hide();
                    $("#but_pay_weixin2").show();
                }
            });
            
            //付费查看
            $(document).on("click", "[f='but_pay_weixin']", function(){
                var $type = $(this).attr("data-type");
                var $uid = $(this).attr("data-uid");
                if (typeof($uid) == 'undefined') {
                    $uid = '';
                }
                if (_inst_oehome_dialog) {
                    _inst_oehome_dialog.closeDialog();
                }  
                _this.payWeixin($type);
            });
            
            
        },
        viewWeixin:function() {
            var _this = this;
            
            var loading = null;
            var timer = setTimeout(function () {
                loading = OEUI.showLoading({
                    type:2
                });
            }, 200);
            
            $.ajax({
                type: "POST",
                url: _this.api_url+"&c=view",
                cache: false,
                data:{
                    id:_this.home_uid
                },
                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") {
                        _this.getWeixin($result);
                    }
                    else if ($response == '2') {
                        var $ret_code = $result.ret_code;
                        if (typeof($ret_code) == 'undefined') {
                            $ret_code = '';
                        }
                        var $ret_msg = $result.ret_msg;
                        if (typeof($ret_msg) == 'undefined') {
                            $ret_msg = '';
                        }
                        if ($ret_code != '') {
                            if ($ret_code == 'hide_weixin') {
                                _this.getKefu();
                            }
                            else {
                                _this.guideDialog($ret_code);
                            }
                        }
                    }
                    else if ($response == '3') {
                        //弹出付费
                        $("#pay_data").html($result);
                        _inst_oehome_dialog.openDialog('pay', 'bottom'); //弹窗
                    }
                    //免费查看 0.05.06
                    else if ($response == '10') {
                        _this.payWeixin('balance');
                    }
                    else {
                        if ($result != '') {
                            OEUI.toast({
                                type: 'bottom',
                                text: $result
                            });
                        }
                    }
                },
                error: function() {
                    clearTimeout(timer);
                    loading && loading.hideLoading();
                    timer = null;
                    
                    OEUI.toast({
                        type: 'bottom',
                        text: '网络繁忙，请稍后再试！'
                    });
                }
            });
        },
        getWeixin:function($code) {
            var _this = this;
            
            $.ajax({
                type: "POST",
                url: _this.api_url+"&c=view",
                cache: false,
                data:{
                    a:'contact', id:_this.home_uid, code:$code
                },
                dataType: "json",
                success: function($data) {
                    var $json = eval($data);
                    var $response = $json.response;
                    var $result = $json.result;
                    
                    if ($response == "1") {
                        $("#contact_data").html($result);
                        _inst_oehome_dialog.openDialog('contact', 'right'); //弹窗
                    }
                    else {
                        if ($result != '') {
                            OEUI.toast({
                                type: 'bottom',
                                text: $result
                            });
                        }
                    }
                },
                error: function() {
                    
                    OEUI.toast({
                        type: 'bottom',
                        text: '网络繁忙，请稍后再试！'
                    });
                }
            });
            
        },
        //查看微信付费
        payWeixin:function($pay_type) {
            var _this = this;
            
            var loading = null;
            var timer = setTimeout(function () {
                loading = OEUI.showLoading({
                    type:2
                });
            }, 200);
            
            var $a = 'onlinepay';
            if ($pay_type == 'usevip') {
                $a = 'usevip';
            }
            else if ($pay_type == 'balance') {
                $a = 'balancepay';
            }
            
            $.ajax({
                type: "POST",
                url: _this.api_url+"&c=view",
                cache: false,
                data:{
                    a:$a, id:_this.home_uid
                },
                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") {
                        _this.getWeixin($result);
                    }
                    else if ($response == '2') {
                        //在线支付
                        window.location.href = WAP_APPFILE+'&c=pay&paynum='+$result;
                    }
                    else if ($response == '22') {
                        //唤醒微信支付
                        var $back_url = WAP_APPFILE+'&c=home&a=detail&id='+_this.home_uid+'&nofollow=1';
                        var $pay_data = {
                            'paynum': $result,
                            'forward': $back_url
                        };
                        _inst_oewx_pay.initPay($pay_data, function(){
                            _this.viewWeixin(); //成功后查看
                        });
                        
                    }
                    else {
                        if ($result != '') {
                            OEUI.toast({
                                type: 'bottom',
                                text: $result
                            });
                        }
                    }
                },
                error: function() {
                    clearTimeout(timer);
                    loading && loading.hideLoading();
                    timer = null;
                    
                    OEUI.toast({
                        type: 'bottom',
                        text: '网络繁忙，请稍后再试！'
                    });
                }
            });
            
        },
        
        //复制
        clickCopy:function() {
            var _this = this;
            
            $(document).on("click", "[f='but_copy']", function(){
                var copy_id = $(this).attr("data-id");
                _this.copyData(copy_id);
            });
        },
        copyData:function(el) {
            var _this = this;
            
            var range = document.createRange();
            range.selectNode(document.getElementById(el));
            var selection = window.getSelection();
            if (selection.rangeCount > 0) selection.removeAllRanges();
            selection.addRange(range);
            document.execCommand('copy');
            OEUI.toast({
                type: 'bottom',
                text: '复制成功'
            });
        },

        
        //引导弹窗信息
        guideDialog:function($guide_code) {
            var _this = this;
            $.ajax({
                type: "POST",
                url: _this.api_url+'&c=home&a=guide',
                cache: false,
                data:{
                    a:'guide', 'uid':_this.home_uid, 'retcode':$guide_code
                },
                dataType: "json",
                success: function($data) {
                    var $json = eval($data);
                    var $response = $json.response;
                    var $result = $json.result;
                    
                    if ($response == "1") {
                        $("#guide_data").html($result);
                        _inst_oehome_dialog.openDialog('guide', 'zoom'); //弹窗
                    }
                    else {
                        OEUI.toast({
                            type: 'bottom',
                            text: '网络繁忙，请稍后再试！'
                        });
                    }
                },
                error: function() {
                    OEUI.toast({
                        type: 'bottom',
                        text: '网络繁忙，请稍后再试！'
                    });
                }
            });
            
        },
        
        //切换导航
        clickNav:function() {
            var _this = this;
            $(document).on("click", "[f='but_select_nav']", function(){
                var $box = $(this).attr("data-box");
                $(this).addClass('current').removeClass('color_9').siblings().removeClass('current').addClass('color_9');
                $(".nav_list").hide();
                $("#"+$box+"_box").show();
                
                if ($box == 'dynamic' || $box == 'video') {
                    _this.getNavData($box);
                }
            });
        },
        //获取动态、视频数据
        getNavData:function($type) {
            var _this = this;
            var $frompre = $("#frompre").val();
            if (typeof($frompre) == 'undefined') {
                $frompre = '';
            }
            $.ajax({
                type: "POST",
                url: _this.api_url+'&c=home',
                cache: false,
                data:{
                    a:'get'+$type, 'uid':_this.home_uid, frompre:$frompre
                },
                dataType: "json",
                success: function($data) {
                    var $json = eval($data);
                    var $response = $json.response;
                    var $result = $json.result;
                    
                    if ($response == "1") {
                        $("#"+$type+"_box").html($result);
                        preload_img();
                    }
                },
                error: function() {
                    
                }
            });
        }, 
        
        //猜你喜欢
        likeUser:function() {
            var _this = this;
            var $frompre = $("#frompre").val();
            if (typeof($frompre) == 'undefined') {
                $frompre = '';
            }
            $.ajax({
                type: "POST",
                url: _this.api_url+'&c=ajax',
                cache: false,
                data:{
                    a:'likeuser', frompre:$frompre
                },
                dataType: "json",
                success: function($data) {
                    var $json = eval($data);
                    var $response = $json.response;
                    var $result = $json.result;
                    if ($response == "1") {
                        $("#likeuser_data").html($result);
                        $("#likeuser_box").show();
                        preload_img();
                    }
                },
                error: function() {
                    
                }
            });
        },
        

        showCardList: function () {
            this._sharecard_dialog = OEUI.Dialog({
                el: '#sharecard_content',
                animated: 'slideRightIn'
            });
        },
        
        //发送认证提醒
        clickSendRzTips:function() {
            var _this = this;
            
            $(document).on("click", "[f='but_send_rztips']", function(){
                //0.04.28
                if (_this.checkLogin() == false) {
                    return;
                }
                
                var loading = null;
                var timer = setTimeout(function () {
                    loading = OEUI.showLoading({
                        type:2
                    });
                }, 200);
                
                $.ajax({
                    type: 'POST',
                    url: _this.api_url,
                    data: {
                        c:'home', a:'sendtip', type:'rz', uid:_this.home_uid
                    },
                    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: '已发送提醒'
                            });
                        }
                        else {
                            OEUI.toast({
                                type: 'bottom',
                                text: '网络繁忙，请稍后再试'
                            });
                        }
                    },
                    error: function () {
                        clearTimeout(timer);
                        loading && loading.hideLoading();
                        timer = null;
                        OEUI.toast({
                            type: 'bottom',
                            text: '网络繁忙，请稍后再试'
                        });
                    }
                });
            });
            
        }
        
    }
    
    _inst_oehome = new OEHome();
});
