// packageE/grab_group/grab_group_home/grab_group_home.js
var app = getApp();

Page({
  /**
   * 页面的初始数据
   */
  data: {
    language: "",
    Height: 0,
    LastHeight: 0,
    show: false,
    activeName: 0,
    titleArr: [
      {
        id: 0,
        name: "全部",
      },
    ],
    shareData: {},
    Banner: [],
    dataList: [],
    serch_title: "",
    //more
    isLoadMore: true,
    page: 1,
    total_page: 0,
  },
  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);
    }
  },
  openIntroduction() {
    this.setData({
      show: true,
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let language = wx.getStorageSync("langIndex");
    this.setData({
      language: language.en,
    });
    this.getdata(false);
    this.getShare();
  },
  closeM() {
    this.setData({
      show: false,
    });
  },
  handleClick(e) {
    this.setData({
      activeName: e.detail.index,
    });
    this.getdata(true);
  },
  getdata(_tag) {
    let that = this;
    let urlStr = app.getNetAddresss(
      "plugin.snatch-regiment.api.index.getSnatchData"
    );
    app._getNetWork({
      url: urlStr,
      data: {
        art: 'index',
        title: this.data.serch_title||0,
        cate_id: this.data.titleArr ? this.data.titleArr[this.data.activeName].id : 0
      },
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          if (!_tag) {
            let o_titleArr = that.data.titleArr;
            that.setData({
              Banner: res.data.index.slide,
              titleArr: o_titleArr.concat(res.data.index.category),
              explain: res.data.index.explain,
            });
          }

          that.setData({
            isLoadMore: true,
            page: 1,
            total_page: res.data.index.goods.last_page,
            dataList: res.data.index.goods.data,
          });
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  getShare() {
    let urlStr = app.getNetAddresss(
      "plugin.snatch-regiment.api.index.getSnatchData"
    );
    urlStr += "&art=getShare";
    app._getNetWork({
      url: urlStr,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          this.setData({
            shareData: res.data.getShare,
          });
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
        }
      },
    });
  },
  gotoGrabGroup(e) {
    let gid = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: "/packageA/detail_v2/detail_v2?id=" + gid + "&name=69",
    });
  },
  searchbtn(e) {
    let val = e.detail.value;
    this.setData({
      serch_title: val,
    });
  },
  searchConfirm() {
    this.getdata(true);
  },
  gotoMyGroup() {
    wx.navigateTo({
      url: "/packageE/grab_group/grab_group_my/grab_group_my",
    });
  },
  goToUrl(e) {
    console.log(e);
    let link = e.currentTarget.dataset.url;
    try {
      wx.navigateTo({
        url: link,
      });
    } catch (err) {
      console.log(err);
    }
  },

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

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

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

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

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

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.isLoadMore) {
      this._getMoreData();
    } else {
      console.log("没有更多数据");
    }
  },
  //获取更多数据
  _getMoreData() {
    const that = this;
    let json = {};
    let api = "";
    let urlStr = "";
    that.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      this.data.page = this.data.page + 1;
      api = "plugin.snatch-regiment.api.index.getSnatchData";
      json = {
        page: that.data.page,
        art: "index",
        title: that.data.serch_title || 0,
        cate_id: that.data.titleArr[that.data.activeName].id,
      };

      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,
              dataList: that.data.dataList.concat(res.data.index.goods.data),
            });
          } else {
            that.setData({
              page: that.data.page - 1,
              isLoadMore: false,
            });
            return;
          }
        },
        fail: function (res) {
          console.log(res.msg);
        },
      });
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: this.data.shareData.share_title,
      imageUrl: this.data.shareData.share_img,
      path: "/packageE/grab_group/grab_group_home/grab_group_home",
    };
  },
});
