var t = getApp(),
  a = t.request,
  o = t.globalData.setting,
  os = o,
  i = require("../../../../../utils/util.js"),
  ut = i,
  s = require("../../../../../utils/common.js");
var regeneratorRuntime = require('../../../../../utils/runtime.js');
var t_pay = require("../../../../../utils/pay2.js");

Page({
      data: {
        iurl: o.imghost,
        showModal: false, //弹框控制器
        perpaid: [], //数据,
        paying:0,
        is_get:0
      },
      onLoad:function (){
          //定义第一次进入
          this.data.fir_in=1;
      },
      /* 生命周期函数--监听页面初次渲染完成*/
      onReady: function() {
		var th=this;

		  
		getApp().request.get("/api/wx/weappSendlist/page", {
		  data: {
		    store_id: os.stoid,
		    typeid: "1002"
		  },
		  success: function(res) {
		    if (res.data.code == 0 && res.data.data.pageData.length > 0) {
		      var template_id = res.data.data.pageData[0].template_id;
			  th.setData({template_id:template_id});
			}  
		  }
		});  

		if(getApp().globalData.user_id){
            this.yuck();
        }
		else{
            wx.showToast({ title: "您还未登陆，请先授权",  icon: 'none',  duration: 2000 })
            setTimeout(function () {
                getApp().goto("/packageE/pages/togoin/togoin");
            },1200)
        }

      },
      /*预存款的劵*/
      async yuck() {


        wx.showLoading();
        var user_id = getApp().globalData.user_id;
        var store_id = os.stoid;
        console.log(user_id, "有进来吗银行卡", store_id );
        var e = this,
          perpaid = "";
        await getApp().request.promiseGet("/api/weshop/users/listYucun", {
          data: {
            user_id: user_id,
            store_id: store_id
          }
        }).then(res => {
          // console.log(res.data.data[0].BeginDate)
          var perpaids = res.data.data;
         // var perpaid = res.data.data;
          console.log( "是什么", perpaids);
          e.setData({
            perpaid: perpaids,is_get:1
          });

          wx.hideLoading();

        })

      },
      //立即充值获取参数到弹框
      submit: function(e) {
        var money = e.target.dataset.name;
        var s = e.target.dataset.money;
        var advancek = e.target.dataset.advance;
		
		var item_type=e.target.dataset.item_type;
		this.data.item_type=item_type;
		
        console.log("sss", e.target.dataset.name);
        this.setData({
          showModal: true
        })
        console.log("立即充值", s, "ss", o);
        this.setData({
          BeginSum: money,
          NewSum: s,
          adv: advancek
        })
      },
      //弹窗的方法
      preventTouchMove: function() {

      },
  /**
* 生命周期函数--监听页面显示
*/
  onShow: function () {

      var th=this;
      //判断是不是第一次进入
      if(this.data.fir_in){
          this.data.fir_in=0;
      }else{
          //-- 富友插件 --
          ut.fy_back('',0)
          t_pay.set_fir();

          //-- 通联的第三方支付的返回优化 --
          ut.is_pay_ok(th.data.ok_order_sn,"/packageD/pages/user/deposit/prepaid/msg/msg?is_nd_pw=1","none",function (){
              if(!th.data.ok_order_sn) return false;
              var ck_url="/api/weshop/recharge/get/"+os.stoid+"/"+th.data.ok_order_sn;
              getApp().request.promiseGet(ck_url,{}).then(res=>{
                  if(res.data.code==0 && res.data.data){
                      var item=res.data.data;
                      if(item.pay_status==1){
                          //支付支付，返回首页
                          ut.m_toast('支付成功');
                          setTimeout(()=>{
                              getApp().re_to("/packageD/pages/user/deposit/prepaid/msg/msg?is_nd_pw=1");
                          },1000)

                      }else{
                          //支付支付，返回首页
                          ut.m_toast('支付失败');

                      }
                  }
              })
          });
      }

      wx.setNavigationBarTitle({
          title: "流动资金转预存",
      })

      this.data.paying=0;


        if(getApp().globalData.user_id && !this.data.is_get){
           this.yuck();
        }

  },
      //确定充值
      determine: function(e) {
        if(this.data.paying) return false;
        this.data.paying=1;

		var th=this;  		  
        this.setData({
          showModal: false
        })
        console.log("aaa");
        var moneyone = e.target.dataset.moneyone;
        var money = e.target.dataset.money;
        var advancek = e.target.dataset.adv;
      	
		this.sendsm(function(){
			th.recharge(money, advancek);
		})
			
      },
      //确定接口
      async recharge(money, advancek) {


        console.log(money, "有进来吗银行卡", advancek);
        var user_id = getApp().globalData.user_id;
        var store_id = os.stoid;
        var recharge_type = 0;
		if(this.data.item_type==2) recharge_type=6;
				
        var buynum = 1;
        console.log(money, "有进来吗预存劵", advancek, user_id, store_id );
        var e = this, perpaid = "";
        let th=this;
        await getApp().request.promiseGet("/api/weshop/users/getPay", {
          data: {
            account: money,
            yucun_id: advancek,
            user_id: user_id,
            store_id: store_id,
            recharge_type: recharge_type,
            buynum: buynum ,
          }
        }).then(res => {
             console.log(res);
             var getcode=res.data.code;
             if (getcode!=0)
             {
               wx.showModal({
                 title: '提示',
                 content:res.data.msg
             });
             return false;
             }
 

              var msg = res.data.data;
              var parentSn = msg.order_sn;
              var store_id = os.stoid;
            t_pay.set_pay_url("/api/weshop/order/pay/createRechargeOrder");

            e.data.ok_order_sn=parentSn;
            t_pay.pay({
                parentSn: parentSn,
                store_id: store_id
            }, function () {
                e.jumpPaymentPage();
            }, function (e) {

                th.data.paying=0;
                wx.showToast({
                    title: e,
                    icon: 'none',
                    duration: 2000
                });
            })

          // getApp().request.post("/api/weshop/order/pay/createRechargeOrder", {
          //   data: {
          //     parentSn: parentSn,
          //     store_id: store_id
          //   },
          //   success: function (su) {
          //     console.log("su是什么",su);
          //     var n=su.data.data;
          //     console.log("su是什么", su);
          //
          //     e.weixinPay(n,
          //       function () {
          //        e.jumpPaymentPage();
          //       }, function () {
          //         getApp().showWarning("支付失败");
          //       })
          //   }
          // })
        })    
      },

  //------支付成功页面--------
  jumpPaymentPage: function () {
    wx.redirectTo({
      url:"msg/msg?is_nd_pw=1",
    });
  },

  //------调起支付框--------
  weixinPay: function (n, success, fail) {
	if(!n) return false;

    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) {
        console.log(n), getApp().showSuccess("支付成功！");
        "function" == typeof success && success();
        wx.redirectTo({
          url: "../deposit/deposit"
        });
      },
      fail: function (n) {
        console.log(n), "requestPayment:fail" == n.errMsg ? getApp().showWarning("支付失败") : "requestPayment:fail cancel" == n.errMsg ? getApp().showWarning("您已取消支付") : getApp().showWarning("支付失败：" + n.errMsg.substr("requestPayment:fail ".length)),
          "function" == typeof fail && fail();
      }
    });
  },
  //关闭弹框
  shut: function () {
    this.setData({
      showModal: false
    })
    console.log("bb");

  },
  
  go_detail:function(e){
	  var id=e.currentTarget.dataset.id;
	  getApp().goto('/packageA/pages/chongzhiDetails/chongzhiDetails?id=' + id);
  },
  
  
  //订阅消息提醒
  sendsm:function(func)
    {  
           var template_id = this.data.template_id;    
            // //授权订阅
            wx.requestSubscribeMessage({
              tmplIds: [template_id],
              success(res) {
                  func();
              },
              fail(res) {
                  func();
              }
            })
          
    },
})
