/**
 * [OElove webapp] (C)2010-2099 oelove.com
 * Email: service@phpcoo.com ,phpcoo@qq.com
 * This is NOT a freeware, use is subject to license terms
 * $ Last update 2020/03/26 by cl $
*/
$(function () {
    
    function OERecharge () {
        this.api_url = WAP_APPFILE;
        
        this.obj_id = '';
        this.wakeRechare();
    }
    
    
    OERecharge.prototype = {
        
        wakeRechare:function() {
            var _this = this;
            
            //选择金额
            $(document).on("click", "[f='but_sel_money']", function() {
                var $val = $(this).attr("data-val");
                $("[f='but_sel_money']").find("i").removeClass("color_f40 color_9 icon-xuanze1");
                $("[f='but_sel_money']").find("i").addClass("color_9 icon-tubiao-xuanze");
                $(this).find("i").addClass("color_f40 icon-xuanze1");
                
                $("#recharge_money").val($val);
            });
            
            //提交充值
            $(document).on("click", "[f='but_submit_recharge']", function(){
                _this.obj_id = $(this).attr("id");
                
                var $money = $('#recharge_money').val();
                if ($money == '' || $money == '0') {
                    OEUI.toast({
                        type: 'bottom',  
                        text: '请输入充值金额'
                    });
                    return;
                }
                
                var $send_data = {
                    'money':$money
                };
                
                _this.submitRecharge($send_data);
            });
            
        },
        
        //充值
        submitRecharge: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);

                $.ajax({
                    type: 'POST',
                    url: _this.api_url+'&c=recharge&a=submit',
                    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') {
                            window.location.href = _this.api_url+'&c=pay&paynum='+$result;
                        }
                        else {
                            if ($result.length == 0 || $result == '') {
                                $result = '充值失败，请检查';
                            }
                            OEUI.toast({
                                type: 'bottom',
                                text: $result
                            }); 
                        }
                    },
                    error: function () {
                        $("#"+_this.obj_id).removeClass("forbid_submit");
                        clearTimeout(timer);
                        loading && loading.hideLoading();
                        timer = null;
                        OEUI.toast({
                            type: 'bottom',
                            text: '网络繁忙，请稍后再试！'
                        });
                    }
                });
                
            }
        }
    }
    new OERecharge();
});