const app = getApp()
const navigationBarHeight = (getApp().statusBarHeight + 50) + "px"
const Http = require("../../../utils/HttpBasics");
const config = require("../../../config/config");
const util = require("../../../utils/util");
Page({
  data: {
    navigationBarHeight,
    boatLsit:[
      {
        name:"未使用",
        value:"0"
      },
      {
        name:"已使用",
        value:"1"
      },
      {
        name:"已过期",
        value:"2"
      },
      {
        name:"已退款",
        value:"3"
      },
    ],
    boatFlag:"0",
    content:"",
    loading: false, //"上拉加载"的变量，默认false，隐藏
    pageNum:1,
    allow_load: true,
    list:[],
  },
  setBoatFlag(e){
    let index = e.currentTarget.dataset.index
    console.log(index);
    this.setData({
      boatFlag:index,
      pageNum:1,
      list:[],
      allow_load: true,
      loading: false, //"上拉加载"的变量，默认false，隐藏
    })
    this.getList()
  },
  getList(){
    let that = this
    if (that.data.allow_load) {
      that.setData({
        loading: true,
        content: '小主,我在玩命加载中...'
      })

      Http.get({
        url:config.api.couponOrderList,
        data:{
          pageNum:that.data.pageNum,
          pageSize: 10,
          couponOrderStatus:that.data.boatFlag=="all"?"":that.data.boatFlag,
          
        }
      }).then(res=>{
        if(that.data.pageNum==1){
          that.setData({
            list: []
          })
        }
        
        let tempArr= that.data.list
        if(tempArr.length>0){
          tempArr.map(item=>{
            item.createDate = util.formatTime(item.createDate, "yyyy-MM-dd hh:mm:ss");
          })
        }
        
        if(that.data.pageNum>=res.data.pages){
          that.setData({
            allow_load: false
          })
        }
        
        if(that.data.pageNum>1){//分页
          that.setData({
            list: res.data.list?[...tempArr ,...res.data.list]:tempArr,
            loading:false
          })
        }else{
          let tempLsit = res.data.list?res.data.list:[]
          if(tempLsit.length>0){
            tempLsit.map(item=>{
              item.createDate = util.formatTime(item.createDate, "yyyy-MM-dd hh:mm:ss");
            })
          }
          that.setData({
            list: tempLsit,
            loading:false
          })
        }
      }).catch(err => {
        tt.showToast({
          title: err.message ? err.message : err.data, // 内容
          icon: "none"
        });
      })
    }else {
      that.setData({
        loading: true,
        content: "——— 再拉裤子就掉了啦 ———"
      })
      setTimeout(function () {
        that.setData({
          loading: false,
        })
      }, 1400)
    }
    
  },
  onLoad: function (options) {

  },
  onReachBottom(){//触底
    console.log(111);
    this.setData({
      pageNum:this.data.pageNum+1
    })
    this.getList()
   },
   onShow:function(){
     debugger
    
    this.getList()
   
    }
   
})