let config = require("../../../config/config.js");
let Http = require("../../../utils/HttpBasics");
const util = require("../../../utils/util");
let app = getApp();

Page({
  data: {
    tabs: [{
        key: "all",
        name: "全部"
      },
      {
        key: 0,
        name: "待付款"
      },
      {
        key: 1,
        name: "已完成"
      }
    ],
    list: [],
    current: "",
    current_scroll: "1",
    page: 1,
    allow_load: true,
    loading: true, //"上拉加载"的变量，默认false，隐藏
    content: "",
  },
  onLoad(e) {
    this.getList(e.id, 1);
    this.setData({
      current_scroll: e.id
    });
  },

  gotopay: function (e) {
    console.log("orderId" + e.currentTarget.dataset.id);
    wx.navigateTo({
      url: `/pages/order/detail/index?orderId=${e.currentTarget.dataset.id}&flag='pay'`
    })
  },
  getList(key, pageNum) {
    let that = this;
    console.log(key);
    /**
     * key==0
     * 不发送该字段
     */
    if (key == 'all') {
      var variable = {
        pageNum: pageNum,
        pageSize: 5,
        paymentType: 0
      };
    } else {
      var variable = {
        pageNum: pageNum,
        pageSize: 5,
        orderStatus: key,
        paymentType: 0
      };
    }

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

      Http.get({
        url: config.api.orderList,
        data: variable
      }).then(res => {
        console.log(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);
        that.setData({
          list: tmpArr
        })
        // for (let i = 0; i < that.data.list.length; i++) {
        //   that.setData({
        //     createDate: util.formatTime(that.data.list[i].createDate, "201y-M-d h:m:s")
        //   })
        // }


        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
          });
        }
      });
    } 
    else {
      console.log("加载完成allow_load设置成false");
      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);
  }
});