// pages/creditOperate/index.js
const config = require('../../config/config.js')
const Http = require('../../utils/HttpBasics.js')
const util = require('../../utils/util.js')
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    date: util.formatTime(new Date(), 'yyyy-MM-dddd'), //默认起始时间  
    date2: util.formatTime(new Date(), 'yyyy-MM-dddd'), //默认结束时间 
    endDate: util.formatTime(new Date(), 'yyyy-MM-dddd'),
    list: [],
    showNocontent: false,
    todayDate: null,
    pageNum: 1,
    length: 0,
    userInfo:null,
    listData:[],
    loading:false,
    content:'正在加载中',
    pages:null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getUser()
  },
  bindDateChange(e) {
    let that = this;
    that.setData({
      date: e.detail.value,
    })
    console.log(that.data.date)
  },
  bindDateChange2(e) {
    let that = this;
    that.setData({
      date2: e.detail.value,
    })
    console.log(that.data.date2)
  },
  search(){
    this.getList(this.data.pageNum)
  },
  getUser(){
    let _this=this;
    app.globalData.userInfo().then(res => {
      this.setData({
        userInfo: res
      })
      _this.getList(1);
      console.log(res);
    })
  },
  formatData(data){
    let arr=[];
    data.map((item,index)=>{
      let a = {};
      a.value = [item];
      a.dateR = item.createDate01;
      let indexSt=0;
      let haveIf=false;
      arr.map((item01,index01)=>{
        if (item.createDate01 == item01.dateR){
          indexSt = index01;
          haveIf=true;
        }
      })                                                     
      if (haveIf && (new Date(item.createDate)).valueOf() >= (new Date((this.data.date + ' 00:00:00').replace(/-/g, "/"))).getTime() && (new Date(item.createDate)).valueOf() <= (new Date((this.data.date2 + ' 23:59:59').replace(/-/g, "/"))).getTime()){
        arr[indexSt].value.push(item)
      } else if ((new Date(item.createDate)).valueOf() >= (new Date((this.data.date + ' 00:00:00').replace(/-/g, "/"))).getTime() && (new Date(item.createDate)).valueOf() <= (new Date((this.data.date2 + ' 23:59:59').replace(/-/g, "/"))).getTime()){
        arr.push(a)
      }
    })
    this.setData({
      list:arr
    })
  },
  getList(page){
    let _this=this;
    Http.get({
      url: config.api.getCreditList,
      data: {
        pageNum: page,
        pageSize: 10,
        startTime: this.data.date + " 00:00:00",
        endTime: this.data.date2 + " 23:59:59",
        merchantId: _this.data.userInfo.merchant_id
      }
    })
    .then(res => {
      tt.stopPullDownRefresh();
      let data=res.data.list;
      let listData = [];
      if(page==1){
        listData = res.data.list;
      }else{
        listData = _this.data.listData;
      }
      console.log(data)
      data.map((item, index) => {
        let haveIf = false;
        item.createDate01 = util.formatTime(item.createDate, 'yyyy-MM-dddd')
        listData.map((item02, index02) => {
          if (item02.creditId == item.creditId) {
            haveIf = true;
          }
        })
        if (!haveIf) {
          listData.push(item)
        }
      })
      _this.formatData(listData)
      
      if (res.data.pages <= page) {
        _this.setData({
          listData,
          pageNum: res.data.pages-1,
          content: '已经加载全部数据！'
        })
      } else {
        _this.setData({
          listData,
          loading: false,
          content:'小主,我在玩命加载中...'
        })
      }
    })
    .catch(err => {
      tt.stopPullDownRefresh();
      tt.showToast({
        title: err.message,
        icon: 'none',
        duration: 2000,
        mask: false
      });
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

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

  },

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

  },

  /**
  * 刷新
  */
  onPullDownRefresh: function (e) {
    let that = this;
    that.setData({
      pageNum: 1,
      list: []
    });
    that.getList(1);
  },
  onReachBottom: function () {
    var that = this;
    that.data.pageNum++;
    that.setData({
      pageNum: that.data.pageNum,
      loading: true
    });
    that.getList(that.data.pageNum);
  },

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

  }
})