// packageF/storeManagement/storeCouponList/storeCouponList.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    language: '',
    selected: 0,
    selectedidx: '1',

    isPresenter: false,

    types: [],
    typesIndex: 0,

    couponListData: [],
    colors: ['#fbbd5c', '#ff8383', '#a291ff', '#f095ed', '#6dccf8', '#fb946c', '#cecece'],

    memberId:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.id){
      this.setData({
        memberId:options.id
      });
    }
    this._initData();
    this._getNetData(1, this.data.typesIndex);
  },

  _initData() {
    // this.setData({
    //   page: 1,
    //   total_page: 0,
    //   allLoaded: false,
    //   isLoadMore: true
    // });

  },

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

  },

  //切换优惠券状态列表
  switchItem(e) {
    let index = e.detail.index;
    if ((index + 1) == this.data.selectedidx) return;
    if (index == 0) {
      this.setData({
        selectedidx: '1'
      });
    } else if (index == 1) {
      this.setData({
        selectedidx: '2'
      });
    } else if (index == 2) {
      this.setData({
        selectedidx: '3'
      });
    }
    this._initData();
    this._getNetData(this.data.selectedidx, this.data.typesIndex);
  },
  setTypesIndex(evt) {
    let index = evt.currentTarget.dataset.index;
    if (index == this.data.typesIndex) return;
    this.setData({
      typesIndex: index
    });
    this._getNetData(this.data.selectedidx, index);
  },


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

  },

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

  },

  //发起http请求
  _getNetData(type, coupon_type) {
    this.setData({
      couponListData: [],
    });
    let that = this;

    let urlStr = app.getNetAddresss("plugin.shop-assistant.frontend.coupon.coupon-list");
    urlStr += "&uid="+this.data.memberId;
    urlStr += "&status_request=" + type;
    urlStr += "&coupon_type=" + coupon_type;

    app._getNetWork({
      url: urlStr,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          var myData = res.data;

          that.data.total_page = res.data.total ? res.data.total : 0;
          that.data.couponListDataSet = myData.data;

          if (res.data.set) {
            that.setData({
              isPresenter: res.data.set.transfer
            });
          }

          if (res.data.search_array) {
            that.setData({
              types: res.data.search_array || []
            });
            for (let i = 0; i < myData.list.length; i++) {
              if (myData.list[i].belongs_to_coupon.coupon_type_name == '门店商品') {
                myData.list[i].type = 6;
              } else if (myData.list[i].belongs_to_coupon.coupon_type_name == '平台商品') {
                myData.list[i].type = 1;
              } else if (myData.list[i].belongs_to_coupon.coupon_type_name == '兑换券') {
                myData.list[i].type = 2;
              } else if (myData.list[i].belongs_to_coupon.coupon_type_name == '分类商品') {
                myData.list[i].type = 3;
              } else if (myData.list[i].belongs_to_coupon.coupon_type_name == '指定商品') {
                myData.list[i].type = 4;
              } else if (myData.list[i].belongs_to_coupon.coupon_type_name == '酒店商品') {
                myData.list[i].type = 5;
              } else if (myData.list[i].belongs_to_coupon.coupon_type_name == '商品和门店') {
                myData.list[i].type = 8;
              }
            }
          }

          that.setData({
            couponListData:  myData.list,
          });

        } else {
          app.tips(res.msg);
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },

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

  },

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

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  },

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

  }
});