// pages/couponRecord/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'),
    time: new Date(),
    list: [],
    showNocontent: false,
    todayDate: null,
    pageNum: 1,
    length: 0,
    userInfo: null,
    listData: [],
    loading: false,
    content: '正在加载中',
    content1: '正在加载中',
    couList:[],
    dataH:[],
    pages:null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getUser();
    this.setData({
      pageNumH: 2,
      flag: "coupons",
      loading:false
    });
  },
  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.getMerchentCoupon(1);
      console.log(res);
    })
  },
  formatData(data) {
    let tempAtt = data
    let arr = []; 
    tempAtt.map(item => {
      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;
        }
      })
      // console.log((new Date(item.createDate)).valueOf())
      // console.log((new Date((this.data.date + ' 00:00:00'))).getTime())
      // console.log((new Date((this.data.date2 + ' 23:59:59').replace(/-/g, "/"))))
      // console.log(this.data.date, this.data.date2)                   
      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
    })
    console.log(arr,888888888888)
  },
  getList(page) {
    let _this = this;
    Http.get({
      url: config.api.wxMerchantcouponStory,
      data: {
        token: app.globalData.token,
        pageNum: page,
        pageSize: 10,
        channelType:7,
        beginDate: this.data.date ,
        endDate: this.data.date2
      }
    })
      .then(res => {
        wx.stopPullDownRefresh();
        let data = res.data.list;
        let listData = [];
        if (page == 1) {
          listData = res.data.list;
        } else {
          listData = _this.data.listData;
        }
        data.map(item => {
          let haveIf = false;
          item.createDate01 = util.formatTime(item.createDate, 'yyyy-MM-dddd hh:mm:ss')
          listData.map((item02, index02) => {
            if (item02.actionId == item.actionId) {
              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 => {
        wx.stopPullDownRefresh();
        wx.showToast({
          title: err.message,
          icon: 'none',
          duration: 2000,
          mask: false
        });
      });
  },
  //赠券库存
  getMerchentCoupon(pageNumH) {
    let that = this;
    let postData = {
      token: app.globalData.token,
      pageNum: pageNumH,
      pageSize: 10,
      sendType: 7,
      expired:'',
      status:'',
    }
    Http.get({
      url: config.api.wxMerchantcouponSend,
      data: postData,
    })
      .then(res => {
        res.data.list.map((item, index) => {
          // item = Object.assign(item, { yxStauts: true })
          // if (item.validDays) {
          //   item = Object.assign(item, { yxStauts: false })
          // }
          // if (item.validEndDate < Date.now()) {
          //   item = Object.assign(item, { yxStauts: true })
          // } else {
          //   item = Object.assign(item, { yxStauts: false })
          // }
          item.validEndDate = util.formatTime(item.validEndDate, 'yyyy-MM-dddd')
          item.validStartDate = util.formatTime(item.validStartDate, 'yyyy-MM-dddd')
        });
        if (pageNumH == 1) {
          console.log("我进来了！")
          var tmpArr = [];
        } else {
          console.log("我赋值了")
          var tmpArr = that.data.couList;
        }
        tmpArr.push.apply(tmpArr, res.data.list);
        that.setData({
          couList: tmpArr,
        })
        if (tmpArr.length >= res.data.total) {
          that.setData({
            content1: '没有更多数据'
          })
          return false;
        } else {
          that.setData({
            loading: false,
            content1: '小主,我在玩命加载中...'
          })
        }
        
      })
      .catch(err => {
        wx.showToast({
          title: err.message,
          icon: 'none',
          duration: 2000,
          mask: false
        });
      });
  },
  //切换库存
  getCoupons() {
    this.setData({
      flag: "coupons",
      pageNumH: 1,
      loading:false
    })
    this.getMerchentCoupon(1, "coupons"),
    wx.setNavigationBarTitle({
      title: '赠券库存'
    })
  },
  //切换记录
  getRecord() {
    this.setData({
      flag: "record",
      page: 1,
      loading:false
    })
    this.getList(1, "record"),
    wx.setNavigationBarTitle({
      title: '注券记录'
    })
  },
 
  /**
  * 刷新
  */
  onPullDownRefresh: function (e) {
    let that = this;
    that.setData({
      pageNum: 1,
    });
    that.getList(1);
    that.getMerchentCoupon(1);
  },
  onReachBottom: function () {
    let that = this;
    that.data.pageNum++;
    that.setData({
      pageNum: that.data.pageNum,
      loading: true
    });
    that.getList(that.data.pageNum);
    let pageNumH = that.data.pageNumH++;
    that.getMerchentCoupon(pageNumH);
  }
})