// packageH/newRetail/newRetailSalesRecord/newRetailSalesRecord.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    activeName: 0,
    dataList: [
      {
        isLoadMore: true,
        page: 1,
        total_page: 0,
        list: []
      },
      {
        isLoadMore: true,
        page: 1,
        total_page: 0,
        list: []
      }
    ],
    titleArr: ["代理套餐", "线上零售"],
    isLoadMore: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getData();
  },
  getData(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;
    }
    let urlStr = app.getNetAddresss('plugin.new-retail.frontend.index.getSalesList');
    app._postNetWork({
      url: urlStr,
      data: {
        sales_type: Number(that.data.activeName) +1
      },
      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,
          });
          console.log(this.data.dataList);
        } 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 {
      this.data.dataList[this.data.activeName].page = this.data.dataList[this.data.activeName].page + 1;
      let urlStr = app.getNetAddresss('plugin.new-retail.frontend.index.getSalesList');
      urlStr += "&page=" + this.data.dataList[this.data.activeName].page;
			
      app._getNetWork({
        url: urlStr,
        data:{ sales_type: Number(that.data.activeName) +1 },
        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);
        }
      });
    }
  },
  goLook(e) {
    let state = e.currentTarget.dataset.state;
    let id = e.currentTarget.dataset.id;

    // state::1-未出售 2-出售中 3-已出售
    if (state == 1 || state == 2) {
      if (this.data.activeName == 0) {
        wx.navigateTo({url: '/packageH/newRetail/newRetail_agencyPackage/newRetail_agencyPackage?id=' + id});
      }
      if (this.data.activeName == 1) {
        wx.navigateTo({url: '/packageH/newRetail/newRetailRetailLink/newRetailRetailLink?id=' + id});
      }
    } else {
      wx.navigateTo({url: '/packageH/newRetail/newRetailSalesRecordDetail/newRetailSalesRecordDetail?id=' + id + '&mode=' + this.data.activeName});
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
});