/**
 * [OElove] (C)2010-2099 oelove.com Inc. biz
 * Email: service@phpcoo.com ,phpcoo@qq.com
 * This is NOT a freeware, use is subject to license terms
 * $ Last update 2020/11/10 by BO $
*/
$(function () {
    function VIP () {
        this.is_http = false;
        this.buy();
        this.renew();
        this.openTJ();
    }
    VIP.prototype = {
        buy: function () {
            var _this = this;
            $('[f="buy_set_menu"]').on('click', function () {
                var id = $(this).attr('data-id');
                _this.httpBuy(id, this);
            });
        },
        httpBuy: function (id, el) {
            var fromurl = $("#fromurl").val() || '';
            var _this = this;

            if (_this.is_http) return;
                _this.is_http = true;

            var loading,
                timer = setTimeout(function () {
                    loading = OEUI.loading({
                        el: el
                    });
                    loading.show();
                }, 200);

            $.ajax({
                type: "POST",
                url: _ROOT_PATH+"index.php?m=user&c=vip",
                data: {
                    a:"confirmbuy", tcid: id, fromurl:fromurl
                },
                success: function(json) {
                    is_http = false;
                    loading && loading.remove();
                    clearTimeout(timer);
                    timer = null;
                    var res = JSON.parse(json);
                    if (res.response == "1") {
                        //余额支付...
                        if (fromurl.length == 0) {
                            fromurl = _ROOT_PATH+"index.php?m=user&c=vip&nofollow=1";
                        }
                        OEUI.message.msg({text: "开通成功"});
                        setTimeout(function(){
                            window.location.href = fromurl;
                        }, 800);
                        
                    } else if (res.response == "2") {
                        //在线支付...
                        if (fromurl.length == 0) {
                            fromurl = _ROOT_PATH+"index.php?m=user&c=vip&nofollow=1";
                        }
                        window.top.location.href = _ROOT_PATH+"index.php?c=pay&paynum="+res.result;
                    } else {
                        if (res.result == '') {
                            res.result = "开通失败，请检查！";
                        }
                        OEUI.message.msg({text: res.result});
                    }
                },
                error: function() {
                    is_http = false;
                    loading && loading.remove();
                    clearTimeout(timer);
                    timer = null;
                    OEUI.message.msg({text: "网络繁忙，请稍后再试"});
                }
            });
        },
        //续费
        renew: function () {
            var _this = this;
            $('[f="renew"]').on('click', function () {
                var id = $(this).attr('data-id');
                _this.httpBuy(id, this);
            });
        },
        //开通推荐套餐
        openTJ: function () {
            var _this = this;
            $('[f="open_tj"]').on('click', function () {
                var id = $('#tjid').val() || '';
                if (id == '') {
                    id = $('[f="buy_set_menu"]').eq(0).attr('data-id');
                }

                _this.httpBuy(id, this);
            });
        }
    }
    new VIP();
});






