var e = getApp(),os = e.globalData.setting;
var utils = require('../../../../utils/util.js');
var regeneratorRuntime = require('../../../../utils/runtime.js');

Page({
  /*** 页面的初始数据*/
  data: {
    color_screen: "#8e8c8d",
    val:0,
    distance:0,
    // 时间组件参数
    isPickerRender: false,
    isPickerShow: false,
    startTime: "",
    endTime: "",

    pickerConfig: {
      endDate: true,
      column: "",
      dateLimit: true,
      initStartTime: "2019-01-01 12:32:44",
      initEndTime: "2019-12-01 12:32:44",
      limitStartTime: "1917-05-06 12:32:44",
      limitEndTime: "2055-05-06 12:32:44"
    },
    cz_val:0,          //成长值
    gradeId:null,      //等级ID
    gradeEndDate:null, //等级结束时间
    need_money:0,      //到下一级需要多少

    value_list:null,   //明细列表
    iurl:os.imghost,
    cur_page:1,
    is_no_more:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
     var  need_money=options.need_money;
     if(need_money>0)
        this.setData({need_money:need_money})
  },
  /*** 生命周期函数--监听页面显示*/
  onShow: async function () {

    var gl_data=getApp().globalData,th=this;
    var cz_val=0,gradeId=null,gradeEndDate=null,need_money=0,val_list=null,backClass=null;

    //获取成长值
    await  getApp().request.promiseGet("/api/weshop/users/grade/aftervipinfo/get",{
      data:{ storeId:os.stoid,userId:gl_data.user_id }
    }).then(res=>{
       var data=res.data.data;
       cz_val=data.GradeSum;
       gradeId=data.GradeId;
       gradeEndDate=data.gradeEndDate;
    })

    //---如果未传值需要多少钱过来的话---
    if(!th.data.need_money) {
      //获取卡类列表,判断到下一级需要多少
      await getApp().request.promiseGet("/api/weshop/users/grade/vipgradeinfo/page", {
        data: {storeId: os.stoid}
      }).then(res => {
        backClass = res.data.data.pageData;
      })
      for (var i in backClass) {
        var im = backClass[i];
        if (cz_val < im.BuyGradeSum) {
           need_money = im.BuyGradeSum; break;
        }
      }
    }

    //--获取明细列表--
    await getApp().request.promiseGet("/api/weshop/users/grade/vipinoutgrade/page", {
      data: {storeId: os.stoid,userId:gl_data.user_id,page:1,pageSize:6}
    }).then(res => {
       if(res.data.data)
          val_list= res.data.data.pageData;
    })
    
    th.setData({cz_val:cz_val,gradeId:gradeId,gradeEndDate:gradeEndDate,need_money:need_money,val_list:val_list})

  },

  /*** 页面上拉触底事件的处理函数 */
  onReachBottom: async function () {
    if(this.data.is_no_more) return;
    var val_list=null,page=++this.data.cur_page,
        c_val_list=this.data.val_list,th=this,gl_data=getApp().globalData;

    var d_data={storeId: os.stoid,userId:gl_data.user_id,page:page,pageSize:6};
    if(th.data.startTime && th.data.endTime ){
       d_data.beginDate=th.data.startTime;
       d_data.endDate=th.data.endTime;
    }
    //--获取明细列表--
    await getApp().request.promiseGet("/api/weshop/users/grade/vipinoutgrade/page", {
      data: d_data,
    }).then(res => {
      if(res.data.data)
         val_list= res.data.data.pageData;
    })
    if(val_list && val_list.length>0 ){
         var dd=[...c_val_list,...val_list];
         th.setData({val_list:dd})
    }else{
       th.data.is_no_more==1;
       getApp().my_warnning("没有更多数据",0,th);
    }
  },

  pickerShow: function () {
    this.setData({
      isPickerShow: true,
      isPickerRender: true,
      chartHide: true,
      color_screen: "#009cfe",
      val:180,
      distance:15,
      is_show:1,
    });
  },
  pickerHide: function() {
    console.log("555");
    this.setData({
      isPickerShow: false,
      chartHide: false,
      color_screen: "#8e8c8d",
      val: 0,
      distance: 0,
    });
  },
  bindPickerChange: function (e) {
    this.getData(this.data.sensorList[e.detail.value].id);
    // let startDate = util.formatTime(new Date(new Date().getTime() - 24 * 60 * 60 * 1000 * 7));
    // let endDate = util.formatTime(new Date());
    this.setData({
      index: e.detail.value,
      sensorId: this.data.sensorList[e.detail.value].id
      // startDate,
      // endDate
    });
  },
  //--选择时间--
  setPickerTime: function (val) {
    let data = val.detail,gl_data=getApp().globalData,th=this;
    this.setData({
      startTime: data.startTime,
      endTime: data.endTime,
      color_screen: "#8e8c8d",
      val: 0,
      distance: 0,
    });

    this.data.cur_page=1;
    var d_data={storeId: os.stoid,userId:gl_data.user_id,page:1,pageSize:6};
    d_data.beginDate=th.data.startTime;
    d_data.endDate=th.data.endTime;

    console.log(d_data);

    //--获取明细列表--
    getApp().request.promiseGet("/api/weshop/users/grade/vipinoutgrade/page", {
      data: d_data,isShowLoading:1
    }).then(res => {
          var val_list=null;
          if(res.data.data) val_list=res.data.data.pageData;
          if(val_list && val_list.length>0 ) {
             this.setData({val_list:val_list});
          }else{
            this.setData({val_list:null});
          }
    })

  },

  goto:function(e){
     var index=e.currentTarget.dataset.ind;
     switch (index) {
       case "0": getApp().goto("/pages/index/index/index");break;
       case "1": getApp().goto("/packageD/pages/user/coupons/coupons"); break;
       case "2": getApp().goto("/packageG/pages/user/userqy/userqy"); break;
     }
  },

  reset_data:function(){
    this.data.cur_page = 1;
    this.setData({
      startTime: null,
      endTime: null,});

    var gl_data = getApp().globalData;
    var d_data = { storeId: os.stoid, userId: gl_data.user_id, page: 1, pageSize: 6 };
    
    //--获取明细列表--
    getApp().request.promiseGet("/api/weshop/users/grade/vipinoutgrade/page", {
      data: d_data, isShowLoading: 1
    }).then(res => {
      var val_list = null;
      if (res.data.data) val_list = res.data.data.pageData;
      if (val_list && val_list.length > 0) {
        this.setData({ val_list: val_list });
      } else {
        this.setData({ val_list: null });
      }
    })
  }

})