const navigationBarHeight = (getApp().statusBarHeight + 44) + 'px'
let config = require("../../../config/config.js");
let Http = require("../../../utils/HttpBasics");
const util = require("../../../utils/util");
let app = getApp();
const imgurl = require("../../../utils/imgurl");
Page({
  data: {
    navigationBarHeight,
    noOrdersUrl: imgurl.noOrders.url,
    loadingUrl: imgurl.loading.url,
    wmhome: imgurl.wmhome.url,
    wmdiscount: imgurl.wmdiscount.url,
    wmgive: imgurl.wmgive.url,
    tabs: [{
        key: "all",
        name: "全部"
      },
      {
        key: 0,
        name: "待付款"
      },
      {
        key: 1,
        name: "已完成"
      }
    ],
    list: [],
    current: "",
    loading: true, //"上拉加载"的变量，默认false，隐藏
    current_scroll: "1",
    allow_load: true,
    page: 1,
    content: "",
    goHomeUrl:""
  },
  goback: function () {
    let this_ = this
    wx.switchTab({
      url: this_.data.goHomeUrl,
    })
  },
  onLoad(e) {
    this.getList(e.id, 1);
    this.setData({
      current_scroll: e.id,
      goHomeUrl: app.globalData.goHomeUrl,
    });
  },
  onShow(){
    this.getList('all',1)
  },
  /**  
   * 砍价中的状态
   * 跳转到砍价详情页面
   */
  gotoDiscount:function(e){
    wx.navigateTo({
      url: `/pages/bargain/bargainDatail/bargainDatail?orderId=${e.currentTarget.dataset.id}`
    })
  },
  gotopay: function (e) {
    let shippingtype = e.currentTarget.dataset.shippingtype

    // if (e.currentTarget.dataset.shipping == 1 || e.currentTarget.dataset.shipping == 2){//线上购物
    //   wx.navigateTo({
    //     url: `/pages/order/detail/index?orderId=${e.currentTarget.dataset.id}&dingdan=order&flag='pay'`
    //   })
    // }else{
    //   wx.navigateTo({
    //     url: `/pages/order/detail/index?orderId=${e.currentTarget.dataset.id}&flag='pay'`
    //   })
    // }
    if (shippingtype != 1 && shippingtype!=2) {
      wx.navigateTo({
        url: `/pages/order/detail/index?orderId=${e.currentTarget.dataset.id}&flag='pay'`
      })
    }else{
      wx.navigateTo({
        url: `/pages/order/detail/index?orderId=${e.currentTarget.dataset.id}&dingdan=order&flag='pay'`
      })
    }
    
    
  },
  //再次购买
  gotopayAgain: function (e) {
    wx.navigateTo({
      url: `/pages/coupon/detail/index?couponChannelId=${
        e.currentTarget.dataset.couponchannelid
        }&couponId=${e.currentTarget.dataset.couponid}`
    });
  },
  // gotoSpellPay(e) {
  //   console.log(e, 333)
  //   let data = e.currentTarget.dataset.data
  //   wx.navigateTo({
  //     url: `/pages/spellGroup/paySpellGroup/index?orderId=${data.orderId}&couponId=${data.couponId}&orderGroupId=${data.orderGroupId}&couponChannelId=${data.couponChannelId}`,
  //   })
  // },
  getList(key, pageNum) {
    let that = this;
    if (key == undefined) {
      key = 'all'
    }
    /**
     * key==0
     * 不发送该字段
     */
    if (key == 'all') {
      var variable = {
        pageNum: pageNum,
        pageSize: 15,
        paymentType: 0
      };
    } else {
      var variable = {
        pageNum: pageNum,
        pageSize: 15,
        orderStatus: key,
        paymentType: 0 
      };
    }

    if (that.data.allow_load) {
      that.setData({
        loading: true,
        content: '小主,我在玩命加载中...'
      })

      Http.get({
        url: config.api.orderList,
        data: variable
      }).then(res => {
        if (pageNum >= res.data.pages) {
          that.setData({
            allow_load: false
          });
          setTimeout(function () {
            that.setData({
              loading: false,
            })
          }, 1400);
        }
        setTimeout(function () {
          that.setData({
            loading: false,
          })
        }, 1400);
        if (pageNum == 1) {
          that.setData({
            list: []
          })
        }
        var tmpArr = that.data.list;
        tmpArr.push.apply(tmpArr, res.data.list);
        // 将砍价的状态过滤出来
        console.log(tmpArr)
        tmpArr.map(item=>{
     
          item.orders.map(itemChild=>{
              itemChild.createDate = util.formatTime(itemChild.createDate, "yyyy-MM-dd hh:mm:ss");
            })

        })
        
        that.setData({
          list: tmpArr
        })
        // for (let i = 0; i < that.data.list.length; i++) {
        //   var createDate1 = util.formatTime(that.data.list[i].createDate, "yyyy-MM-dd hh:mm:ss");
        //   /**
        //    * 修改list的endtime
        //    * 渲染到页面
        //    */
        //   var createDate = 'list[' + i + '].createDate'
        //   that.setData({
        //     [createDate]: createDate1
        //   });
        // }

      })
      .catch(err => {
        wx.showModal({
          title: '提示',
          content: err.errMsg,
          showCancel:false
        })
      })
    } 
    else {
      that.setData({
        loading: true,
        content: "——— 再拉裤子就掉了啦 ———"
      })
      setTimeout(function () {
        that.setData({
          loading: false,
        })
      }, 1400)
    }
  },
  handleChange({
    detail
  }) {
    this.setData({
      current: detail.key
    });
  },
  handleChangeScroll({
    detail
  }) {
    this.setData({
      list: [],
      allow_load: true,
      current_scroll: detail.key,
      page: 1
    })
    this.getList(detail.key, 1);
  },
  onReachBottom: function () {
    var that = this;
    that.data.page++;
    that.setData({
      page: that.data.page
    });
    that.getList(that.data.current_scroll, that.data.page);
  },
});