/*
$(function(){
    
    //弹出购页...8.08.16
    $(document).on("click", "[f='but_vip_buy']", function(){
        $cur_grid = $(this).attr("data-curgrid"); //当前会员组别
        if (typeof($cur_grid) == "undefined") {
            $cur_grid = 1;
        }
        $cur_grid = parseInt($cur_grid);
        $_grid = $(this).attr("data-grid"); //需要购买的组别
        $_grid = parseInt($_grid);
        $_grname = $(this).attr("data-grname");
        if (typeof($_grname) == "undefined") {
            $_grname = "";
        }
        if ($_grid < 2) {
            layer.msg("升级出错，请重新选择");
            return false;
        }
        if ($_grid < $cur_grid) {
            layer.msg("对不起，不能购买比当前等级还低的会员级别");
            return false;
        }
        
        $fromurl = $("#fromurl").val();
        if (typeof($fromurl) == "undefined") {
            $fromurl = "";
        }
        
        $width = 950;
        $height = 500;
        layer.open({
            title:"【升级会员】 "+$_grname,
            type: 2,
            area: [''+$width+'px', ''+$height+'px'],
            shadeClose: true,
            resize: false,
            scrollbar: false,
            content: _OE_FILE+"&c=vip&a=power&id="+$_grid+"&fromurl="+encodeURIComponent($fromurl)
        });
    });
    
    
    //立即购买
    $(document).on("click", "[f='but_confirm_buy']", function(){
        $obj_id = $(this).attr("id");
        $grid = $(this).attr("data-grid");
        $grname = $(this).attr("data-grname");
        $feeid = $(this).attr("data-feeid");
        $price = $(this).attr("data-price");
        $days = $(this).attr("data-days");
        
        if ($grid.length == 0) {
            layer.msg("请选择会员等级");
            return false;
        }
        if ($feeid.length == 0) {
            layer.msg("请选择购买套餐");
            return false;
        }
        $_fromurl = $("#fromurl").val();
        if (typeof($_fromurl) == "undefined") {
            $_fromurl = "";
        }

        if (!$("#"+$obj_id).hasClass("forbid_submit")) {
            $("#"+$obj_id).addClass("forbid_submit");
            $("#"+$obj_id).find("span").show();
            
            $.ajax({
                type: "POST",
                url: _OE_FILE + "&c=vip",
                cache: false,
                data: {
                    a:"confirmpay", groupid:$grid, feeid:$feeid, fromurl:$_fromurl, r:get_rndnum(8)
                },
                dataType: "json",
                success: function($data) {
                    $json = eval($data);
                    $response = $json.response;
                    $result = $json.result;
                    if ($response == "1") {
                        //购买成功
                        layer.msg("购买成功");
                        if ($_fromurl.length > 0) {
                            setTimeout(function(){
                                window.parent.location.href = $_fromurl;
                            }, 500);
                        }
                        else {
                            setTimeout(function(){
                                window.parent.location.reload();
                            });
                        }
                    }
                    else if ($response == "2") {
                        //在线支付
                        window.top.location.href = _ROOT_PATH+"index.php?c=pay&paynum="+$result;
                    }
                    else {
                        if ($result.length > 0) {
                            layer.msg($result);
                        }
                        else {
                            layer.msg("购买失败");
                        }
                    }
                    $("#"+$obj_id).removeClass("forbid_submit");
                    $("#"+$obj_id).find("span").hide();
                    
                },
                error: function() {
                    layer.msg("系统繁忙，请稍后再试");
                    $("#"+$obj_id).removeClass("forbid_submit");
                    $("#"+$obj_id).find("span").hide();
                }
            });
  
        }
        
    });

    
    $('[f="buy_set_menu"]').on('click', function() {
          if($(this).hasClass('forbid_submit')){
            return;
          }
          $(this).closest('li').addClass('current').siblings().removeClass('current');
          $set_menu = $(this).attr('data-id');
          $(this).find('span').show();
          _postBuyVip($set_menu);
    });

});	

//购买套餐 2018.12.02
function _postBuyVip($tcid) {
    $_fromurl = $("#fromurl").val();
    if (typeof($_fromurl) == "undefined") {
      $_fromurl = "";
    }
    $.ajax({
      type: "POST",
      url: _ROOT_PATH+"index.php?m=user&c=vip",
      cache: false,
      data: {
          a:"confirmbuy", tcid:$tcid, fromurl:$_fromurl, r:get_rndnum(8)
      },
      dataType: "json",
      success: function($data) {
          $('[f="buy_set_menu"]').removeClass("forbid_submit");
          $('[f="buy_set_menu"]').find('span').hide();
          $json = eval($data);
          $response = $json.response;
          $result = $json.result;
          if ($response == "1") {
              //余额支付...
              if ($_fromurl.length == 0) {
                  $_fromurl = _ROOT_PATH+"index.php?m=user&c=vip&nofollow=1";
              }
              layui.msg("开通成功");
              setTimeout(function(){
                  window.location.href = $_fromurl;
              }, 500);
              
          }
          else if ($response == "2") {
              //在线支付...
              if ($_fromurl.length == 0) {
                  $_fromurl = _ROOT_PATH+"index.php?m=user&c=vip&nofollow=1";
              }
              window.top.location.href = _ROOT_PATH+"index.php?c=pay&paynum="+$result;
          }
          else {
              $_error = $result;
              if ($_error.length == 0) {
                  $_error = "开通失败，请检查！";
              }
              layer.msg($_error)
          }
      },
      error: function() {
          $('[f="buy_set_menu"]').find('span').hide();
          layer.msg("系统繁忙，请稍后再试");
          $('[f="buy_set_menu"]').removeClass("forbid_submit");
      }
    });
}*/
