// packageB/member/distribution_queue/index/index.js
var app = getApp();
Page({

  /**
	 * 页面的初始数据
	 */
  data: {
    Height: 0,
    LastHeight: 0,
    rotation: [], //轮播图
    activity_list: [],
    advert_one: {},
    advert_two: {},
    advert_three: {},
    advert_four: {},
    name: '',
    //more
    isLoadMore: true,
    page: 1,
    total_page: 0,
    is_rotation:false
  },

  /**
	 * 生命周期函数--监听页面加载
	 */
  onLoad: function(options) {
    this.getData();
  },
  imgHeight: function(e) {
    var winWid = wx.getSystemInfoSync().windowWidth; //获取当前屏幕的宽度
    var imgh = e.detail.height; //图片高度
    var imgw = e.detail.width; //图片宽度
    var swiperH = winWid * imgh / imgw + "px"; //等比设置swiper的高度。 即 屏幕宽度 / swiper高度 = 图片宽度 / 图片高度  ==》swiper高度 = 屏幕宽度 * 图片高度 / 图片宽度
    if (parseInt(swiperH) > this.data.LastHeight) {
      this.setData({
        Height: swiperH, //设置高度
      });
      this.data.LastHeight = parseInt(swiperH);
    }
  },
  getData() {
    let that = this;
    this.data.isLoadMore = false;
    let urlStr = app.getNetAddresss("plugin.commission-activity.api.index.index");
    app._postNetWork({
      url: urlStr,
      showToastIn: false,
      data: {
        page: this.data.page
      },
      success: function(resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          // let list = res.data;
          let activity = res.data.activity;
          that.setData({
            rotation: res.data.rotation,
            activity_list: activity.data,
            page: activity.current_page,
            total_page: activity.last_pag,
            is_rotation:(res.data.is_rotation&&res.data.is_rotation==1)?true:false
          });
          if(res.data.adv){
            let adv = res.data.adv;
            that.setData({
              advert_one: adv.mini_advert_one,
              advert_two: adv.mini_advert_two,
              advert_three: adv.mini_advert_three,
              advert_four: adv.mini_advert_four
            });
          }
          if (res.data.name) {
            wx.setNavigationBarTitle({
              title: res.data.name
            });
          }
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function(res) {
        console.log(res.msg);
      }
    });
  },
  gotoDiy(e) {
    let url = e.currentTarget.dataset.url;
    if (url) {
      try {
        wx.navigateTo({
          url: url
        });
      } catch (e) {
        wx.showToast({
          title: '链接设置错误:' + url,
          icon: 'none',
          duration: 2000
        });
        //TODO handle the exception
      }
    } else {
      wx.showToast({
        title: '链接未设置',
        icon: 'none',
        duration: 2000
      });
    }
  },
  /**
	 * 生命周期函数--监听页面初次渲染完成
	 */
  onReady: function() {

  },

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

  },

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

  },
  gotoDetail(e) {
    let index = e.currentTarget.dataset.index;
    let id = this.data.activity_list[index].id;
    wx.navigateTo({
      url: '/packageB/member/distribution_queue/DistributionActivityDetail/DistributionActivityDetail?id=' + id
    });
  },
  tosearch() {
    wx.navigateTo({
      url: '/packageB/member/distribution_queue/DistributionSearch/DistributionSearch'
    });
  },
  /**
	 * 生命周期函数--监听页面卸载
	 */
  onUnload: function() {

  },

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

  },
  _getMoreData() {
    const that = this;
    let json = {
      page: this.data.page
    };
    let api = 'plugin.commission-activity.api.index.index';
    let urlStr = '';
    that.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      this.data.page = this.data.page + 1;
      urlStr = app.getNetAddresss(api);
      app._postNetWork({
        url: urlStr,
        showToastIn: false,
        data: json,
        success: function(resdata) {
          var res = resdata.data;
          if (res.result == 1) {
            that.setData({
              isLoadMore: true,
              activity_list: that.data.activity_list.concat(res.data.activity.data)
            });
          } else {
            that.setData({
              page: that.data.page - 1,
              isLoadMore: false
            });
            return;
          }
        },
        fail: function(res) {
          console.log(res.msg);
        }
      });
    }
  },
  /**
	 * 页面上拉触底事件的处理函数
	 */
  onReachBottom: function() {
    if (this.data.isLoadMore) {
      this._getMoreData();
    } else {
      console.log('没有更多数据');
    }
  },

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

  }
});
