$(function () {
    // var biaoti1 ='';
    // var biaoti2='';

    $.ajax({
        url:url+"/certification/certification/listForPage",
        dataType: "json",
        type: "POST",
        success: function (response, status, xhr) {
            var textable ='';
            var textable2='';
            for (var i = 0; i < response.length; i++) {
                if (response[i].type == "说明") {
                    textable += "<p class='db-st'><span class='green-text'>â–¼</span>"+response[i].head+"</p><p class='db-content'>";
                    var neirong  = response[i].content.split("{{br}}");
                    for (var a=0; a<neirong.length;a++) {
                        textable += neirong[a]+'<br />';
                    }
                    textable += "</p>";


                } else if (response[i].type == "保障") {

                    textable2 +="<p class='db-st'><span class='orange-text'>â–¼</span>"+response[i].head+"</p><p class='db-content'>";
                    var neirong2  = response[i].content.split("{{br}}");
                    for (var a=0; a<neirong2.length;a++) {
                        textable2 += neirong2[a]+'<br />';
                    }
                    textable2 += "</p>";

                }
            }
            $("#text").html(textable);
            $("#text2").html(textable2);
        }
    });
});

function loadPrice() {
    var price = 0;
    $.ajax({
        url:url+"/jgsz/jgsz/listForPage",
        data: {},
        type: "post",
        dateType: "json",
        async: false,
        success:function (response, status, xhr){
            console.log(response);
            $.each(response,function (i, item) {
               if(item.ming=='担保认证'){
                   price = item.jiage;
               }
            });
        },
        error:function (e) {
            layer.msg("系统错误,请联系客服!", { icon: 5 });
            console.log(JSON.stringify(e));
            console.log(e.status);
            console.log(e.responseText);
        }
    });
    return price;
}

$(function () {
   $("#zfb>a").click(function () {
       if(UserID){
           var price = loadPrice();
           if(price){
               toPay(price,'ALI','支付担保认证')
           }else{
               layer.msg("系统错误,请联系客服!", { icon: 5 });
           }
       }else{
           layer.confirm('尚未登录,请先登录', {
               btn: ['登录','注册'] //按钮
           }, function(){
               window.location.href='../reg201206/login06.html';
           }, function(){
               window.location.href='../reg201206/register06.html';
           });
       }
   });
   $("#wxPay>a").click(function () {
       if(UserID){
           var price = loadPrice();
           if(price){
               toPay(price,'WX','支付担保认证')
           }else{
               layer.msg("系统错误,请联系客服!", { icon: 5 });
           }
       }else{
           layer.confirm('尚未登录,请先登录', {
               btn: ['登录','注册'] //按钮
           }, function(){
               window.location.href='../reg201206/login06.html';
           }, function(){
               window.location.href='../reg201206/register06.html';
           });
       }
   })
});

