// packageC/micro_communities/microIndex/microIndex.js
const storeAlone = "plugin.store-alone-temp.frontend.microCommunities.index.getStick";
var location = require("../../../mybehaviors/location/location");
var app = getApp();
Page({
  behaviors: [location],
  /**
   * 页面的初始数据
   */
  data: {
    isshow: false, //判断二维码的显示
    poster: '',
    active: 0,
    topContent: {},
    bannerContent: {},
    functionList: {},
    cate: [],
    cateList: [],
    newClassification: '',
    nearbyClassification: '',
    address: '',
    city: '',
    point: '',
    Height: 0,
    LastHeight: 0,
    //more
    isLoadMore: true,
    page: 1,
    total_page: 0,
    getMoreDataType: 'newClassification',
    cateMoreData: {},
    loading: false,
    resizeTabs: false,
    // 门店独立模板
    stroeAloneStyle: false,
    closeNearby: false, //是否关闭附近
    showLoading: true//接口正在请求加载中
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (app.globalData.store_alone_temp == 1) {
      this.setData({
        stroeAloneStyle: true
      });
    }
    this.getHomePageData(options);
    this._getLocation((mapdata, e) => {});
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

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

  },
  onShareTimeline() {},
  openQrCode() {
    //打开二维码
    if (!this.data.poster) {
      this.getPoster();
    } else {
      this.setData({
        isshow: true
      });
    }
  },
  getPoster() {
    let that = this;
    let urlStr = app.getNetAddresss("member.poster");
    app._getNetWork({
      url: urlStr,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            poster: res.data.image_url,
            isshow: true
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 1500
          });
        }

      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  getHomePageData(options) {
    let urlStr = app.getNetAddresss("plugin.micro-communities.api.index.index");
    app._getNetWork({
      url: urlStr,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          if (res.data.set) {
            this.setData({
              topContent: {
                micro_top: res.data.set.micro_top,
                micro_top_jump: res.data.set.micro_top_small,
                browse_num: res.data.browse_num,
                trick_num: res.data.trick_num
              }
            });
            if (res.data.video_set) {
              if (res.data.video_set.video_path) {
                this.setData({
                  video_path: res.data.video_set.video_path
                });
              }
            }
            this.setData({
              bannerContent: res.data.set
            });
            this.setData({
              functionList: {
                show_sign: res.data.set.show_sign == 1 ? true : false,
                show_clock: res.data.set.show_clock == 1 ? true : false,
                show_video: res.data.set.show_video == 1 ? true : false,
                show_qr: res.data.set.show_qr == 1 ? true : false,
              }
            });
          }
          this.data.closeNearby = res.data.nearby_close ? Number(res.data.nearby_close) : false;
          let cateList = this.data.cateList;
          for (let i = 0; i < res.data.cate.length; i++) {
            cateList.push([]);
          }
          this.setData({
            cate: res.data.cate,
            closeNearby: this.data.closeNearby,
            cateList: cateList
          });

          let is_default = true;
          if (options && options.cat_id) {
            // 通过分类id进入
            let cate_index = null;
            let child_flag = false;
            this.data.cate.map((item, i) => {
              if (item.id == options.cat_id) {
                cate_index = i;
              }
              item.child.map((child) => {
                if (child.id == options.cat_id) {
                  cate_index = i;
                  child_flag = true;
                }
              });
            });
            if (cate_index !== null) {
              is_default = false;
              this.setData({
                active: this.data.closeNearby? cate_index + 1 : cate_index + 2
              });
              if (child_flag) {
                this.getCateChild(Number(options.cat_id));
              } else {
                this.getClassificationBtn({
                  detail: {
                    index: this.data.active,
                    title: ''
                  }
                });
              }
            }
          }
          

          if (is_default) {
            this.getClassification('newClassification');
          }
          this.setData({
            resizeTabs: true
          });

        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 1500
          });
        }

      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  getClassification(type, id, index) {
    let urlStr = app.getNetAddresss(app.globalData.store_alone_temp == 1 ? storeAlone : "plugin.micro-communities.api.index.getStick");
    this.pageInit();
    let dataJson = {};
    if (id) {
      dataJson.cat_id = id;
    } else {
      if (type == 'newClassification') {
        dataJson.trick_new = 1;
      } else if (type == 'nearbyClassification') {
        dataJson.lng = this.data.point.lng;
        dataJson.lat = this.data.point.lat;
        dataJson.city_name = this.data.city;
        dataJson.address = this.data.city;
      }
    }
    dataJson.stick_type = 1;
    app._postNetWork({
      url: urlStr,
      showToastIn: false,
      data: dataJson,
      success: (resdata) => {
        this.setData({showLoading: false})
        var res = resdata.data;
        if (res.result == 1) {
          this.data.total_page = res.data.last_page;
          if (id) {
            // let cateList = this.data.cateList;
            this.setData({
              ['cateList[' + index + ']']: res.data.data
            });
          } else {
            if (type == 'newClassification' || app.globalData.store_alone_temp == 1) {
              this.setData({
                newClassification: res.data.data
              });
            } else if (type == 'nearbyClassification') {
              this.setData({
                nearbyClassification: res.data.data
              });
            }
          }
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {}
    });
  },
  jumpToSearch() {
    wx.navigateTo({
      url: '/packageC/micro_communities/microSearchIndex/microSearchIndex',
    });
  },
  jumpAddress(e) {
    let url = e.currentTarget.dataset.url;
    if (url.indexOf('http') < 0) {
      wx.navigateTo({
        url: url
      });
    }
  },
  goToUrl(e) {
    let name = e.currentTarget.dataset.name;
    if (name == 'sign') {
      wx.navigateTo({
        url: '/packageA/member/sign/sign',
      });
    } else if (name == 'ClockPunch') {
      wx.navigateTo({
        url: '/packageA/member/ClockPunch/ClockPunch',
      });
    } else if (name == 'VideoList') {
      if (this.data.video_path == 'videoList') {
        wx.navigateTo({
          url: '/packageC/video_goods/VideoList/VideoList',
        });
      } else if (this.data.video_path == 'videoDetail') {
        wx.navigateTo({
          url: '/packageC/video_goods/VideoDetail/VideoDetail',
        });
      } else {
        wx.navigateTo({
          url: '/packageC/video_goods/VideoList/VideoList',
        });
      }
    }
  },
  goClassification(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/packageC/micro_communities/microClassification/microClassification?cateId=' + id
    });
  },
  jumpDetails(e) {
    let stick_id = e.currentTarget.dataset.stickid;
    wx.navigateTo({
      url: '/packageC/micro_communities/microCommentDetails/microCommentDetails?stickId=' + stick_id
    });
  },
  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);
    }
  },
  getClassificationBtn(e) {
    let index = e.detail.index;
    let title = e.detail.title;
    this.setData({
      active: e.detail.index
    });
    if (title == '最新') {
      this.getClassification('newClassification');
      this.data.getMoreDataType = 'newClassification';
    } else if (title == '附近') {
      this.getClassification('nearbyClassification');
      this.data.getMoreDataType = 'nearbyClassification';
    } else {
      let indexData = this.data.closeNearby? index - 1 : index - 2;
      this.getClassification('', this.data.cate[indexData].id, indexData);
      this.data.getMoreDataType = 'cate';
      this.setData({
        ['cateMoreData.type']: '',
        ['cateMoreData.id']: this.data.cate[indexData].id,
        ['cateMoreData.index']: indexData
      });
    }
  },
  getCateChild(e) {
    let id = e.currentTarget ? e.currentTarget.dataset.id : e;
    let indexData = this.data.closeNearby? this.data.active - 1 : this.data.active - 2;
    this.getClassification('', id, indexData);
    this.data.getMoreDataType = 'cate';
    this.setData({
      ['cateMoreData.type']: '',
      ['cateMoreData.id']: id,
      ['cateMoreData.index']: indexData,
      ['cateList[' + indexData + ']']: []
    });
  },
  pageInit() {
    this.setData({
      page: 1,
      total_page: 0,
      isLoadMore: true,
      loading: false,
      showLoading: true
    });
  },
  getMoreData() {
    let json = {};
    if (this.data.getMoreDataType == "newClassification") {
      json.trick_new = 1;
    } else if (this.data.getMoreDataType == "nearbyClassification") {
      json.lng = this.data.point.lng;
      json.lat = this.data.point.lat;
      json.city_name = this.data.city;
      json.address = this.data.city;
    } else {
      json.cat_id = this.data.cateMoreData.id;
    }
    this.data.isLoadMore = false; // 防止多次请求分页数据
    let urlStr = app.getNetAddresss("plugin.micro-communities.api.index.getStick");
    if (this.data.page >= this.data.total_page) {
      this.setData({
        loading: true
      });
    } else {
      this.data.page = this.data.page + 1;
      json.page = this.data.page;
      json.stick_type = 1;
      app._postNetWork({
        url: urlStr,
        showToastIn: false,
        data: json,
        success: (resdata) => {
          var res = resdata.data;
          if (res.result == 1) {
            var myData = res.data.data;
            if (this.data.getMoreDataType == "newClassification") {
              this.setData({
                newClassification: this.data.newClassification.concat(myData)
              });
            } else if (this.data.getMoreDataType == "nearbyClassification") {
              this.setData({
                nearbyClassification: this.data.nearbyClassification.concat(myData)
              });
            } else {
              this.setData({
                ['cateList[' + this.data.cateMoreData.index + ']']: this.data.cateList[this.data.cateMoreData.index].concat(myData)
              });
            }
          } else {
            this.data.page = this.data.page - 1;
            this.data.isLoadMore = false;
          }
        },
        fail: function (res) {}
      });
    }
  },
  // 隐藏二维码
  clicktapshow() {
    this.setData({
      isshow: false
    });
  }
});