var app = getApp();

Page({
  data: {
    language: '',
    // goodsData:[],
    goodsData: [],
    dataState: 0,
    showEdit: false,
    // 是否显示面板指示点
    indicatorDots: true,
    // 方向
    vertical: false,
    // 是否自动切换 
    autoplay: true,
    // 自动切换时间间隔
    interval: 4000,
    // 滑动动画时长
    duration: 300,

    integral: "积分",

    page: 1,
    finisend: false,
  },


  // 单条删除
  delData: function(e) {
    var that = this;
    var url = app.getNetAddresss("member.member-history.destroy");
    console.log(e.currentTarget.dataset.id);
    url += "&id=" + e.currentTarget.dataset.id;
    wx.showModal({
      title: '提示',
      content: '确定要删除足迹么？此操作不可逆！',
      success: function(res) {
        if (res.confirm) {
          console.log('用户点击确定');
          app._getNetWork({
            url: url,
            success: function(res) {
              var _data = res.data;
              console.log("_data", _data);
              if (_data.result == 1) {
                for(let i = 0;i<that.data.goodsData.length;i++){
                  console.log(that.data.goodsData[i]);
                  if(that.data.goodsData[i].id == e.currentTarget.dataset.id){
                    that.data.goodsData.splice(i,1);
                  }
                }
                that.setData({
                  goodsData:that.data.goodsData
                });
                // that._getData();
                wx.showToast({
                  title: _data.msg,
                  icon: 'success',
                  duration: 2000,
                  success: function() {},
                  fail: function() {}
                });
              } else {
                wx.showToast({
                  title: '数据错误',
                  icon: 'success',
                  duration: 2000,
                  success: function() {},
                  fail: function() {}
                });
              }

            },
            fail: function(res) {}
          });
        }
      }
    });
  },
  goDetail: function(e) {
    let itemdata = e.currentTarget.dataset.itemdata;
    if (itemdata.owner_id != 0) {
      //goodsO2O
    } else {
      wx.navigateTo({
        url: '/packageA/detail_v2/detail_v2?id=' + itemdata.goods.id
      });
    }
  },
  // 获取数据
  _getData: function() {
    if (this.data.finisend) {
      return;
    }
    var that = this;
    let urlStr = app.getNetAddresss("member.member-history.index");
    if(app.globalData.store_alone_temp == 1) {
      // 开启门店独立模块
      urlStr = app.getNetAddresss("plugin.store-alone-temp.frontend.member.history-list");
    }
    app._getNetWork({
      url: urlStr,
      data: {
        page: this.data.page
      },
      showToastIn: false,
      success: function(res) {
        var _data = res.data;
        if (_data.result == 1) {
          let finisend = false;
          let goodsData = [];
          console.log(_data);
          if (that.data.page >= _data.data.last_page || _data.data.member_histories.legnth < _data.data.per_page) {
            finisend = true;
          } else {
            finisend = false;
          }
          if (that.data.page > 1) {
            goodsData = that.data.goodsData.concat(_data.data.member_histories);
          } else {
            goodsData = _data.data.member_histories;
          }
          that.setData({
            goodsData,
            finisend,
            page: (++that.data.page)
          });
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          });
        }
      },
      fail: function(res) {}
    });
  },
  onReady: function() {
    let language = wx.getStorageSync('langIndex');
    this.setData({ 'language': language.en});

    let value = wx.getStorageSync("integral");
    if (value) {
      this.setData({
        integral: value,
      });
    }
    // 页面渲染完成
  },
  onShow: function() {
    this.setData({
      page: 1,
      finisend: false
    });
    this._getData();
    // 页面显示
  },
  onHide: function() {
    // 页面隐藏
  },
  onUnload: function() {
    // 页面关闭
  },

  onReachBottom () {
    this._getData();
  }
});
