
var app = getApp()
var that=null
var sopenFrame=null
Page( {
  data: {
    orderImg: '../../images/iconfont-dingdan.png',
    tipWords: '亲，您还没有订单哦！',
    orderHidden:false,
    noorderHidden:true,
    loadingHidden:false,
    orderpayHidden:true,
    orderseefHidden:true,
    orderseef0Hidden:true,
    hxddHidden:true,
    hxm:"",
    myorder:[],
    Start: 0,
    t:0,
    zftype:2,
    FHEnable2:"0",
    nav:0,
    orderid:0,
    fhhome:0,
    bhyy:"",
    image:"",
    image0:"",
    apiurl:""  
  },

  onLoad: function(options) {
      that = this
      if(options.t){
        that.setData({
           t:options.t
         });
      }
      if(options.nav){
      that.setData({
         nav:options.nav
       });
      }
     if(that.data.t==3){
        wx.setNavigationBarTitle({
           title: "自提订单"
        })
     }else if(that.data.t==2){
       wx.setNavigationBarTitle({
          title: "快递订单"
       })
    }else{
      wx.setNavigationBarTitle({
        title: "发货订单"
      })
    }
     
      that.clearCache();//清本页缓存
      that.getlist(0)
      sopenFrame=setInterval(function (){
        if(app.globalData.IFRefreshmyorder==1){
           app.globalData.IFRefreshmyorder=0
            that.getlist(0)
        }
   }, 1000);
    },onUnload: function () {
      wx.navigateBack({//返回
        delta: 3
      })
    }, // 下拉刷新
    onPullDownRefresh: function () {
      //在当前页面显示导航条加载动画
     wx.showNavigationBarLoading(); 
     //显示 loading 提示框。需主动调用 wx.hideLoading 才能关闭提示框
     wx.showLoading({
         title: '刷新中...',
     })
        that.clearCache();
        that.getlist(1);//第一次加载数据
    },

     // 页面上拉触底事件（上拉加载更多）
    onReachBottom: function () {
        that.setData({
        loadingHidden:false
        }); 
        that.getlist(2);//后台获取新数据并追加渲染
    }, 

 // 清缓存  
 clearCache:function(){
    that.setData({
      orderHidden:false,
      noorderHidden:true,
      loadingHidden:false,
      orderpayHidden:true,
      Start: 0,
      orderid:0,
      zftype:2,
      myorder:[]
    }); 
},getlist: function(gp) {
  
      var userId = wx.getStorageSync("userId")
      var LoginId = wx.getStorageSync("LoginId")
      var userInfodata = wx.getStorageSync("userInfo")
      if(userId&&LoginId&&userInfodata){
        var userInfo = JSON.parse(userInfodata)
        that.setData({
             FHEnable2:userInfo.FHEnable2
        })
      
      wx.request({
        url: app.globalData.apiurl + "/xapiajax.ashx", 
        data: { 
          action:"getorderslist",
           t0:(that.data.nav==0?10:that.data.nav),
           t:that.data.t,
           Start:that.data.Start,
           userId:(userId==null?"":userId),
           LoginId:(LoginId==null?"":LoginId)
        },
        method:"GET",
        dataType:"json", 
        header: { 
          "content-type": "application/json" // 默认值
        },
        success (res) { 
            console.log("myorder0 API返回数据:", res.data);
            if(res.data.status==1){
              
              var tmpArr = null;
              tmpArr = that.data.myorder;
              tmpArr.push.apply(tmpArr,res.data.data);
              that.setData({
                  myorder: tmpArr,
                  apiurl:app.globalData.apiurl,
                  Start: that.data.Start+res.data.data.length,
                  loadingHidden:true
              })
              if(gp==0||gp==1){
            if(res.data.data.length>0){
              that.setData({ 
                  orderHidden:false, 
                  noorderHidden:true
                  
              })
            }else{
              that.setData({
                orderHidden:true,
                noorderHidden:false
               })
            }
          }
         
        } else {
          console.log("myorder0 API返回状态错误:", res.data);
        }
        if(gp==1){
          //隐藏loading 提示框
          wx.hideLoading();
          //隐藏导航条加载动画
          wx.hideNavigationBarLoading();
          //停止下拉刷新
          wx.stopPullDownRefresh();
      }
          }
      })
    }else{
      wx.navigateTo({  
        url: "../login/index"
       })
    }
    },viewimages0:function(e) {
      var src = e.currentTarget.dataset.index;
      var src0 = e.currentTarget.dataset.index0;
      var imgList=new Array(); 
      imgList[0]=src;
      imgList[1]=src0;
      wx.previewImage({
          urls: imgList,
          current:imgList[0] 
      })
  },viewimages1:function(e) {
    var src = e.currentTarget.dataset.index;
      var src0 = e.currentTarget.dataset.index0;
      var imgList=new Array(); 
      imgList[0]=src;
      imgList[1]=src0;
      wx.previewImage({
          urls: imgList,
          current:imgList[1] 
      })
},onnav:function(e) {
      var userId = wx.getStorageSync("userId")
      var LoginId = wx.getStorageSync("LoginId")
      if(userId&&LoginId){
      var index = e.currentTarget.dataset.id
      that.setData({
          nav:index
      })
      that.clearCache();
      that.getlist(0);
    }else{
      wx.navigateTo({  
        url: "../login/index"
       })
    }
    },onshowpay:function(e) {
      var userId = wx.getStorageSync("userId")
      var LoginId = wx.getStorageSync("LoginId")
      if(userId&&LoginId){
      var id = e.currentTarget.dataset.id
      that.setData({
        orderpayHidden:false,
        orderid:id
      })
    }else{
      wx.navigateTo({  
        url: "../login/index"
       })
    }
    },onshowseef:function(e) {
      var userId = wx.getStorageSync("userId")
      var LoginId = wx.getStorageSync("LoginId")
      if(userId&&LoginId){
      var id = e.currentTarget.dataset.id
      var image = e.currentTarget.dataset.index
      var image0 = e.currentTarget.dataset.index0
      that.setData({
        orderseefHidden:false,
        image:image,
        image0:image0,
        orderid:id
      })
    }else{
      wx.navigateTo({  
        url: "../login/index"
       })
    }
    },getbhyyValue:function(e) {
      that.setData({
        bhyy:e.detail.value
      })
       
  },gethxmValue:function(e) {
      console.log("Input value:", e.detail.value);
      that.setData({
        hxm:e.detail.value
      })
      console.log("hxm value set to:", that.data.hxm);
       
  },onshowseef0:function(e) {
      var userId = wx.getStorageSync("userId")
      var LoginId = wx.getStorageSync("LoginId")
      if(userId&&LoginId){
      var id = e.currentTarget.dataset.id
      var image = e.currentTarget.dataset.index
      var image0 = e.currentTarget.dataset.index0
      that.setData({
        orderseef0Hidden:false,
        image:image,
        image0:image0,
        orderid:id
      })
    }else{
      wx.navigateTo({  
        url: "../login/index"
       })
    }
    },weixinpay:function(e) {
      var userId = wx.getStorageSync("userId")
      var LoginId = wx.getStorageSync("LoginId")
      if(userId&&LoginId){
      that.setData({   
          zftype:0
      })
    }else{
      wx.navigateTo({  
        url: "../login/index"
       })
    }
  },jianjinpay:function(e) {
    var userId = wx.getStorageSync("userId")
    var LoginId = wx.getStorageSync("LoginId")
    if(userId&&LoginId){
      that.setData({
          zftype:2
      })
    }else{
      wx.navigateTo({  
        url: "../login/index"
       })
    }
  },qypay:function() {
      var userId = wx.getStorageSync("userId")
      var LoginId = wx.getStorageSync("LoginId")
      if(userId&&LoginId){
      var openid = wx.getStorageSync("openid");
      wx.request({
        url: app.globalData.apiurl + "/xapiajax.ashx", 
        data: { 
          action:"payorder",
           userId:(userId==null?"":userId),
           LoginId:(LoginId==null?"":LoginId),
           openid:(!openid?"":openid),
           orderid:that.data.orderid,
           zftype:that.data.zftype
        },
        method:"POST",
        dataType:"json", 
        header: { 
          "content-type": "application/x-www-form-urlencoded" // 默认值
        },
        success (res0) { 
          if(res0.data.ResultId>0){
            wx.requestPayment({
              timeStamp: res0.data.timeStamp,
              nonceStr: res0.data.noncestr,
              package: res0.data.package,
              signType: 'MD5',
              paySign: res0.data.paySign,
              success (res) { 
                 that.clearCache();
                 that.getlist(0);
              },
              fail (res) {  
                wx.showToast({
                title: JSON.stringify(res),
                icon: "none",
                duration: 3000
               })
              }
            })
          }else{
              wx.showToast({
                 title: res0.data.msg,
                 icon: "none",
                 duration: 3000
              })
             
          }
           
            
          }
      })
    }else{
      wx.navigateTo({  
        url: "../login/index"
       })
    }
    },qyseef:function() {
      var userId = wx.getStorageSync("userId")
      var LoginId = wx.getStorageSync("LoginId")
      if(userId&&LoginId){
      wx.request({
        url: app.globalData.apiurl + "/xapiajax.ashx", 
        data: { 
          action:"seeforder",
           userId:(userId==null?"":userId),
           LoginId:(LoginId==null?"":LoginId),
           orderid:that.data.orderid
        },
        method:"POST",
        dataType:"json", 
        header: { 
          "content-type": "application/x-www-form-urlencoded" // 默认值
        },
        success (res0) { 
          if(res0.data.status==1){
             wx.showToast({
                 title: res0.data.msg,
                 icon: "none",
                 duration: 3000
              })
              that.setData({
                orderseefHidden:true
              })
                 that.clearCache();
                 that.getlist(0);
             
          }else{
              wx.showToast({
                 title: res0.data.msg,
                 icon: "none",
                 duration: 3000
              })
             
          }
           
            
          }
      })
    }else{
      wx.navigateTo({  
        url: "../login/index"
       })
    }
    },qyseef0:function() {
      var userId = wx.getStorageSync("userId")
      var LoginId = wx.getStorageSync("LoginId")
      if(userId&&LoginId){
        if(that.data.bhyy!=""){
      wx.request({
        url: app.globalData.apiurl + "/xapiajax.ashx", 
        data: { 
          action:"seeforder0",
           userId:(userId==null?"":userId),
           LoginId:(LoginId==null?"":LoginId),
           bhyy:that.data.bhyy,
           orderid:that.data.orderid
        },
        method:"POST",
        dataType:"json", 
        header: { 
          "content-type": "application/x-www-form-urlencoded" // 默认值
        },
        success (res0) { 
          if(res0.data.status==1){
             wx.showToast({
                 title: res0.data.msg,
                 icon: "none",
                 duration: 3000
              })
              that.setData({
                orderseef0Hidden:true
              })
                 that.clearCache();
                 that.getlist(0);
             
          }else{
              wx.showToast({
                 title: res0.data.msg,
                 icon: "none",
                 duration: 3000
              })
             
          }
           
            
          }
      })
    }else{
      wx.showToast({
        title: "驳回原因不能为空",
        icon: "none",
        duration: 3000
     })
    }
    }else{
      wx.navigateTo({  
        url: "../login/index"
       })
    }
    },closeorderpay:function(e) {
      that.setData({
        orderpayHidden:true
       })
    },inorderpay:function(e) {

    },closeorderseef:function(e) {
      that.setData({
        orderseefHidden:true
       })
    },closeorderseef0:function(e) {
      that.setData({
        orderseef0Hidden:true
       })
    },oncancel:function(e) {
      var id = e.currentTarget.dataset.id
      that.setData({
        orderid:id
      })
      wx.showModal({
        title: '取消提示',
        content: '此操作不可恢复！您确认要取消吗？',
        success (res1) {
          if (res1.confirm) {
      var userId = wx.getStorageSync("userId")
      var LoginId = wx.getStorageSync("LoginId")
      wx.request({
        url: app.globalData.apiurl + "/xapiajax.ashx", 
        data: { 
          action:"CancelOrder",
           userId:(userId==null?"":userId),
           LoginId:(LoginId==null?"":LoginId),
           orderid:that.data.orderid
        },
        method:"POST",
        dataType:"json", 
        header: { 
          "content-type": "application/x-www-form-urlencoded" // 默认值
        },
        success (res) { 
          if(res.data.status==1){
                wx.showToast({
                title: res.data.msg,
                icon: "success",
                duration: 3000
                })
                that.clearCache();
                that.getlist(0);
          }else{
              wx.showToast({
                 title: res.data.msg,
                 icon: "none",
                 duration: 3000
              })
             
          }
           
        }
      })
    }
  }
    })
  },onqysh:function(e) {
    var id = e.currentTarget.dataset.id
    that.setData({
      orderid:id
    })
    wx.showModal({  
      title: '友情提示',
      content: '确认收货前请确认您是否真的已经收到货了？您确认要收货吗？',
      success (res1) {
        if (res1.confirm) {
    var userId = wx.getStorageSync("userId")
    var LoginId = wx.getStorageSync("LoginId")
    wx.request({
      url: app.globalData.apiurl + "/xapiajax.ashx", 
      data: { 
        action:"qyshorder",
         userId:(userId==null?"":userId),
         LoginId:(LoginId==null?"":LoginId),
         orderid:that.data.orderid
      },
      method:"POST",
      dataType:"json", 
      header: { 
        "content-type": "application/x-www-form-urlencoded" // 默认值
      },
      success (res) { 
        if(res.data.status==1){
              wx.showToast({
              title: res.data.msg,
              icon: "success",
              duration: 3000
              })
              that.clearCache();
              that.getlist(0);
        }else{
            wx.showToast({
               title: res.data.msg,
               icon: "none",
               duration: 3000
            })
           
        }
         
      }
    })
  }
}
  })
},onorderdetail:function(e) {
  var id = e.currentTarget.dataset.id
  wx.navigateTo({  
    url: "../myorderinfo/index?id=" + id + "&t=" + that.data.t
   })
},onhxdd:function(e) {
  var userId = wx.getStorageSync("userId")
  var LoginId = wx.getStorageSync("LoginId")
  if(userId&&LoginId){
  var id = e.currentTarget.dataset.id
  that.setData({
    hxddHidden:false,
    orderid:id,
    hxm:""
  })
  }else{
    wx.navigateTo({  
      url: "../login/index"
     })
  }
},closehxdd:function(e) {
  that.setData({
    hxddHidden:true
   })
},inhxdd:function(e) {

},qxhxdd:function() {
  var userId = wx.getStorageSync("userId")
  var LoginId = wx.getStorageSync("LoginId")
  console.log("hxm value:", that.data.hxm);
  console.log("Full URL:", "../hxorderinfo/index?id=" + that.data.orderid + "&t=" + that.data.t + "&hxm=" + that.data.hxm);
  if(userId&&LoginId){
    if(that.data.hxm!=""){
      wx.navigateTo({  
        url: "../hxorderinfo/index?id=" + that.data.orderid + "&t=" + that.data.t + "&hxm=" + encodeURIComponent(that.data.hxm)
       })
      that.setData({
        hxddHidden:true
       })
    }else{
      wx.showToast({
        title: "核销码不能为空",
        icon: "none",
        duration: 3000
     })
    }
  }else{
    wx.navigateTo({  
      url: "../login/index"
     })
  }
},ondetail:function(e) {
  var id = e.currentTarget.dataset.id
  wx.navigateTo({  
    url: "../details/index?id=" + id
   })
},onckwl:function(e) {
  var wlxx = e.currentTarget.dataset.id
  wx.navigateTo({  
    url: "../myorderwlxx/index?wlxx=" + wlxx
   })
}
})