function toPay(total_amount, fun,content){
    if(userInfo == "" || userInfo == null || userInfo == "null" || userInfo == undefined || userInfo == 'undefined'){
        layer.msg("请重新登录");
        return;
    }
    var ddh ="";
    if(fun == 'WX'){
        $.ajax({
            url:url+"/vip/pay/wxPay",
            data: {
                "total_amount": total_amount,
                "fun": fun,
                "userMobile": userInfo.id,
                "update_user":content
            },
            type: "post",
            dateType: "json",
            async: false,
            success: function success(response, status, xhr) {
                if(response != ""){
                    var arr = response.split("?author1=ASY&author2=LZN");
                    ddh = arr[1];
                    $("#qrcode").empty();
                    $("#WXPayCaptions").attr("src","../images/WXPayCaptions.png");
                    var qrcode = new QRCode(document.getElementById("qrcode"), {
                        width : 200,
                        height : 200
                    });
                    qrcode.makeCode(arr[0]);
                    //捕获页
                    layer.open({
                        type: 1,
                        shade: false,
                        title: "微信支付", //不显示标题
                        content: $('#QrCodeParent'), //捕获的元素,注意:最好该指定的元素要存放在body最外层,否则可能被其它的相对元素所影响
                    });
                } else {
                    layer.msg("系统错误,请联系客服!", { icon: 5 });
                }
            },
            error: function error(e) {
                layer.msg("系统错误,请联系客服!", { icon: 5 });
                console.log(JSON.stringify(e));
                console.log(e.status);
                console.log(e.responseText);
            }
        });
    } else {
        let num = total_amount;
        $.ajax({
            url:url+"/vip/pay/aliPay",
            data: {
                "num": num,
                "fun": content,
                "userMobile": userInfo.id
            },
            type: "post",
            dateType: "json",
            async: false,
            success: function success(response, status, xhr) {
                if(response != "error"){
                    var arr = response.split("?author1=ASY&author2=LZN");
                    ddh = arr[1];
                    $("#qrcode").empty();
                    $("#WXPayCaptions").attr("src","../images/ALIPayCaptions.jpeg");
                    let qr_code = $.parseJSON( arr[0] ); //jQuery.parseJSON(jsonstr),可以将json字符串转换成json对象

                    console.log(qr_code);
                    var qrcode = new QRCode(document.getElementById("qrcode"), {
                        width : 200,
                        height : 200
                    });
                    qrcode.makeCode(qr_code.alipay_trade_precreate_response.qr_code);
                    //捕获页
                    layer.open({
                        type: 1,
                        shade: false,
                        title: "支付宝支付", //不显示标题
                        content: $('#QrCodeParent'), //捕获的元素,注意:最好该指定的元素要存放在body最外层,否则可能被其它的相对元素所影响
                    });
                } else {
                    layer.msg("系统错误,请联系客服!", { icon: 5 });
                }
                // if(response != ""){
                //     myWindow=window.open('',response,'');
                //     myWindow.document.write(response);
                //     myWindow.focus();
                //     // $("#topay").html();
                //     console.log(response);
                // } else {
                //     layer.msg("系统错误,请联系客服!", { icon: 5 });
                //     console.log(status)
                //     console.log(xhr)
                // }
            },
            error: function error(e) {
                layer.msg("系统错误,请联系客服!", { icon: 5 });
                console.log(JSON.stringify(e));
                console.log(e.status);
                console.log(e.responseText);
            }
        });
    }
    checkPay(ddh,fun,total_amount);
}

function checkPay(ddh,way,amount) {
    var pay_status = 1;
    console.log("out_trade_no:"+ddh+
        ",create_user:"+userInfo.id+
        ",pay_way:"+way+
        ",total_amount:"+amount);
    $.ajax({
        url:url+"/vip/pay/checkPay",
        data: {
            "out_trade_no":ddh,
            "create_user":userInfo.id,
            "pay_way":way,
            "total_amount":amount
        },
        type: "post",
        dateType: "json",
        async: false,
        success: function (response, status, xhr) {
            if(response!=null){
                pay_status = response;
            }
        },
        error: function (e) {
            layer.msg("系统错误,请联系客服!", { icon: 5 });
            console.log("查询支付状态失败");
            console.log(e.status);
            console.log(e.responseText);
        }
    });
    if(pay_status.length==1&&parseInt(pay_status)==0){
        layer.closeAll();
        var loading = layer.msg('支付成功,正在修改会员信息,请勿刷新页面', {
            icon: 16,
            shade: 0.8,
            time: false,
            scrollbar: false
        });
        $.ajax({
            url:url+"/user2019/qyml/changeDanBao",
            data:{"id":UserID},
            type: "post",
            dateType: "json",
            async: false,
            success: function (response, status, xhr) {
                if(response==1){
                    setTimeout(function () {
                        layer.close(loading);
                        layer.msg("修改成功,即将跳转!", { icon: 1 });
                        setTimeout(function () {
                            window.location.href='../manage/';
                        },2000);
                    },2000);
                }else{
                    setTimeout(function () {
                        layer.close(loading);
                        layer.msg("修改失败,请联系客服!", { icon: 5 });
                        console.log("changeDanBao!=1");
                    },2000);
                }
            },
            error: function (e) {
                setTimeout(function () {
                    layer.close(loading);
                    layer.msg("修改失败,请联系客服!", { icon: 5 });
                    console.log("修改会员信息失败");
                    console.log(e.status);
                    console.log(e.responseText);
                },2000);
            }
        });
    }else{
        setTimeout(function () {
            checkPay(ddh,way,amount)
        },2000);
    }
}