// packageI/newGroup/homeList/homeList.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    paramsId: 0,
    searchText: '',

    explainFlag: false,

    tabs_list: [],
    tabs_index: 0,
    banner_list: [],

    page: 1, //分页数，当前页数
    isLoadMore: true, //判断是否要加载更多的标志
    total_page: 0, //总页数
    listData: [],
    networkLoading: false,

    explainText: "",

    firstLoading: false, //第一次加载，后面只设置listData
    shareConfig: {},
    Height: 0,
    LastHeight: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.id) {
      this.data.paramsId = options.id;
    }
    this.getListData(true);
  },
  setExplainFlag() {
    this.setData({
      explainFlag: !this.data.explainFlag,
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  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);
    }
  },
  setTabsIndex(evt) {
    console.log(evt);
    let index = evt.detail.index;
    this.data.tabs_index = index;
    this.getListData();
  },

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

  },

  tapUrl(e) {
    let link = e.currentTarget.dataset.url;
    if (link == "") return false;
    try {
      wx.navigateTo({
        url: link,
      });
    } catch (err) {
      console.log(err);
    }
  },

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

  },

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

  },

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

  },

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

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let value = wx.getStorageSync("yz_uid");
    let mid = "";
    if (value) {
      mid = value;
    }
    return {
      title: this.data.shareConfig.share_title,
      path: '/packageI/groupWork/groupWorkIndex/groupWorkIndex?&mid=' + mid,
      imageUrl: this.data.shareConfig.share_thumb
    };
  },


  initPage() {
    this.setData({
      listData: [],
      networkLoading: false
    });
    this.data.page = 1;
    this.data.total_page = 0;
    this.data.isLoadMore = true;

  },
  getListData(tag) {
    this.initPage();
    let urlStr = app.getNetAddresss("plugin.group-work.frontend.controllers.page.index");

    let {
      tabs_index,
      tabs_list,
      searchText
    } = this.data;
    if (tabs_list.length != 0) {
      urlStr += "&search[category_id]=" + tabs_list[tabs_index].id;
    } else {
      urlStr += "&search[category_id]=" + this.data.paramsId;
    }
    if (searchText != '') {
      urlStr += "&search[title]=" + searchText;
    }

    app._postNetWork({
      url: urlStr,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result != 1) return app.tips(res.msg);
        let changeData = {};
        if (!this.data.firstLoading) {
          changeData['tabs_list'] = [{
            title: "全部",
            id: 0
          }].concat(res.data.category_list);
          if (tag) {
            changeData.tabs_list.forEach((val, index) => {
              if (val.id == this.data.paramsId) {
                this.setData({
                  tabs_index: index
                });
              }
            });
          }

          changeData['banner_list'] = res.data.banner_list;
          changeData['explainText'] = res.data.explain;
          changeData['firstLoading'] = true;
          wx.setNavigationBarTitle({
            title: res.data.costum_name
          });
          this.data.shareConfig.share_title = res.data.share_title;
          this.data.shareConfig.share_thumb = res.data.share_thumb;
        }
        changeData['isLoadMore'] = true;
        changeData['total_page'] = res.data.page_list.last_page;
        changeData['listData'] = res.data.page_list.data;
        changeData['networkLoading'] = true;
        this.setData(changeData);

        if (!this.data.total_page) {
          this.data.total_page = 0;
        }

      }
    });
  },
  //获取更多数据
  getMoreData() {
    let urlStr = app.getNetAddresss("plugin.group-work.frontend.controllers.page.index");
    // 防止多次请求分页数据
    this.data.isLoadMore = false;
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      this.data.page++;
      let json = {
        page: this.data.page
      };

      let {
        tabs_index,
        tabs_list,
        searchText
      } = this.data;
      if (tabs_list.length != 0) {
        urlStr += "&search[category_id]=" + tabs_list[tabs_index].id;
      }
      if (searchText != '') {
        urlStr += "&search[title]=" + searchText;
      }

      app._getNetWork({
        url: urlStr,
        data: json,
        success: (resdata) => {
          var res = resdata.data;
          if (res.result == 1) {
            this.data.isLoadMore = true;
            var myData = res.data.page_list.data;
            this.setData({
              listData: this.data.listData.concat(myData)
            });
          } else {
            this.data.isLoadMore = false;
            this.data.page--;
          }
        }
      });
    }
  },

  onSearch(e) {
    console.log(e);
    let value = e.detail.value;
    this.data.searchText = value;
    this.getListData();
  },
  clearSearch() {
    this.setData({
      searchText: ''
    });
  },
  gotoUrl(e) {
    if (e.detail) {
      wx.navigateTo({
        url: '/packageA/detail_v2/detail_v2?activity_id=' + e.detail + '&name=99'
      });
    }
  }
});