var t = getApp(), n = t.request;

module.exports = {
    wx_paying:0,
    url: "/api/weshop/plus/create/plus/order",
    set_pay_url:function (url){
      this.url=url;//让链接地址进行赋值
    },
    set_fir(){
       this.wx_paying=0;
    },
    //-- createRechargeOrder  create/plus/order --
    // get_order是通联专用，获取订单号
    pay: function(dd, succ,fail,th) {
        if(this.wx_paying) return false;
        this.wx_paying=1;
        wx.showLoading();

        getApp().globalData.is_tonglian_pay=0; //是通联支付的时候

        var i = this;
        n.post(this.url, {
            data: dd,
            success: function(t) {
                wx.hideLoading();
                if(t.data.code==0){
                    if(t.data.data=="升级成功"){
                        "function" == typeof succ && succ(1);
                    }else{

                        var is_pos_pay=t.data.data.is_pos_pay;
                        if(t.data.data.is_fuioupay){
                            const plugin = requirePlugin('fuiou-pay')
                            getApp().globalData.no_clear=1;
                            getApp().globalData.fuiou_pay=1;

                            var paramsJson={
                                reqType:"11",//必填 01-商户模式，11-机构模式
                                reqData:t.data.data.reqData,//必填 富友加密参数
                            }
                            console.log(plugin, plugin.fuioupay)
                            plugin.fuioupay(parseInt(t.data.data.amt),paramsJson,function (res){
                                console.log(res);
                                getApp().globalData.fuiou_pay=0;
                                i.wx_paying=0;
                                "function" == typeof succ && succ();
                            },function (error){
                                console.log("插件支付错误",error);
                                //支付失败的时候
                                //getApp().confirmBox(error.code+error.message);
                                var msg='取消支付';
                                getApp().globalData.fuiou_pay=0;
                                if(error.code){
                                    msg=error.code+":"+error.message;
                                }
                                fail(msg)
                                i.wx_paying=0;
                            })
                        }
                        //-- 通联的支付插件 --
                        else if(is_pos_pay){
                            var p_data=t.data.data;
                            delete p_data.is_pos_pay; //清理掉，不然签名不通过

                            if(th){

                                var ordno=p_data.reqsn;
                                if(ordno.indexOf('_')){
                                    var str_arr=ordno.split('_');
                                    ordno=str_arr[0];
                                }
                                th.data.ok_order_sn=ordno;
                            }

                            getApp().globalData.is_tonglian_pay=1; //是通联支付的时候

                            wx.openEmbeddedMiniProgram({
                                appId: 'wxef277996acc166c3',
                                extraData: p_data,
                                fail(){

                                    //-- 取消不支付 --
                                    i.wx_paying=0;
                                    "function" == typeof fail && fail('取消支付');
                                }
                            })
                        }else {
                            i.weixinPay(t.data.data, succ, fail);
                        }
                    }
                }
                else if(t.data.code==-2 && dd.rechargeType==3){
                    i.wx_paying=0;
                    "function" == typeof fail && fail(t.data);
                }
                else{
                    i.wx_paying=0;
                    "function" == typeof fail && fail(t.data.msg);
                }
                return false;
            },
            fail: function() {
                wx.hideLoading();
                i.wx_paying=0;
                "function" == typeof fail && fail();
            },
            failStatus: function() {
                wx.hideLoading();
                i.wx_paying=0;
                "function" == typeof fail && fail();
            }
        });
    },

    //-- 这里是专享礼包的调用 --
    pay_data:function (t,succ,fail,get_order){
        var th=this;
        var ob=t.data.data.result;
        this.wx_paying=1;

        if(!ob){
            this.set_fir();
            fail('支付参数错误');
            return false;
        }

        getApp().globalData.is_tonglian_pay=1; //是通联支付的时候

        if(ob.is_fuioupay){
            const plugin = requirePlugin('fuiou-pay')

            var paramsJson={
                reqType:"11",//必填 01-商户模式，11-机构模式
                reqData:ob.reqData,//必填 富友加密参数
            }
            getApp().globalData.no_clear=1;
            getApp().globalData.fuiou_pay=1;


            console.log(plugin, plugin.fuioupay)
            plugin.fuioupay(parseInt(ob.amt),paramsJson,function (res){
                getApp().globalData.fuiou_pay=0;
                console.log(res);
                th.wx_paying=0;
                "function" == typeof succ && succ();
            },function (error){
                getApp().globalData.fuiou_pay=0;
                th.wx_paying=0;
                console.log("插件支付错误",error);
                //支付失败的时候
                //getApp().confirmBox(error.code+error.message);
                var msg='取消支付';
                if(error && error.code){
                    msg=error.code+":"+error.message;
                }
                fail(msg)

            })
        }

        //-- 通联的支付插件 --
        else if(ob.is_pos_pay){
            var p_data=ob;
            delete p_data.is_pos_pay; //清理掉，不然签名不通过
            if(get_order){
                get_order(p_data.reqsn); //把单号返回
            }

            getApp().globalData.is_tonglian_pay=1; //是通联支付的时候

            wx.openEmbeddedMiniProgram({
                appId: 'wxef277996acc166c3',
                extraData: p_data,
                fail(){
                    //-- 取消不支付 --
                    th.wx_paying=0;
                    "function" == typeof a && a();
                }
            })
        }

        else {
            this.weixinPay(ob, succ, fail);
        }
    },
    weixinPay: function(n, succ,fail) {
		if(!n){
            this.set_fir();
            return false;
        }

        var th=this;
        getApp().globalData.no_clear=1;

        wx.requestPayment({
            timeStamp: String(n.timeStamp),
            nonceStr: n.nonceStr,
            package: n.packageValue,
            signType: n.signType,
            paySign: n.paySign,
            success: function(n) {
                th.wx_paying=0;
                "function" == typeof succ && succ();
            },
            fail: function(n) {
                th.wx_paying=0;
                var msg='';
                switch (n.errMsg){
                    case "requestPayment:fail": msg="支付失败";break;
                    case "requestPayment:fail cancel": msg="您已取消支付";break;
                    default: msg="支付失败：" + n.errMsg.substr("requestPayment:fail ".length);
                }
                console.log(n);
                "function" == typeof fail && fail(msg);
            }
        });
    }
};