// pages/cardBag/cardBag.js
import {
  get,
  post
} from '../../../utils/http.js'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    current: 1,
    list: [],
    page: 1,
    limit: 50,
    total: 0,
    storeId: wx.getStorageSync('store_id'),
    couponType: ['抵扣券', '满减券', '全额减免', '卡券'], //卡券类型 优惠券类型，1直接抵扣券，2满减券，3全额减免券，4卡券
    couponList: [], //优惠券列表
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      current:options.current
    })
    // 获取卡包列表
    this.getList()
    // 获取优惠券列表
    this.getCoupon()
  },

  /**
   * 切换状态
   */
  selectTab(e) {
    let current = e.currentTarget.dataset.current
    this.setData({
      current,
      page: 1,
      total: 0,
      couponList: [],
      list: [],
    })
    if (current == 1) {
      this.getList(1)
    } else {
      this.getCoupon(1)
    }
  },
  // 获取卡包列表
  getList(_page) {
    let {
      page,
      limit,
      current,
      list
    } = this.data
    get('v2/api/user/card/list', {
      page: _page || page,
      limit,
      type: 0
    }, (res) => {
      if (_page == 1 || page == 1) {
        list = []
        this.data.page = 1
      }
      list.push(...res.data.list)
      this.setData({
        list,
        total: res.data.total
      })
    })
  },
  /**
   * 获取优惠券列表
   * /api/user/coupon
   */
  getCoupon(_page) {
    let {
      couponList,
      page
    } = this.data
    get(
      'v2/api/user/coupon/list', {
        type: 'project',
        page: _page || page,
        limit: 10
      },
      (res) => {
        if (_page == 1 || page == 1) {
          couponList = []
          this.data.page = 1
        }
        couponList.push(...res.data.list)
        this.setData({
          couponList,
          total: res.data.total
        })
      }
    )
  },
  /**
   * 使用优惠券
   */
  goToOrderBy(e) {
    let coupon_range  = e.currentTarget.dataset.item.coupon_range
    let project_id = e.currentTarget.dataset.item.project_id
    if (coupon_range == 3) {
      wx.switchTab({
        url: "/pages/shoppingMall/shoppingMall"
      })
    }else if(coupon_range == 1){
      if(project_id==0){
        wx.switchTab({
          url: '/pages/orderBy/orderBy',
        })
      }else{
        wx.navigateTo({
          url: `/pages/projectDetail/projectDetail?source=project&id=${project_id}`,
        })
      }
    }else{
      wx.switchTab({
        url: '/pages/orderBy/orderBy',
      })
    }
    wx.setStorageSync('curre', 1)
  },
  // 下拉触底触发
  toLower() {
    if (this.data.list.length < this.data.total) {
      // 获取卡包列表
      this.getList(++this.data.page)
    }
  },

  // 跳转到次卡详情
  jumpCard(e) {
    let id = e.currentTarget.dataset.id
    let status = e.currentTarget.dataset.status
    if(status==-1){
      wx.showToast({
        title: '次卡已过期',
        icon:'none'
      })
      return
    }
    if(status==0){
      wx.showToast({
        title: '次卡已使用',
        icon:'none'
      })
      return
    }
    if(status==-2){
      wx.showToast({
        title: '次卡已注销',
        icon:'none'
      })
      return
    }
    wx.navigateTo({
      url: `/subPackages/pages/subCardDetails/subCardDetails?source=project&id=${id}&store_id=${this.data.storeId}`,
    })
  },

  // 跳转到项目详情
  jumpProject(e) {
    let project_id = e.currentTarget.dataset.project_id
    let id = e.currentTarget.dataset.id
    if (project_id == 0) {
      wx.navigateTo({
        url: `/subPackages/pages/subCardDetails/subCardDetails?source=project&id=${id}&store_id=${this.data.storeId}`,
      })
      return
    }
    wx.navigateTo({
      url: `/pages/projectDetail/projectDetail?source=project&id=${project_id}&store_id=${this.data.storeId}`,
    })
  },
  // 跳转到历史记录页面
  jumpHis() {
    if (this.data.current == 1) {
      wx.navigateTo({
        url: `/subPackages/pages/couponHistory/couponHistory`,
      })
    } else {
      wx.navigateTo({
        url: `/pages/couponHistory/couponHistory`,
      })
    }
  },
  // 跳转到更多
  goMoreCard() {
    app.globalData.isCardBag = true
    wx.redirectTo({
      url: '/pages/shoppingMall/shoppingMall',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

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

  },

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

  },

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

  },

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

  },

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

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

  }
})