var util = require('../../../utils/util.js');
var mall = require('../../../api/mall.js'); 
var app = getApp();
Page({
  data: {
    orderList: [],
    showType: 0,
    page: 1,
    limit: 10,
    totalPages: 1,
    myqr_show:false,//我的分享二维码
    myqr_url:null,
    myqr_blur:0,//背景模糊值
    myqr_width:0,
    hasLogin:false,
    goodsId:''
  },
  onLoad: function(options) {
    // 页面初始化 options为页面跳转所带来的参数
    // let that = this
    // try {
    //   var tab = wx.getStorageSync('tab');

    //   this.setData({
    //     showType: tab
    //   });
    // } catch (e) {}
  this.getOrderList();
  },
  getOrderList() {
    wx.showLoading({
      title: '加载中',
    });
    let that = this;
    util.request(mall.OrderList, {
      showType: that.data.showType,
      page: that.data.page,
      limit: that.data.limit
    }).then(function(res) {
      wx.hideLoading();
      if (res.errno === 0) {
        console.log(res.data);
        that.setData({
          orderList: that.data.orderList.concat(res.data.list),
          totalPages: res.data.pages
        });
      }
    });
	},
	getToolkitList() {
    wx.showLoading({
      title: '加载中',
    });
    let that = this;
    util.request(mall.ToolkitList, {
    
      page: that.data.page,
			size: that.data.limit,
			// disStatus:2
    }).then(function(res) {
      wx.hideLoading();
      if (res.errno === 0) {
        console.log(res.data);
        that.setData({
          orderList: that.data.orderList.concat(res.data.list),
          totalPages: res.data.pages
        });
      }
    });
  },
  onReachBottom() {
    if (this.data.totalPages > this.data.page) {
      this.setData({
        page: this.data.page + 1
      });
      if(this.data.showType!=6){
        this.getOrderList();
      }else{
				// 待领取订单
				this.getToolkitList();
      }
      
    } else {
      wx.showToast({
        title: '没有更多订单了',
        icon: 'none',
        duration: 2000
      });
      return false;
    }
  },
 
  onTabChange(e){
 
  let showType = e.detail.name;
  this.setData({
    orderList: [],
    showType: showType,
    page: 1,
    limit: 10,
    totalPages: 1
  });
  if(showType==6){
		this.getToolkitList();
  }else{
    this.getOrderList();
  }
  
  },
 
  viewService(e){
    let id=e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../serviceDetail/serviceDetail?orderId=' +id
    })
  },
  qrtouchstart:function(){
    this.setData({
      myqr_blur:0,
      myqr_show:false  
    });
  },
  shareFriendOrCircle: function(e) {  
    // let that = this;
    // if (!app.globalData.hasLogin) {
    //   wx.navigateTo({
    //     url: "/pages/auth/login/login"
    //   });
    //   return;
    // }
    // //获取分享id 用来追踪链接
    // //引荐人和非引荐人的 链接不同
    // let imgUrl = that.data.goods.shareWxUrl ? that.data.goods.shareWxUrl : that.data.goods.gallery[0]; 
    // var path = '/pages/goods/goods?id=' + that.data.id + '&rId=' + that.data.userInfo.id;
    // if (that.data.userInfo.isDis) { //是引荐人 
    //   path = '/pages/goods/goods?id=' + that.data.id + '&shareId=' + that.data.wxShareId;
    // }
    // this.hidePoster();
    // return {
    //   title: that.data.goods.name,
    //   desc: that.data.myqr_ads,
    //   path: path,
    //   imageUrl: imgUrl
    // }
  },
  // 保存分享图
  saveShare: function() {
    let that = this;
    wx.showLoading({
      title: '图片保存中',
    });
    console.log(that.data);
    wx.downloadFile({
      url: that.data.myqr_url,
      success: function(res) {
        console.log(res)
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success: function(res) {
            wx.hideLoading();
            wx.showModal({
              title: '存图成功',
              content: '图片成功保存到相册了，可以分享到朋友圈了',
              showCancel: false,
              confirmText: '好的',
              confirmColor: '#a78845',
              success: function(res) {
                if (res.confirm) {
                  console.log('用户点击确定');
                  that.setData({ 
                    myqr_show:false,
                    myqr_blur:0
                    }); 
                }
              }
            })
          },
          fail: function(res) {
            wx.hideLoading();
           wx.showModal({
              title: '警告',
              content: '不授权无法保存',
              showCancel: false
          })
          that.setData({ 
            myqr_show:false,
            myqr_blur:0
            }); 
        }
        })
      },
      fail: function(res) {
        wx.hideLoading();
        console.log(res);
      }
    })
  },
  // “去付款”按钮点击效果
  payOrder: function(e) {
    let that = this;
    let id = e.currentTarget.dataset.id;
    util.request(mall.OrderPrepay, {
      orderId: id
    }, 'POST').then(function(res) {
      if (res.errno === 0) {
        const payParam = res.data;
        console.log("支付过程开始");
        wx.requestPayment({
          'timeStamp': payParam.timeStamp,
          'nonceStr': payParam.nonceStr,
          'package': payParam.packageValue,
          'signType': payParam.signType,
          'paySign': payParam.paySign,
          'success': function(res) {
            console.log("支付过程成功");
            that.setData({
              orderList: [],
              page: 1,
              limit: 10,
              totalPages: 1
            });
            that.getOrderList();
            // wx.redirectTo({
            //   url: '/pages/mall/order/order'
            // });
            // util.redirect('/pages/mall/order/order');
          },
          'fail': function(res) {
            console.log("支付过程失败");
            util.showErrorToast('支付失败');
          },
          'complete': function(res) {
            console.log("支付过程结束")
          }
        });
      }
    });

  },
   // “删除”点击效果
   deleteOrder: function(e) {
    let that = this;
    let id = e.currentTarget.dataset.id;

    wx.showModal({
      title: '',
      content: '确定要删除此订单？',
      success: function(res) {
        if (res.confirm) {
          util.request(mall.OrderDelete, {
            orderId: id
          }, 'POST').then(res=> {
            if (res.errno === 0) {
              wx.showToast({
                title: '删除订单成功'
              });
              that.setData({
                orderList: [],
                page: 1,
                limit: 10,
                totalPages: 1
              });
              that.getOrderList();
              // wx.redirectTo({
              //   url: '/pages/mall/order/order'
              // });
              // util.redirect('/pages/mall/order/order');
            } else {
              util.showErrorToast(res.errmsg);
            }
          });
        }
      }
    });
  },
 // “取消订单”点击效果
 cancelOrder: function(e) {
  let that = this;
  let id = e.currentTarget.dataset.id;

  wx.showModal({
    title: '',
    content: '确定要取消此订单？',
    success: function(res) {
      if (res.confirm) {
        util.request(mall.OrderCancel, {
          orderId: id
        }, 'POST').then(function(res) {
          if (res.errno === 0) {
            wx.showToast({
              title: '取消订单成功'
            });
            that.setData({
              orderList: [],
              page: 1,
              limit: 10,
              totalPages: 1
            });
            that.getOrderList();
            // wx.redirectTo({
            //   url: '/pages/mall/order/order'
            // });
            // util.redirect('/pages/ucenter/order/order');
          } else {
            util.showErrorToast(res.errmsg);
          }
        });
      }
    }
  });
},
// “取消订单”点击效果
cancelSpec: function(e) {
  let id = e.currentTarget.dataset.id;

  wx.showModal({
    title: '',
    content: '确定我不需要？',
    success: res=> {
      if (res.confirm) {
        util.request(mall.ToolkitReceive, {
					agree:false,
          toolkitId: id
        }, 'POST').then(resp=>{
          if (resp.errno === 0) {
            wx.showToast({
              title: '操作成功'
            });
            this.setData({
              orderList: [],
              page: 1,
              limit: 10,
              totalPages: 1
            });
            this.getToolkitList();
          } else {
            util.showErrorToast(res.errmsg);
          }
        });
      }
    }
  });
},
 // “确认领取”点击效果
 confirmOrder: function(e) {
  let id = e.currentTarget.dataset.id;

  wx.showModal({
    title: '',
    content: '确认领取？',
    success:res => {
      if (res.confirm) {
        util.request(mall.OrderConfirm, {
          orderId: id
        }, 'POST').then(res=> {
          if (res.errno === 0) {
            wx.showToast({
              title: '操作成功！'
            });
						this.setData({
							orderList: [],
							page: 1,
							limit: 10,
							totalPages: 1
						});
						this.getOrderList();
          } else {
            util.showErrorToast(res.errmsg);
          }
        });
      }
    }
  });
},
// “取消订单并退款”点击效果
refundOrder: function(e) {
  let that = this;
  let id = e.currentTarget.dataset.id;

  wx.showModal({
    title: '',
    content: '确定要申请退款么？',
    success: function(res) {
      if (res.confirm) {
        util.request(mall.OrderRefund, {
          orderId: id
        }, 'POST').then(function(res) {
          if (res.errno === 0) {
            wx.showToast({
              title: '申请成功'
            });
            that.setData({
              orderList: [],
              page: 1,
              limit: 10,
              totalPages: 1
            });
            that.getOrderList();
            // wx.redirectTo({
            //   url: '/pages/mall/order/order'
            // });
            // util.redirect('/pages/mall/order/order');
          } else {
            util.showErrorToast(res.errmsg);
          }
        });
      }
    }
  });
},
confirmSpec(e){
	app.globalData.specRefresh=false;
	let id=e.currentTarget.dataset.id;
	wx.navigateTo({
		url: '/pages/mall/toolkitConfirm/toolkitConfirm?id='+id,
	})
},
  onReady: function() {
    // 页面渲染完成
  },
  onShow: function() {
    // 页面显示
		// this.getOrderList(); 
		if(app.globalData.specRefresh&&this.data.showType==6){
			this.setData({
				orderList: [],
				page: 1,
				limit: 10,
				totalPages: 1
			});
		
				this.getToolkitList();
		}
  },
  onHide: function() {
    // 页面隐藏
  },
  onUnload: function() {
    // 页面关闭
  }
})