import {
  fetchCouponList
} from '../../../services/coupon/index';

Page({
  data: {
    status: 1,
    list: [{
        text: '可使用',
        key: 1,
      },
      {
        text: '已使用',
        key: 2,
      },
      {
        text: '已失效',
        key: 3,
      },
    ],

    couponList: [],
  },

  onLoad() {
    this.init();
  },

  init() {
    this.fetchList();
  },

  fetchList(status = this.data.status) {
    let statusInFetch = '';
    switch (Number(status)) {
      case 1: {
        statusInFetch = 'default';
        break;
      }
      case 2: {
        statusInFetch = 'useless';
        break;
      }
      case 3: {
        statusInFetch = 'disabled';
        break;
      }
      default: {
        throw new Error(`unknown fetchStatus: ${statusInFetch}`);
      }
    }
    // 优惠券接口
    fetchCouponList({
      status: this.data.status
    }).then((couponList) => {
      this.setData({
        couponList
      });
    });
  },

  tabChange(e) {
    const {
      value
    } = e.detail;

    this.setData({
      status: value
    });
    this.fetchList(value);
  },

  goCouponCenterHandle() {
    wx.showToast({
      title: '去领券中心',
      icon: 'none'
    });
  },

  onPullDownRefresh_() {
    this.setData({
        couponList: [],
      },
      () => {
        this.fetchList();
      },
    );
  },
});