/**
 * [OElove] (C)2010-2099 oelove.com Webapp
 * Email: service@phpcoo.com ,phpcoo@qq.com
 * This is NOT a freeware, use is subject to license terms
 * $ Last update 2020/04/23 by CL $
*/
var _inst_oecp = null;
$(function () {
   
    function OECP() {
        this.api_url = WAP_APPFILE +'&c=cp';
        this._this_dialog = null;
        this._match_dialog = null;
        this.slide_time = 200;
        
        this.obj_id = '';
        this.moneyname = '';
        this.initTip();
        this.refreshHeadImage();    
        this.clickShare();
        this.clickMatch();
        
    }
    OECP.prototype = {
        
        //每日播报
        initTip:function() {
            var _this = this;
            setTimeout(function(){
                _this.getDayTip();
            }, 500);
        },
        
        //刷新头像URL
        refreshHeadImage:function() {
            var _this = this;
            
            setTimeout(function(){
                
                var $window_url = window.location.href;
                if ($window_url != '') {
                    var $refresh = $window_url.indexOf('refresh=avatar');
                    if (typeof($refresh) == 'undefined') {
                        $refresh = '-1';
                    }
                    if ($refresh != '-1' && $refresh != '') {
                        if ($("#headimg_preview").length > 0) {
                            var $src = $("#headimg_preview").attr("src");
                            $("#headimg_preview").attr("src", $src+"?spm="+get_rndnum(6));
                        }
                    }
                }
                
            }, 200);
            
        },
        
        
        //邀请单身...
        clickShare:function() {
            var _this = this;
            
            //去推广
            $(document).on("click", "[f='but_union_share']", function() {
                var $type = $(this).attr("data-type");
                _this.sendShare($type);
            });
            
            //关闭提示 0.04.23
            $(document).on("click", "[f='close_mpreg_dialog']", function() {
                var $url = $(this).attr("data-url");
                if (typeof($url) == 'undefined') {
                    $url = '';
                }
                if ($url != '') {
                    _this._this_dialog.hide(function(){
                        setTimeout(function(){
                            window.location.href = $url;
                        }, 100);
                    });
                }
                else {
                    _this._this_dialog.hide();
                }
            });
            //0.04.23
            $(document).on("click", "[f='close_mprz_dialog']", function() {
                var $url = $(this).attr("data-url");
                if (typeof($url) == 'undefined') {
                    $url = '';
                }
                if ($url != '') {
                    _this._this_dialog.hide(function(){
                        setTimeout(function(){
                            window.location.href = $url;
                        }, 100);
                    });
                }
                else {
                    _this._this_dialog.hide();
                }
            });
        },
        sendShare:function($type) {
            var _this = this;
            
            var loading = null;
            var timer = setTimeout(function () {
                loading = OEUI.showLoading({
                    type:2
                });
            }, 200);
            
            $.ajax({
                type: "POST",
                url: WAP_APPFILE,
                cache: false,
                data: {
                    c:'my', a:'mpaction'
                },
                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") {
                        var $url = _ROOT_PATH+"index.php?m=union";
                        if ($type == 'myuser') {
                            $url += "&c=user";
                        }
                        else if ($type == 'myincome') {
                            $url += "&c=income";
                        }
                        else if ($type == 'myteam') {
                            $url += "&c=node";
                        }
                        else if ($type == 'mycp') {
                            $url += "&c=cp";
                        }
                        else {
                            $url += "&c=poster";
                        }
                        window.location.href = $url;
                    }
                    else if ($response == "2") {
                        _this.openMpDialog('mpreg');
                    }
                    else if ($response == "3") {
                        _this.openMpDialog('mprz');
                    }
                    else {
                        if ($result.length == 0 || $result == '') {
                            $result = '请检查系统是否支持推广功能';
                        }
                        OEUI.toast({
                            text: $result
                        }); 
                    }
                    
                },
                error: function() {
                    clearTimeout(timer);
                    loading && loading.hideLoading();
                    timer = null;
                    OEUI.toast({
                        text: '网络繁忙，请稍后再试！'
                    });
                }
            });    
        },
        
        openMpDialog:function($dialog) {
            var _this = this;
            _this._this_dialog = OEUI.Dialog({
                el: '#'+$dialog+'_dialog',
                animated: 'zoomIn',
                time:_this.slide_time
            }); 
        },
        clickMpDialog:function() {
            var _this = this;
            if (_this._this_dialog) {
                _this._this_dialog.hide();
            }
            
        }, 
        
        //点击速配
        clickMatch:function() {
            var _this = this;
            $(document).on("click", "[f='but_show_match']", function(){
                var loading = null;
                var timer = setTimeout(function () {
                    loading = OEUI.showLoading({
                        type:2
                    });
                }, 200);
                
                $.ajax({
                    type: "POST",
                    url: WAP_APPFILE,
                    cache: false,
                    data: {
                        c:'ajax', a:'matchuser'
                    },
                    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") {
                            $("#match_data").html($result);
                            _this._match_dialog = OEUI.Dialog({
                                el: '#match_dialog',
                                animated: 'zoomIn',
                                time:_this.slide_time
                            });
                        }
                        else {
                            if ($result.length == 0 || $result == '') {
                                $result = '获取数据失败，请检查';
                            }
                            OEUI.toast({
                                text: $result
                            }); 
                        }
                        
                    },
                    error: function() {
                        clearTimeout(timer);
                        loading && loading.hideLoading();
                        timer = null;
                        OEUI.toast({
                            text: '网络繁忙，请稍后再试！'
                        });
                    }
                });    
                
            });
            
            //关闭速配
            $(document).on("click", "[f='but_close_match']", function() {
                var $url = $(this).attr("data-url");
                if (typeof($url) == 'undefined') {
                    $url = '';
                }
                if ($url != '') {
                    _this._match_dialog.hide(function(){
                        setTimeout(function(){
                            window.location.href = $url;
                        }, 50);
                    });
                }
                else {
                    _this.closeMatchDialog();
                }
            });
        },
        closeMatchDialog:function() {
            var _this = this;
            if (_this._match_dialog) {
                _this._match_dialog.hide();
            }
        },
        
        //播报墙
        getDayTip:function() {
            var _this = this;
            $.ajax({
                type: "POST",
                url: WAP_APPFILE+'&c=ajax',
                cache: false,
                data:{
                    a:'randtip', nums:'5'
                },
                dataType: "json",
                success: function($data) {
                    var $json = eval($data);
                    var $response = $json.response;
                    var $result = $json.result;
                    if ($response == "1") {
                        var $html = "";
                        $.each($result, function($iv, $elm){
                            $html += "<li class='ws oh pr pr80'>"+$elm.title+" <span class='color_c pa rt fz12'>"+$elm.trantime+"</span></li>";
                        });
                        $("#slide_daytip_data").html($html);
                        //载入marquee js
                        var $js = "<script src='"+_ROOT_PATH +"tpl/wbapp/js/oe/marquee.js?v="+VERSION+"'></script>"; 
                        $("body").append($js);
                        setTimeout(function(){
                            new OEMarquee({el:document.querySelector('#slide_daytip_list')});
                        }, 100);
                    }
                },
                error: function() {
                    
                }
            });
        }
        
    }
    _inst_oecp = new OECP();
});
