import {
  get,
} from '../../utils/http';
import {
  formatActivity,
} from '../../utils/time';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    page: 1,
    total: 0,
    coupon: null,
    homeData: null,
    visible: false,
    posterPopup: false,
    isLoading: true,
    btmToast: true,
    activity: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      visible: getApp().globalData.couponPopup,
      coupon: getApp().globalData.coupon
    })
    this.getHomeData()
    if(wx.getStorageSync('day') != new Date().getDate()) {
      this.setData({
        posterPopup: true
      })
    }
    wx.setStorageSync('day', new Date().getDate())
  },

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

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

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

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    if(this.timer_) {
      clearInterval(this.timer_);
    }
  },

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

  },

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

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

  },

  /**
   * 关闭弹框
  */
  onClose(e,_bool) {
    let key = _bool ? e : e.currentTarget.dataset.key
    this.setData({
      [key]: false,
    })
    getApp().globalData.couponPopup = false;
  },

  /**
   * 确认领取
  */
  onConfirm() {
    wx.showToast({
      title: '领取成功',
      icon: 'none',
    })
    getApp().globalData.couponPopup = false;
    this.onClose("visible",true)
    // this.setData({ visible: false })
  },

  /**
   * 首页数据
  */
  getHomeData() {
    get('api/home',{},(res) => {
      this.setData({
        homeData: res.data
      },() => {
        this.getProjectList(1)
      })
    })
  },

  /**
   * 首页列表
  */
  getProjectList(_page) {
    let { list,page } = this.data;
    get('api/project',{
      store_id: this.data.homeData.store.id,
      page: _page || page,
      limit: 10
    },(res) => {
      if(_page == 1 || page == 1) {
        list = []
        this.data.page = 1
      }
      list.push(...res.data.list)
      this.setData({
        list,
        isLoading: false,
        total: res.data.total
      })
    })
  },

  /**
   * 店铺信息
   * /api/store/info
  */
  getStoreInfo(store_id) {
    get('api/store/info',{
      store_id
    },(res) => {
      this.setData({
        ['homeData.store']: res.data,
      },() => {
        this.getProjectList(1)
      })
      console.log(res)
    })
  },

  /**
   * 首页广告跳转
  */
  goToUrl(e) {
    if(e.currentTarget.dataset.url) {
      wx.navigateTo({
        url: e.currentTarget.dataset.url,
      })
    }
    this.setData({
      posterPopup: false
    })
  },

  /**
   * 跳转店铺列表
  */ 
  goToStore() {
    wx.navigateTo({
      url: '/pages/store/store?defaultStore='+ JSON.stringify(this.data.homeData.store),
    })
  },

  /**
   * 跳转项目详情
  */
  goToProject(e) {
    let item = e.currentTarget.dataset.item || {}
    wx.navigateTo({
      url: `/pages/projectDetail/projectDetail?project_id=${item.id}&store_id=${this.data.homeData.store.id}`
    })
  },

  /**
   * 获取首次开卡优惠
   * api/user/activity
  */
  getUserActivity() {
    if(this.timer) {
      clearInterval(this.timer_);
    }
    get('api/user/activity',{},(res) => {
      if(res.data) {
        res.data.currentTime = formatActivity(res.data.expire_time_seconds)
        this.setData({
          activity: res.data
        })
        this.timer_ = setInterval(() => {
          if(res.data.expire_time_seconds <= 0) {
            clearInterval(this.timer_);
          }
          res.data.expire_time_seconds--
          res.data.currentTime = formatActivity(res.data.expire_time_seconds)
          this.setData({
            activity: res.data
          })
        }, 1000);
      }
    })
  },

  /**
   * 开卡优惠关闭
  */
  onCloseToast() {
    this.setData({
      btmToast: false
    })
  }
})