/**
 * [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/12/01 by BO $
*/
$(function () {
    function Pay () {
        this.selectPaytype();
        this.selectOnlinepay();
        this.submit();
        this.selectBalance();
    }
    Pay.prototype = {
        selectPaytype: function () {
            //选择支付类型
            $(document).on("click", "[f='but_select_paytype']", function(){
                var $val = $(this).attr("data-val");
                $(this).addClass("current").siblings().removeClass("current");
                $("#paytype").val($val);
                    
                //使用余额支付... 8.12.02
                if ($val == "balance") {
                    $("[f='but_select_onlinepay']").removeClass("current");
                }

                //选择支付方式 19.02.25 by $bo
                $('[data-name="'+ $val +'"]').show().siblings('[data-name]').hide();
            });
        },
        selectOnlinepay: function () {
           //选择在线支付方式
          $(document).on("click", "[f='but_select_onlinepay']", function(){
              var $_sdkid = $(this).attr("data-sdkid");
              var $_sdkname = $(this).attr("data-sdkname");
              $("[f='but_select_onlinepay']").removeClass("current");
              $(this).addClass("current");

              $("#sdkid").val($_sdkid);
              $("#sdkname").val($_sdkname);
              $('#paytype').val('onlinepay');
              $('[f="select_paytype"]').removeClass('checked');
          }); 
        },
        submit: function () {
            //提交支付...
            var _this = this;
            $(document).on("click", "[f='but_confirm_pay']", function(){
                var $obj_id = $(this).attr("id");
                if (!$("#"+$obj_id).hasClass("forbid_submit")) {
                    var $paytype = $("#paytype").val();
                    var $paynum = $("#paynum").val();
                    var $sdkid = $("#sdkid").val();
                    var $sdkname = $("#sdkname").val();
                    var $bankid = $("#bankid").val();
                    
                    if ($paytype == "balance") {
                        $sdkid = "999"; //余额支付 8.12.02
                    }
                    if ($sdkid.length == 0) {
                        OEUI.message.msg({text: '请选择支付方式'});
                        return false;
                    }
                    $("#"+$obj_id).addClass("forbid_submit");
                    
                    $.ajax({
                        type: "POST",
                        url: _ROOT_PATH + "index.php?c=pay",
                        cache: false,
                        data: {
                            a:"savepaysdk", paynum:$paynum, sdkid:$sdkid, sdkname:$sdkname, bankid:$bankid
                        },
                        dataType: "json",
                        success: function($data) {
                            var $json = eval($data);
                            var $response = $json.response;
                            var $result = $json.result;
                            if ($response == "1") { //成功
                                if ($sdkid == "999") {
                                    _this.payByBalance($paynum);
                                }
                                else {
                                    window.location.href = _ROOT_PATH+"index.php?c=pay&a=submit&paynum="+$paynum;
                                }
                            }
                            else {
                                if ($result.length > 0) {
                                    OEUI.message.msg({text: $result});
                                }
                                else {
                                    OEUI.message.msg({text: '支付失败，请检查！'});
                                }
                                $("#"+$obj_id).removeClass("forbid_submit");
                            }
                        },
                        error: function() {
                            OEUI.message.msg({text: '系统繁忙，请稍后再试'});
                            $("#"+$obj_id).removeClass("forbid_submit");
                        }
                    });
                
                }
          });
        },
        payByBalance: function ($paynum) {
            var $forward = $("#forward").val();
            if (typeof($forward) == "undefined") {
                $forward = "";
            }
            if ($forward.length == 0) {
                $forward = _ROOT_PATH+"index.php?m=user";
            }
            $.ajax({
                type: "POST",
                url: _ROOT_PATH + "index.php?c=pay",
                cache: false,
                data: {
                        a:"paybalance", paynum:$paynum, r:get_rndnum(8)
                },
                dataType: "json",
                success: function($data) {
                    var $json = eval($data);
                    var $response = $json.response;
                    var $result = $json.result;
                    
                    if ($response == "1") {
                        OEUI.message.msg({text: '支付成功'});
                        setTimeout(function(){
                            window.location.href = $forward;
                        }, 500);
                    }
                    else {
                        if ($result.length > 0) {
                            OEUI.message.msg({text: $result});
                        }
                        else {
                            OEUI.message.msg({text: '支付失败，请检查'});
                        }
                    }
                    $("#but_confirm_pay").removeClass("forbid_submit");
                },
                error: function() {
                    OEUI.message.msg({text: '系统繁忙，请稍后再试'});
                    $("#but_confirm_pay").removeClass("forbid_submit");
                }
            }); 
        },
        selectBalance: function () {
            document.querySelector('[f="select_paytype"]').addEventListener('change', function (e) {
                var val = e.detail.value;
                if (val == '1') {
                  $('#paytype').val('balance');
                  $('[f="but_select_onlinepay"]').removeClass('current');
                } else {
                  $('#paytype').val('');
                  $("#sdkid").val('');
                }
            });
        }
    }
    new Pay();
});
