// pages/tabchange/tabchange.js
const app=getApp();
Page({

 /**
  * 页面的初始数据
  */
 data: {
  status: 0,//当前所在滑块的 0全部,1已下单,2已取货,3已到达,4已到达绿色分拣厂,5已分类,6已再生利用
  tap:true,
  orderlist:[],
  is_mobile:''
 },
 is_phone(){
  let that=this;
  let url ='home/home/is_phone';
  let header={
   'XX-Token':wx.getStorageSync('token')
  }
  app.post(url, {}, header).then((res)=>{
   that.setData({
    is_mobile: res.is_mobile
   })
  }).catch((err)=>{
   console.log(err)
  })
 },
 // 手机号授权
 getPhoneNumber: function (e) {
  let that = this;
  wx.login({
   success: function (s) {
    let url = 'wxapp/public/getSessionKey';
    if (s.code) {
     var code = s.code;
     var param = {
      code: code
     }
     app.post(url, param).then((res) => {
      that.login_phone(res.openid, res.session_key, e.detail.encryptedData, e.detail.iv);
     }).catch((errMsg) => {
      console.log(errMsg);
     })
    }
   }
  });
 },
 login_phone(openid, session_key, encrypted_data, iv) {
  let that = this;
  let param = {
   openid: openid,
   session_key: session_key,
   encrypted_data: encrypted_data,
   iv: iv
  }
  let url = 'wxapp/public/login_phone';
  app.post(url, param).then((res) => {
   wx.navigateTo({
    url: '/pages/appointment/appintment',
    success: function (res) { },
    fail: function (res) { },
    complete: function (res) { },
   })
  }).catch((errMsg) => {
   console.log(errMsg);
  })
 },
 //查看物流
 lookwuliu(e){
  console.log(e);
  wx.navigateTo({
   url: '/pages/wuliu/wuliu?id=' + e.currentTarget.id,
   success: function(res) {},
   fail: function(res) {},
   complete: function(res) {},
  })
 },
 exitorder(e){
  console.log(e);
  wx.navigateTo({
   url: '/pages/appointment/appintment?id='+e.currentTarget.id,
  })
 },
 //tab切换
 tab: function (event) {
  let that=this;
  this.setData({ 
   status: event.target.dataset.status 
   })
  that.getpostdata();
 },
 //滑动事件
 eventchange: function (event) {
  let that=this;
  this.setData({ status: event.detail.current });
  that.getpostdata();
 },
 getpostdata() {
  //status ''全部,1-已下单,2-已取货,3-已到达,4已到达绿色分拣厂,5-已分类,6已再生利用
  let that = this;
  let url = 'home/Member_center/order';
  let header = {
  'XX-Token': wx.getStorageSync('token')
  }
  
  let param = {
   'status': that.data.status
  }
  app.post(url, param, header).then((res) => {
  
   let statustxtarray = ['已下单', '已取货', '已到达', '已到达绿色分拣厂', '已分类','再生利用']
   for(let obj of res){
    obj.statustxt = statustxtarray[obj.status-1]
   }
   that.setData({
    orderlist: res
   })
  }).catch((err) => {
   console.log(err)
  })
 },
 goorderdetail(e){
  let that=this;
  wx.navigateTo({
   url: '/pages/order_detail/order_detail?id=' + e.currentTarget.id,
  })
 },
 cancelorder(e){
  let that=this;
  let id = e.currentTarget.id;
  let index=e.currentTarget.dataset.index;
  wx.showModal({
   title: '提示',
   content: '是否取消订单',
   showCancel: true,
   cancelText: '',
   cancelColor: '否',
   confirmText: '是',
   confirmColor: '',
   success: function(res) {
    if (res.confirm) {
     console.log('用户点击确定')
     that.cancel_order(id,index);
    } else if (res.cancel) {
     console.log('用户点击取消')
    }
   },
   fail: function(res) {},
   complete: function(res) {},
  })


 },
 //取消订单
 cancel_order(id,index){
  let that=this;
  let orderlist = that.data.orderlist;
  let url ='home/Member_center/cancel_order';
  let header={
   'XX-Token':wx.getStorageSync('token')
  }
  let params={
   id:id
  }
  app.post(url,params,header).then((res)=>{
   wx.showToast({
    title: '操作成功',
   })
   orderlist.splice(index, 1);
   that.setData({
    orderlist: orderlist
   })
  }).catch((err)=>{
   console.log(err)
  })
 },
 /**
  * 生命周期函数--监听页面加载
  */
 onLoad: function (options) {
  let that=this;
  that.setData({
   status: options.current
  })
  that.getpostdata();
  that.is_phone();
 },
 goindex() {
  wx.switchTab({
   url: '/pages/index/index',
  })
 
 },
 /**
  * 生命周期函数--监听页面初次渲染完成
  */
 onReady: function () {

 },

 /**
  * 生命周期函数--监听页面显示
  */
 onShow: function () {
  let that=this;
  that.getpostdata();
  that.is_phone();

 },

 /**
  * 生命周期函数--监听页面隐藏
  */
 onHide: function () {

 },

 /**
  * 生命周期函数--监听页面卸载
  */
 onUnload: function () {

 },

 /**
  * 页面相关事件处理函数--监听用户下拉动作
  */
 onPullDownRefresh: function () {

 },

 /**
  * 页面上拉触底事件的处理函数
  */
 onReachBottom: function () {

 },

 /**
  * 用户点击右上角分享
  */
 onShareAppMessage: function () {

 }
})