// packageH/newDraw/luckyDraw/luckyDraw.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    activeName: 0,
    types: 'activity',
    dataList: [],
    titleArr: ["全部", "未开始", "活动中", "已结束"],
    isLoadMore: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.tag&&options.tag == "my") {
      this.setData({
        titleArr: ["全部", "进行中", "已中奖", "收藏", "评论"],
        types: 'my'
      });
    }
    this.setNewArr();
  },
  setNewArr() {
    let dataList = [];
    this.data.titleArr.forEach((val, index) => {
      dataList.push({
        isLoadMore: true,
        page: 1,
        total_page: 0,
        list: []
      });
    });
    this.setData({
      dataList
    });
    if (this.options.index) {
      this.setData({
        activeName: Number(this.options.index)
      });
    }
    this.handleClick();
  },
  handleClick(event) {
    if(event){
      this.data.activeName =event.detail.name;
    }
    let that = this;
    if (this.data.dataList[that.data.activeName].list.length > 0) {
      this.setData({
        isLoadMore: this.data.dataList[that.data.activeName].isLoadMore
      });
      return;
    }
    wx.showLoading({
      title: '加载中',
    });
    let _url = "";
    let _json = {};
    if (this.data.types == "activity") {
      _url = "plugin.luck-draw.frontend.index.index";
      _json = {
        page: 1,
        search: {
          state: this.data.activeName - 1
        }
      };
      if (this.options.gid) {
        _json.search.goods_id = this.options.gid;
      }
    } else {
      _url = "plugin.luck-draw.frontend.index.myLuck";
      _json = {
        page: 1,
        state: this.data.activeName
      };
    }

    let urlStr = app.getNetAddresss(_url);
    app._postNetWork({
      url: urlStr,
      data: _json,
      success: (resdata) => {
        wx.hideLoading();
        let res = resdata.data;
        if (res.result == 1) {
          that.setData({
            ['dataList[' + that.data.activeName + '].isLoadMore']: true,
            ['dataList[' + that.data.activeName + '].total_page']: res.data.last_page,
            ['dataList[' + that.data.activeName + '].list']: res.data.data,
          });
        } else {
          wx.showToast({
            title: res.msg,
            duration: 1000,
            icon: 'none'
          });
        }
      },
      fail: function (res) {
        wx.hideLoading();
        console.log(res.msg);
      }
    });
  },
  _getMoreData() {
    let that = this;
    this.setData({
      ['dataList[' + that.data.activeName + '].isLoadMore']: false
    });
    if (this.data.dataList[this.data.activeName].page >= this.data.dataList[this.data.activeName].total_page) {
      return;
    } else {
      if (this.data.types == "activity") {
        _url = "plugin.luck-draw.frontend.index.index";
        _json = {
          search: {
            state: this.data.activeName - 1
          }
        };
        if (this.options.gid) {
          _json.search.goods_id = this.options.gid;
        }
      } else {
        _url = "plugin.luck-draw.frontend.index.myLuck";
        _json = {
          state: this.data.activeName
        };
      }
      this.data.dataList[this.data.activeName].page = this.data.dataList[this.data.activeName].page + 1;
      let urlStr = app.getNetAddresss(_url);
      urlStr += "&page=" + this.data.dataList[this.data.activeName].page;
			
      app._getNetWork({
        url: urlStr,
        data:_json,
        success: function(resdata) {
          var res = resdata.data;
          if (res.result == 1) {
            let myData = res.data;
            that.setData({
              ['dataList[' + that.data.activeName + '].isLoadMore']: true,
              ['dataList[' + that.data.activeName + '].list']: that.data.dataList[that.data.activeName].list.concat(myData.data)
            });
          }else{
            that.setData({
              ['dataList[' + that.data.activeName + '].isLoadMore']: false,
              ['dataList[' + that.data.activeName + '].page']: that.data.dataList[that.data.activeName].page -1
            });
          }
        },
        fail: function(res) {
          console.log(res);
        }
      });
    }
  },
  gotoIndex(e){
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/packageH/newDraw/newDrawIndex/newDrawIndex?id=' + id
    });
  },
  gotoDetail(e){
    let id = e.currentTarget.dataset.order;
    wx.navigateTo({
      url: '/packageA/member/orderdetail/orderdetail?order_id=' + id + '&orderType=luck-draw'
    });
  },
  gotoMydraw(){
    wx.navigateTo({
      url: '/packageH/newDraw/mydraw/mydraw'
    });
  },
  confirmSelectAddress(){

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

  },

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

  },

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

  },

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

  },

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

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.isLoadMore) {
      this._getMoreData();
    } else {
      console.log('没有更多数据');
    }
  },

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

  }
});