// packageC/o2o/o2oGrouplist/o2oGrouplist.js
var app = getApp();
var location = require("../../../mybehaviors/location/location");

Page({
  behaviors: [location],

  /**
   * 页面的初始数据
   */
  data: {
    showLocation: false,
    activeName: 0,
    isLoading: false,
    dataList: [{
      isLoadMore: true,
      page: 1,
      total_page: 0,
      list: []
    },
    {
      isLoadMore: true,
      page: 1,
      total_page: 0,
      list: []
    }
    ],
    isLoadMore: true,
    titleArr: ["离我最近", "好评优先"],

    lat: 0,
    lng: 0,
    city: '',
    point: {
      lat: '',
      lng: ''
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.city && options.city !== '1') {
      if (options.city) {
        this.setData({
          city: options.city
        });
      }
      if (options.point) {
        this.setData({
          point: JSON.parse(options.point)
        });
      }
      this.handleClick();
    } else {
      this._getLocation(() => {this.handleClick();});
    }
  },

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

  },
  tabClick(e) {
    this.setData({
      activeName: e.detail.index
    });
    this.handleClick();
  },
  handleClick() {
    let that = this;
    if (this.data.dataList[that.data.activeName].list.length > 0) {
      that.data.isLoadMore = this.data.dataList[that.data.activeName].isLoadMore;
      return;
    }

    let urlStr = app.getNetAddresss("plugin.fight-groups.frontend.store.store-fight-arrondi.get-fight-groups");
    app._getNetWork({
      url: urlStr,
      data: {
        page: 1,
        type_id: that.data.activeName + 1,
        lng: that.data.point.lng,
        lat: that.data.point.lat
      },
      success: function (resdata) {
        var 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,
            icon: 'none',
            duration: 1000
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  goGroup(e) {
    let id = e.currentTarget.dataset.id;
    let storeid = e.currentTarget.dataset.storeid;
    wx.navigateTo({
      url: "/packageA/detail_v2/detail_v2?id=" + id + '/' + storeid + '&name=group'
    });
  },
  gotoO2oHome(e){
    let storeid = e.currentTarget.dataset.storeid;
    wx.navigateTo({
      url: '/packageC/o2o/HomeSeller/HomeSeller?store_id=' + storeid
    });
  },


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

  },

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

  },

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

  },

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

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.dataList[this.data.activeName].isLoadMore) {
      this._getMoreData();
    } else {
      console.log('没有更多数据');
    }
  },
  _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.fight-groups.frontend.store.store-fight-arrondi.get-fight-groups');
      urlStr += "&page=" + this.data.dataList[this.data.activeName].page;

      app._getNetWork({
        url: urlStr,
        data: {
          page: that.data.page,
          type_id: that.data.activeName + 1,
          lng: that.data.lng,
          lat: that.data.lat
        },
        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);
        }
      });
    }
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
});