import {
  get,
  post
} from '../../utils/http'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    current: 0,
    list: [],
    list2: [],
    total: 0,
    page: 1,
    source: 'project',
    couponType: ['抵扣券', '满减券', '全额减免', '卡券'], //卡券类型
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getCoupon()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

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

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

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

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.page * 10 < this.data.total) {
      this.getCoupon(++this.data.page)
    }
  },

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

  /**
   * 切换状态
   */
  onTabsChange(e) {
    let current = e.currentTarget.dataset.id
    if (current == this.data.current) {
      return
    }
    this.setData({
        current,
        page: 1
      },
      () => {
        this.getCoupon()
      }
    )
  },

  /**
   * 切换详细详细
   */
  onChangeDesc(e) {
    let {
      item,
      index
    } = e.currentTarget.dataset
    let query = wx.createSelectorQuery()
    query
      .selectAll('#item' + index)
      .boundingClientRect((rect) => {
        item.childrenHeight = item._status ? 0 : rect[0].height
        item._status = !item._status
        this.setData({
          ['list[' + index + ']']: item
        })
      })
      .exec()
  },

  /**
   * 使用优惠券
   */
  goToHome(data) {
    wx.switchTab({
      url: '/pages/home/home'
    })
  },

  /**
   * 使用优惠券
   */
  goToOrderBy(e) {
    // console.log(e, 'item', e.currentTarget.dataset.item.project_id);
    let project_id = e.currentTarget.dataset.item.project_id
    if (project_id) {
      wx.navigateTo({
        url: `/pages/projectDetail/projectDetail?source=${this.data.source}&id=${project_id}`,
      })
      return
    }
    wx.setStorageSync('curre', 1)
    wx.switchTab({
      url: "/pages/orderBy/orderBy"
    })
  },

  /**
   * 获取优惠券列表
   * /api/user/coupon
   */
  getCoupon(_page) {
    let {
      list,
      current,
      page
    } = this.data
    get(
      'api/user/coupon', {
        type: current,
        page: _page || page,
        limit: 10
      },
      (res) => {
        if (_page == 1 || page == 1) {
          list = []
          this.data.page = 1
        }
        res.data.list.forEach((item) => {
          item.ex_time = item.ex_time.replace(/-/g, '.')
          item._status = false
          item.childrenHeight = 0
           // 计算优惠券到期时间
           let time = item.ex_time.split('至')
           let now = new Date()
           let nowTime = `${now.getFullYear()}/${now.getMonth()+1}/${now.getDate()}`
          //  ios上不识别xx-xx-xx newDate
           time[1] = time[1].replace(/\./g, '/')
           item.fina_time=time[1]
           item.finaTime=this.DateDiff(nowTime,time[1])
        })
        list.push(...res.data.list)
        // if(list.length > 0 && list[0].coupon_desc) {
        //   list[0]._status = true
        // }
        this.setData({
          list,
          total: res.data.total
        })
      }
    )
  },
   // 优惠券到期时间计算
   DateDiff(sDate1, sDate2) { 
    var aDate, oDate1, oDate2, iDays
    aDate = sDate1.split("/")
    oDate1 = new Date(aDate[1] + '/' + aDate[2] + '/' + aDate[0]) //转换为12/18/2006格式  
    aDate = sDate2.split("/")
    oDate2 = new Date(aDate[1] + '/' + aDate[2] + '/' + aDate[0])
    iDays = parseInt(Math.abs(oDate1 - oDate2) / 1000 / 60 / 60 / 24) //把相差的毫秒数转换为天数  
    return iDays
  },
  // 点击跳转到历史记录
  goHistory() {
    wx.navigateTo({
      url: '/pages/couponHistory/couponHistory',
    })
  }
})