Vue.config.devtools = true;
var v = new Vue({
    el: ".container",
    data: {
        objType: '',
        objId: '',
        objInfo: {},
        subing: false,
        memToken: '',
        payTypes: [false, true],
        payType: 2,
        objCount: 1,
        orderId: '',
        integralType: "",
        integralCount: "",
        showPoint: false, // 积分提示
        showPointYs: false, // 积分提示
    },
    created: function () {
        if (!checkLogin()) {
            // goLogin();
            return false;
        }
        var that = this;
        that.objType = $("#objType").val();
        that.objId = $("#objId").val();
        if ($("#objCount").val()) {
            that.objCount = $("#objCount").val();
        }
        if ($("#orderId").val()) {
            that.orderId = $("#orderId").val();
        }
        that.getObjInfo();
    },
    mounted: function () {
        var that = this;
        if ($("#appId").val()) {
            if (typeof WeixinJSBridge == "undefined") {
                if (document.addEventListener) {
                    document.addEventListener('WeixinJSBridgeReady',
                        that.onBridgeReady, false);
                } else if (document.attachEvent) {
                    document.attachEvent('WeixinJSBridgeReady',
                        that.onBridgeReady);
                    document.attachEvent('onWeixinJSBridgeReady',
                        that.onBridgeReady);
                }
            } else {
                that.onBridgeReady();
            }
        }
        that.findUserInfoWithSm()
        that.findUserInfoWithYsWeb()
    },
    methods: {
        onBridgeReady() {
            var that = this;
            WeixinJSBridge.invoke('getBrandWCPayRequest', {
                "appId": $("#appId").val(), //公众号名称，由商户传入
                "timeStamp": $("#timeStamp").val(), //时间戳，自1970年以来的秒数
                "nonceStr": $("#nonceStr").val(), //随机串
                "package": $("#packages").val(),
                "signType": $("#signType").val(), //微信签名方式：
                "paySign": $("#paySign").val()
                //微信签名
            }, function (res) {
                //alert(JSON.stringify(res));
                if (res.err_msg == "get_brand_wcpay_request:ok") {
                    //支付成功
                    //积分兑换成功，跳转回商品页面
                    if (that.objType == 1) {
                        that.integralType = "领取课程";
                        that.integralCount = 10;
                    } else if (that.objType == 2) {
                        that.integralType = "领取直播";
                        that.integralCount = 10;
                    } else if (that.objType == 3) {
                        that.integralType = "报名会议";
                        that.integralCount = 50;
                    } else if (that.objType == 4) {
                        that.integralType = "领取课件";
                        that.integralCount = 5;
                    }
                    $(".integral-toast").show();
                    playMusic()
                    setTimeout(function () {
                        $(".integral-toast").fadeOut(200);
                        that.gotoObjDetail();
                    }, 1000);
                } else {
                    //支付失败
                    alert("支付失败");
                    //alert(JSON.stringify(res));
                }
            });
        },
        toPay() {
            var that = this;
            if (that.payType == 1) {
                var orderId = that.createOrder();
                var localurl = encodeURIComponent(window.location.href);
                if (orderId) {
                    that.JSAPIPay(localurl, orderId);
                }
            } else if (that.payType == 2) {
                that.pointPay();
            }
        },
        JSAPIPay(localurl, orderId) {
            var redirect_uri = serverHttp + "/weixin/jsapipay?redirectUrl=" + localurl + "&orderId=" + orderId;
            redirect_uri = encodeURIComponent(redirect_uri);
            location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxa35f548647a3fa20&redirect_uri=" + redirect_uri + "&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect";
        },
        createOrder() {
            var that = this;
            var orderId = "";
            $.ajax({
                type: 'POST',
                url: serverHttp + "/pay/createOrder",
                async: false,
                data: {
                    memToken: that.memToken,
                    objType: that.objType,
                    objId: that.objId,
                    paySource: 1,
                    fromMemCard: $.cookie("fromMemCard"),
                    objCount: that.objCount,
                    orderId: that.orderId

                },
                success: function (data) {
                    if (data.code == 200) {
                        //订单创建成功
                        orderId = data.data;
                    } else {
                        alert(data.message);
                    }
                },
                error: function (data) {
                }
            });
            return orderId;
        },
        pointPay() {
            var that = this;
            $.ajax({
                type: 'POST',
                url: serverHttp + "/pay/pointPay",
                data: {
                    memToken: that.memToken,
                    objType: that.objType,
                    objId: that.objId,
                    paySource: 1,
                    fromMemCard: $.cookie("fromMemCard"),
                    objCount: that.objCount,
                    orderId: that.orderId
                },
                success: function (data) {
                    if (data.code == 200) {
                        //积分兑换成功，跳转回商品页面
                        if (that.objType == 1) {
                            that.integralType = "领取课程";
                            that.integralCount = 10;
                        } else if (that.objType == 2) {
                            that.integralType = "领取直播";
                            that.integralCount = 10;
                        } else if (that.objType == 3) {
                            that.integralType = "报名会议";
                            that.integralCount = 10;
                        } else if (that.objType == 4) {
                            that.integralType = "领取课件";
                            that.integralCount = 5;
                        }
                        $(".integral-toast").show();
                        playMusic()
                        setTimeout(function () {
                            $(".integral-toast").fadeOut(200);
                            that.gotoObjDetail();
                        }, 1000);

                    } else {
                        alert(data.message);
                    }
                },
                error: function (data) {
                }
            });
        },
        payTypeChange(payType) {
            this.payType = payType;
        },
        getObjInfo() {
            var that = this;
            $.ajax({
                type: 'POST',
                url: serverHttp + "/pay/getObjInfo",
                data: {
                    memToken: that.memToken,
                    objType: that.objType,
                    objId: that.objId,
                },
                success: function (data) {
                    if (data.code == 200) {
                        that.objInfo = data.data;
                        if (that.objInfo.objPrice == 0) {
                            that.payTypes = [false, true];
                            that.payType = 2;
                        } else if (that.objInfo.objPoint == 0) {
                            that.payTypes = [true, false];
                            that.payType = 1;
                        }
                    }
                },
                error: function (data) {
                }
            });
        },
        gotoObjDetail() {
            window.location.href=document.referrer;
            // window.history.go(-1);
            // var objType = $("#objType").val();
            // var objId = $("#objId").val();
            // if (objType == 1) {
            //     window.location.href = serverHttp + "/course/courseDetails?from=H5&id=" + objId;
            // } else if (objType == 2) {
            //     window.location.href = serverHttp + "/live/detail?from=H5&liveId=" + objId;
            // } else if (objType == 3) {
            //     window.location.href = serverHttp + "/meeting/details?from=H5&conferId=" + objId;
            // } else if (objType == 4) {
            //     window.location.href = serverHttp + "/courseware/index?from=H5";
            // }
        },
        // 查询是否实名认证过
        findUserInfoWithSm() {
            $.ajax({
                type: 'POST',
                url: "/user/findUserInfoWithSmWeb",
                success: (data) => {
                    this.showPoint = data.code == 200 && data.data == null;
                },
                error: function (data) {
                }
            });
        },
        // 查询是否医师认证过
        findUserInfoWithYsWeb() {
            $.ajax({
                type: 'POST',
                url: "/user/findUserInfoWithYsWeb",
                success: (data) => {
                    this.showPointYs = data.code == 200 && data.data == null;
                },
                error: function (data) {
                }
            });
        },
    }
})
