var e = getApp();
var regeneratorRuntime = require('../../../../utils/runtime.js');
var ut = require("../../../../utils/util.js");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    url: e.globalData.setting.imghost,
    resourceUrl: e.globalData.setting.resourceUrl,
    iurl: e.globalData.setting.imghost,
    order:null,
    type:1,
    allmoney:0,
    user_money:0,
    order_sn:"",
    pick:null,
    pre_cut:0,//预存金额
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad:async function(options) {
      wx.setNavigationBarTitle({
          title: '支付成功',
      });
			this.setData({
				options,
			});
      var type=options.type,order_sn=options.order_sn;
      var th=this,order=null;

	  getApp().request.promiseGet("/api/weshop/ad/page?pid=1211&store_id=" + getApp().globalData.setting.stoid, {
        data: {
          enabled: 1
        }
      }).then(res => {
        if (res.data.code == 0 && res.data.data.pageData && res.data.data.pageData.length > 0) {
          var a = res.data.data.pageData;
          var arr = new Array();
          for (var i = 0; i < a.length; i++) {
            var tt = {
              'ad_code': getApp().globalData.setting.imghost + a[i].ad_code,
              'media_link': '',
              'ad_weapplink': a[i].ad_weapplink
            };
            arr.push(tt);
          }
          if (arr.length > 0) th.setData({
            banner: arr,
          });
        }
      })

			
			
			// 卡项订单
			if(options.card == 1) {
				let url = '/api/weshop/recharge/user/page'
				let data = {
					store_id: getApp().globalData.setting.stoid,
					user_id: getApp().globalData.userInfo.user_id,
					order_id: this.data.options.order_id,
				};
				
				// 请求数据
				getApp().promiseGet(url, {
					data: data,
				}).then(res => {
					if (res.data.code == 0) {
						console.log('请求成功', res);
						this.setData({
							order: res.data.data.pageData[0],
							order_sn: res.data.data.pageData[0].order_sn,
						});
					} else {
						throw (res);
					};
				}).catch(err => {
					wx.showToast({
						title: err.data.data,
						icon: 'error',
					});
				});
			} else {
				
				//如果是等1，就是单个订单的订单号
				if(type==1){
				    await  getApp().request.promiseGet("/api/weshop/order/page",
				         {data:{store_id:e.globalData.setting.stoid,order_sn:order_sn}}).then(res=>{
				             if(ut.ajax_ok(res)){
				                 order=res.data.data.pageData[0];
				                 th.setData({order:order,order_sn:order_sn})
				             }
				
				    })
				}else{
				    await  getApp().request.promiseGet("/api/weshop/order/page",
				        {data:{store_id:e.globalData.setting.stoid,parent_sn:order_sn}}).then(res=>{
				
				            if(ut.ajax_ok(res)){
				                var allmoney=0,user_money=0;
				                for(var i in res.data.data.pageData){
				                    var item=res.data.data.pageData[i];
				                    allmoney+=item.order_amount;
				                    user_money += item.user_money?item.user_money:0;
				                }
				                order=res.data.data.pageData[0];
				                th.setData({ order: order, type: 2, allmoney: allmoney, order_sn: order_sn, user_money: user_money})
				            }
				
				
				    })
				}
				//--获取门店--
				if(order.exp_type==1) {
				    await getApp().request.promiseGet("/api/weshop/pickup/get/"+ e.globalData.setting.stoid+"/"+order.pickup_id,
				        {1:1}).then(res => {
				        th.setData({pick:res.data.data})
				    });
				};
        //--只使用预存支付，达到免单时要请求预存使用额度----
        if(order.order_amount==0 && order.user_money==0){
          await getApp().request.promiseGet("/api/weshop/orderMore/get/"+ e.globalData.setting.stoid+"/"+order_sn,{1:1}).then(res => {
            if(res.data && res.data.code==0 && res.data.data){
              let pre_cut=res.data.data.pre_cut
              th.setData({pre_cut})
            }
          });
        }
				
			};
      
			
  },
  onReady(){
    var encourage = this.selectComponent("#encourage"); //组件的id
    if (encourage){
      encourage.get_encourage();
    }
  },
  goto: function() {
     var  url= '/pages/index/index/index';
     getApp().goto(url)
  },

  gotoshequn:function(e){
	getApp().goto(e.currentTarget.dataset.url);
  },


})