// pages/tabchange/tabchange.js
const app=getApp();
var QQMapWX = require('../../libs/qqmap-wx-jssdk.js');
var qqmapsdk;
Page({

 /**
  * 页面的初始数据
  */
 data: {
  status: 0,//当前所在滑块的 0全部,1已下单,2已取货,3已到达,4已到达绿色分拣厂,5已分类,6已再生利用
  tap:true,
  orderlist:[],
  
 },
 
 //查看物流
 lookwuliu(e){
  wx.navigateTo({
   url: '/pages/wuliu/wuliu?id=' + e.currentTarget.id,
   success: function(res) {},
   fail: function(res) {},
   complete: function(res) {},
  })
 },
 exitorder(e){
  wx.navigateTo({
   url: '/pages/appointment/appintment?id=' + e.currentTarget.id ,
  })
 },
 //到预约页
 goappintment() {
  if (app.globalData.city == '') {
   wx.getLocation({
    success: function (res) {
     let location = {
      latitude: res.latitude,
      longitude: res.longitude
     }
     qqmapsdk.search({
      keyword: '全部',
      location: location,
      success: function (res) {
       let city = res.data[0].ad_info.city;
       let province = res.data[0].ad_info.province;
       app.globalData.city = city;

      },
      fail: function (res) {
      },
      complete: function (res) {
      }
     });
    },
   })
  } else {
   wx.navigateTo({
    url: '/pages/appointment/appintment',
    success: function (res) { },
    fail: function (res) { },
    complete: function (res) { },
   })
  }
 },
 //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];
    if (obj.status=='-1'){
     obj.statustxt='已取消'
    }
   }
   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,
   cancelColor:'#000000',
   cancelText: '否',
   confirmText: '是',
   confirmColor:'#3cc51f',
   success: function(res) {
    if (res.confirm) {
     that.cancel_order(id,index);
    } else if (res.cancel) {
    }
   },
   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;
  qqmapsdk = new QQMapWX({
   key: 'GERBZ-FTDR6-M4HS4-MRP44-25KPT-UCBBV'
  });
  that.setData({
   status: options.current
  })
  that.getpostdata();
 },
 goindex() {
  wx.switchTab({
   url: '/pages/index/index',
  })
 
 },
 /**
  * 生命周期函数--监听页面初次渲染完成
  */
 onReady: function () {

 },

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

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

 },

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

 },

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

 },

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

 },

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

 }
})