/**
 * [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/01 by CL $
*/
$(function () {
    
    //签到
    function OELoginSign() {
        this.api_url = WAP_APPFILE +'&c=cp';
        this.sign_in = $('#sign_in'); //动画box
        this.slide_time = 300;
        this.obj_id = '';
        this.moneyname = '';
        this.rwmoney = '';//签到奖励金额
        
        this.clickLoginSign();
        this.listenIconAniamte();
        this.listenNumAniamte();
        this.closeAniamte();
    }
    
    
    OELoginSign.prototype = {
        //点击签到
        clickLoginSign:function() {
            var _this = this;
            $(document).on("click", "[f='but_loginsign']", function(){
                _this.obj_id = $(this).attr("id");
                _this.moneyname = $(this).attr("data-moneyname");
                
                if (!$(this).hasClass('current')) {
                    _this.sendLoginSign();
                }
            });
        },
        //发送签到
        sendLoginSign:function() {
            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: {
                        a:'loginsign'
                    },
                    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") {
                            $result = parseFloat($result);
                            if ($result > 0) {
                                _this.rwmoney = $result; //奖励金豆
                                
                                if ($("#total_money").length > 0) {
                                    var $total_money = $("#total_money").html();
                                    $total_money = parseFloat($total_money);
                                    var $tips_total = ($total_money+$result);
                                    $tips_total = $tips_total.toFixed(2);
                                    $("#total_money").html($tips_total);
                                }
                                
                                var $mp3_html = "<audio id='audio' src='"+_ROOT_PATH+"tpl/wbapp/mp3/sign_in.mp3?v="+VERSION+"' style='display:none;'></audio>";
                                $("body").append($mp3_html);
                                setTimeout(function(){
                                    _this.showAniamte();
                                }, 100);
                            }
                            else {
                                OEUI.toast({
                                    text: '恭喜你，签到成功'
                                });
                            }
                            $("[f='but_loginsign']").removeClass("bg_red color_f").addClass("bg_de color_9 current");
                            $("[f='but_loginsign']").html("今日已签到");
                        }
                        else {
                            if ($result.length == 0 || $result == '') {
                                $result = '签到失败，请检查是否重复签到';
                            }
                            OEUI.toast({
                                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({
                            text: '网络繁忙，请稍后再试！'
                        });
                    }
                });
            }
        },
        
        //显示动画
        showAniamte: function () {
            var _this = this;
            _this.sign_in.show();
            _this.sign_in.find('.mask').addClass('animated fadeIn');
            _this.sign_in.find('.icon').addClass('bounce');
            setTimeout(function () {
                document.getElementById('audio').play();
            }, 500);
        },
        //动画
        listenIconAniamte: function () {
            var _this = this;
            _this.sign_in.find('.icon').on('animationend', function () {
                if (_this.rwmoney != '') {
                    _this.sign_in.find('.num').html('增加'+_this.rwmoney+_this.moneyname).addClass('slideInUp');
                }
            });
        },
        //动画
        listenNumAniamte: function () {
            var _this = this;
            _this.sign_in.find('.num').on('animationend', function () {
                setTimeout(function(){
                    _this.sign_in.remove();
                }, 600);
                
            });
        },
        //关闭
        closeAniamte:function () {
            var _this = this;
            $('[f="close_sign_in"]').on('click', function () {
                _this.sign_in.remove();
            });
        }
    }
    
    new OELoginSign();
    
});
